Auto-redirect in presence of search parameters
This commit is contained in:
parent
dbe74f4ed1
commit
f5f4e85e50
|
@ -5,6 +5,7 @@
|
|||
<meta charset="UTF-8">
|
||||
<meta name="referrer" content="no-referrer">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<link rel="search" type="application/opensearchdescription+xml" href="opensearch.xml">
|
||||
<style>
|
||||
* { margin: 0; }
|
||||
html, body { width: 100%; height: 100%; }
|
||||
|
|
48
index.js
48
index.js
|
@ -1,24 +1,38 @@
|
|||
const storageType = 'localStorage'
|
||||
|
||||
var data = {}
|
||||
let data = {}
|
||||
|
||||
const infoEl = document.getElementById('info')
|
||||
const formEl = document.getElementById('search')
|
||||
|
||||
const errors = {
|
||||
missingQuery: new TypeError('Query is missing.'),
|
||||
noFoundInstances: new TypeError('No instances were found.'),
|
||||
}
|
||||
|
||||
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
|
||||
})
|
||||
const url = `${instanceURL}?${params.toString()}`
|
||||
return url
|
||||
}
|
||||
|
||||
// Register event listeners
|
||||
formEl.addEventListener('submit', (event) => {
|
||||
event.preventDefault()
|
||||
if ('instances' in data && data.instances.length > 0) {
|
||||
// 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)
|
||||
}
|
||||
location.assign(buildSearchURL({
|
||||
query: formEl.query.value
|
||||
}))
|
||||
})
|
||||
|
||||
function locallyGetInstances () {
|
||||
|
@ -73,6 +87,16 @@ if (Date.now() > timestamp + 3_600_000 /* 1 hour */) {
|
|||
}
|
||||
}
|
||||
|
||||
// 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({
|
||||
query: searchParams.get('q')
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
if (data.instances.length > 0) {
|
||||
infoEl.textContent = `${data.instances.length} instances`
|
||||
} else {
|
||||
|
|
7
opensearchdescription.xml
Normal file
7
opensearchdescription.xml
Normal file
|
@ -0,0 +1,7 @@
|
|||
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
|
||||
<ShortName>TROM</ShortName>
|
||||
<Description>TROM Metasearch</Description>
|
||||
<InputEncoding>UTF-8</InputEncoding>
|
||||
<Url type="application/opensearchdescription+xml" rel="self" template="https://testsearch.trom.tf/opensearchdescription.xml">
|
||||
<Url type="text/html" template="https://testsearch.trom.tf/?q={searchTerms}">
|
||||
</OpenSearchDescription>
|
Loading…
Reference in New Issue
Block a user