commit b37d5417b781fbd59ce0c7bce1911d44f0f52fd2 Author: Danny Harpigny Date: Sat Oct 14 14:41:06 2023 +0200 Initial commit diff --git a/data.json b/data.json new file mode 100644 index 0000000..94ee9c9 --- /dev/null +++ b/data.json @@ -0,0 +1,5 @@ +{ + "instances": [ + "https://searxng.no-logs.com/search" + ] +} diff --git a/index.html b/index.html new file mode 100644 index 0000000..f88df2b --- /dev/null +++ b/index.html @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + diff --git a/index.js b/index.js new file mode 100644 index 0000000..743a27b --- /dev/null +++ b/index.js @@ -0,0 +1,42 @@ +// Register service worker +if ('serviceWorker' in navigator) { + navigator.serviceWorker.register('./sw.js', { + type: 'module' + }) + .then(() => console.log('sw ok')) + .catch(() => console.log('sw not ok')) +} + +var data = {} + +const infoEl = document.getElementById('info') +const formEl = document.getElementById('search') + +// Register event listeners +formEl.addEventListener('submit', (event) => { + if ('instances' in data && data.instances.length > 0) { + event.preventDefault() + // Random instance + const n = Math.floor(Math.random() * data.instances.length) + const instanceURL = data.instances[n] + // Assign URL + const params = new URLSearchParams({ + q: formEl.query.value + }) + const url = `${instanceURL}?${params.toString()}` + location.assign(url) + } +}) + +// Fetch data +await fetch('./data.json') + .then(req => req.json()) + .then(newData => { data = newData }) + .then(() => { + infoEl.textContent = `${data.instances.length} instances` + }) + .catch(err => { + console.error(err) + infoEl.textContent = err.toString() + infoEl.classList.add('error') + }) diff --git a/sw.js b/sw.js new file mode 100644 index 0000000..15937d9 --- /dev/null +++ b/sw.js @@ -0,0 +1,30 @@ +const cacheName = 'v1' + +self.addEventListener('install', (event) => { + event.waitUntil( + caches + .open(cacheName) + .then(cache => cache.addAll([ + '.', + './index.html', + './index.js', + './index.css', + './instances.json' + ])) + ) +}) + +self.addEventListener('activate', (_event) => { +}) + +self.addEventListener('fetch', async (event) => { + const res = await fetch(event.request) + if (!res.ok) { + const cachedRes = await caches.match(event.request) + if (cachedRes) { + event.respondWith(cachedRes) + return + } + } + event.respondWith(res) +})