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 charset="UTF-8">
|
||||||
<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">
|
||||||
|
<link rel="search" type="application/opensearchdescription+xml" href="opensearch.xml">
|
||||||
<style>
|
<style>
|
||||||
* { margin: 0; }
|
* { margin: 0; }
|
||||||
html, body { width: 100%; height: 100%; }
|
html, body { width: 100%; height: 100%; }
|
||||||
|
|
40
index.js
40
index.js
|
@ -1,24 +1,38 @@
|
||||||
const storageType = 'localStorage'
|
const storageType = 'localStorage'
|
||||||
|
|
||||||
var data = {}
|
let data = {}
|
||||||
|
|
||||||
const infoEl = document.getElementById('info')
|
const infoEl = document.getElementById('info')
|
||||||
const formEl = document.getElementById('search')
|
const formEl = document.getElementById('search')
|
||||||
|
|
||||||
// Register event listeners
|
const errors = {
|
||||||
formEl.addEventListener('submit', (event) => {
|
missingQuery: new TypeError('Query is missing.'),
|
||||||
event.preventDefault()
|
noFoundInstances: new TypeError('No instances were found.'),
|
||||||
if ('instances' in data && data.instances.length > 0) {
|
}
|
||||||
|
|
||||||
|
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
|
// 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]
|
||||||
// Assign URL
|
|
||||||
|
// Build URL
|
||||||
const params = new URLSearchParams({
|
const params = new URLSearchParams({
|
||||||
q: formEl.query.value
|
q: opts.query
|
||||||
})
|
})
|
||||||
const url = `${instanceURL}?${params.toString()}`
|
const url = `${instanceURL}?${params.toString()}`
|
||||||
location.assign(url)
|
return url
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Register event listeners
|
||||||
|
formEl.addEventListener('submit', (event) => {
|
||||||
|
event.preventDefault()
|
||||||
|
location.assign(buildSearchURL({
|
||||||
|
query: formEl.query.value
|
||||||
|
}))
|
||||||
})
|
})
|
||||||
|
|
||||||
function locallyGetInstances () {
|
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) {
|
if (data.instances.length > 0) {
|
||||||
infoEl.textContent = `${data.instances.length} instances`
|
infoEl.textContent = `${data.instances.length} instances`
|
||||||
} else {
|
} 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