2023-10-22 15:21:39 +00:00
|
|
|
const storageType = 'localStorage'
|
2023-10-14 12:41:06 +00:00
|
|
|
|
2023-12-13 22:11:22 +00:00
|
|
|
let data = {}
|
2023-10-14 12:41:06 +00:00
|
|
|
|
2024-07-30 09:37:38 +00:00
|
|
|
const statusEl = document.getElementById('status')
|
2023-10-14 12:41:06 +00:00
|
|
|
const formEl = document.getElementById('search')
|
|
|
|
|
2023-12-13 22:11:22 +00:00
|
|
|
const errors = {
|
|
|
|
missingQuery: new TypeError('Query is missing.'),
|
2024-07-30 09:37:38 +00:00
|
|
|
noFoundInstances: new Error('No instances were found.'),
|
2023-12-13 22:11:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function buildSearchURL (opts = {}) {
|
|
|
|
if (typeof opts.query !== 'string') throw errors.missingQuery
|
|
|
|
if (!('instances' in data)) throw errors.noFoundInstances
|
|
|
|
if (data.instances.length === 0) throw errors.noFoundInstances
|
|
|
|
|
|
|
|
// Random instance
|
|
|
|
const n = Math.floor(Math.random() * data.instances.length)
|
|
|
|
const instanceURL = data.instances[n]
|
|
|
|
|
|
|
|
// Build URL
|
|
|
|
const params = new URLSearchParams({
|
|
|
|
q: opts.query
|
|
|
|
})
|
2023-12-30 16:39:20 +00:00
|
|
|
if (opts.category && opts.category !== 'all') {
|
|
|
|
params.append(`category_${opts.category}`, '')
|
|
|
|
}
|
2023-12-13 22:11:22 +00:00
|
|
|
const url = `${instanceURL}?${params.toString()}`
|
|
|
|
return url
|
|
|
|
}
|
|
|
|
|
2023-10-14 12:41:06 +00:00
|
|
|
// Register event listeners
|
|
|
|
formEl.addEventListener('submit', (event) => {
|
2023-10-18 23:50:10 +00:00
|
|
|
event.preventDefault()
|
2023-12-13 22:11:22 +00:00
|
|
|
location.assign(buildSearchURL({
|
2023-12-30 16:39:20 +00:00
|
|
|
query: formEl.query.value,
|
|
|
|
category: formEl.category.value
|
2023-12-13 22:11:22 +00:00
|
|
|
}))
|
2023-10-14 12:41:06 +00:00
|
|
|
})
|
|
|
|
|
2023-10-18 23:50:10 +00:00
|
|
|
function locallyGetInstances () {
|
|
|
|
if (!(storageType in window)) return []
|
|
|
|
const item = window[storageType].getItem('instances')
|
|
|
|
try {
|
|
|
|
return JSON.parse(item)
|
2023-10-22 15:21:39 +00:00
|
|
|
} catch (_err) {
|
2023-10-18 23:50:10 +00:00
|
|
|
return []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2023-10-22 15:21:39 +00:00
|
|
|
if (details.uptime == null) return
|
2023-10-18 23:50:10 +00:00
|
|
|
if (details.uptime.uptimeDay !== 100) return
|
|
|
|
return true
|
|
|
|
}))
|
|
|
|
.then((entries) => entries.map(([url]) => url))
|
|
|
|
.catch((err) => {
|
|
|
|
console.error(err)
|
|
|
|
return []
|
|
|
|
})
|
2023-10-22 15:21:39 +00:00
|
|
|
window[storageType].setItem('timestamp', Date.now().toString())
|
2023-10-18 23:50:10 +00:00
|
|
|
return instances
|
|
|
|
}
|
|
|
|
|
2023-10-22 15:21:39 +00:00
|
|
|
// Get local instances
|
2023-10-18 23:50:10 +00:00
|
|
|
data.instances = locallyGetInstances()
|
2023-10-22 15:21:39 +00:00
|
|
|
|
|
|
|
// Fetch remote instances if necessary
|
|
|
|
const timestamp = +window[storageType].getItem('timestamp')
|
|
|
|
if (Date.now() > timestamp + 3_600_000 /* 1 hour */) {
|
|
|
|
const remoteInstances = await remotelyGetInstances()
|
|
|
|
if (remoteInstances.length > 0) {
|
|
|
|
data.instances = remoteInstances
|
|
|
|
locallySetInstances()
|
|
|
|
}
|
2023-10-18 23:50:10 +00:00
|
|
|
}
|
|
|
|
|
2023-12-13 22:11:22 +00:00
|
|
|
// Auto-redirect in presence of search parameters
|
|
|
|
if (location.search.length > 0) {
|
|
|
|
const searchParams = new URLSearchParams(location.search.slice(1))
|
|
|
|
if (searchParams.has('q')) {
|
|
|
|
location.assign(buildSearchURL({
|
2023-12-30 16:39:20 +00:00
|
|
|
query: searchParams.get('q'),
|
|
|
|
category: searchParams.get('in') ?? 'all'
|
2023-12-13 22:11:22 +00:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-30 09:37:38 +00:00
|
|
|
statusEl.textContent = `${data.instances.length} instances`
|
|
|
|
if (data.instances.length === 0) {
|
|
|
|
statusEl.classList.add('error')
|
2023-12-30 16:39:20 +00:00
|
|
|
}
|