This commit is contained in:
Danny Harpigny 2023-10-19 01:50:10 +02:00
parent b37d5417b7
commit 428b042fe9
3 changed files with 63 additions and 52 deletions

View File

@ -6,13 +6,23 @@
<meta name="referrer" content="no-referrer"> <meta name="referrer" content="no-referrer">
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<style> <style>
* { margin: 0; }
html, body { width: 100%; height: 100%; }
#search-box {
position: fixed;
left: 50%;
top: 1rem;
transform: translateX(-50%);
}
#info { color: gray; } #info { color: gray; }
#info.error { color: red; } #info.error { color: red; }
</style> </style>
<script type="module" src="index.js"></script> <script type="module" src="index.js"></script>
</head> </head>
<body> <body>
<div class="search-box"> <div id="search-box">
<form id="search"> <form id="search">
<input name="query" type="text" placeholder="Query"> <input name="query" type="text" placeholder="Query">
<input type="submit" value="Search"> <input type="submit" value="Search">

View File

@ -1,11 +1,4 @@
// Register service worker const storageType = 'sessionStorage'
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('./sw.js', {
type: 'module'
})
.then(() => console.log('sw ok'))
.catch(() => console.log('sw not ok'))
}
var data = {} var data = {}
@ -14,8 +7,8 @@ const formEl = document.getElementById('search')
// Register event listeners // Register event listeners
formEl.addEventListener('submit', (event) => { formEl.addEventListener('submit', (event) => {
event.preventDefault()
if ('instances' in data && data.instances.length > 0) { if ('instances' in data && data.instances.length > 0) {
event.preventDefault()
// Random instance // Random instance
const n = Math.floor(Math.random() * data.instances.length) const n = Math.floor(Math.random() * data.instances.length)
const instanceURL = data.instances[n] const instanceURL = data.instances[n]
@ -28,15 +21,53 @@ formEl.addEventListener('submit', (event) => {
} }
}) })
// Fetch data function locallyGetInstances () {
await fetch('./data.json') if (!(storageType in window)) return []
.then(req => req.json()) const item = window[storageType].getItem('instances')
.then(newData => { data = newData }) try {
.then(() => { return JSON.parse(item)
infoEl.textContent = `${data.instances.length} instances` } finally {
}) return []
.catch(err => { }
console.error(err) }
infoEl.textContent = err.toString()
infoEl.classList.add('error') function locallySetInstances () {
}) if (!(storageType in window)) return
if (!('instances' in data)) return
if (data.instances.length === 0) return
window[storageType].setItem(
'instances',
JSON.stringify(data.instances)
)
}
async function remotelyGetInstances () {
const instances = await fetch('https://searx.space/data/instances.json')
.then((req) => req.json())
.then((json) => Object.entries(json.instances))
.then((entries) => entries.filter(([_url, details]) => {
if (details.network_type !== 'normal') return
if (details.uptime.uptimeDay !== 100) return
return true
}))
.then((entries) => entries.map(([url]) => url))
.catch((err) => {
console.error(err)
return []
})
return instances
}
data.instances = locallyGetInstances()
const remoteInstances = await remotelyGetInstances()
if (remoteInstances.length > 0) {
data.instances = remoteInstances
locallySetInstances()
}
if (data.instances.length > 0) {
infoEl.textContent = `${data.instances.length} instances`
} else {
infoEl.classList.add('error')
infoEl.textContent = '0 instances'
}

30
sw.js
View File

@ -1,30 +0,0 @@
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)
})