|
9 | 9 | <link rel="icon" href="https://searx.space/favicon.png" sizes="any"> |
10 | 10 | <link rel="icon" href="https://searx.space/favicon.svg" type="image/svg+xml"> |
11 | 11 | <link rel="apple-touch-icon" href="https://searx.space/favicon.png"> |
12 | | -<title>SearXNG Random Instance Redirector</title> |
| 12 | +<title>Random SearXNG Redirector</title> |
13 | 13 | <style> |
14 | 14 | code { background-color: rgba(128, 128, 128, 0.4); padding: 0.1rem 0.25rem 0.1rem 0.25rem } |
15 | 15 | .pale { color: #808080 } |
|
19 | 19 | <script type="module"> |
20 | 20 | import { detectAll } from '{{ site.url }}/assets/js/vendor/tinyld.min.js'; |
21 | 21 |
|
| 22 | +const INSTANCES_URL = 'https://searx.space/data/instances.json'; |
22 | 23 | const INSTANCES = ['search.charliewhiskey.net', 'search.im-in.space', 'search.ipsys.bf', 'search.mycotrip.tech', 'search.ononoki.org', 'search.pollorebozado.com', 'search.rowie.at', 'search.system51.co.uk', 'search.undertale.uk', 'search.url4irl.com', 'searx.bndkt.io', 'searx.dresden.network', 'searx.foss.family', 'searx.oloke.xyz', 'searx.ox2.fr', 'searx.party', 'searx.perennialte.ch', 'searx.tiekoetter.com', 'searx.zhenyapav.com', 'searxng.shreven.org'] |
23 | 24 | const LANGUAGES = ['en']; |
24 | 25 | const TIMEOUT = 5000; |
|
36 | 37 | }); |
37 | 38 | if (params.get('fast') === '1') return instances; |
38 | 39 |
|
39 | | - const response = await fetch('https://searx.space/data/instances.json'); |
| 40 | + const response = await fetch(INSTANCES_URL); |
40 | 41 | if (!response.ok) { |
41 | 42 | console.log('failed to load instances'); |
42 | 43 | return instances; |
|
117 | 118 |
|
118 | 119 | const targetUrl = new URL(shuffledInstances[0]); |
119 | 120 | targetUrl.pathname = '/search'; |
120 | | - targetUrl.searchParams.set('safesearch', params.get('safesearch') || '0'); |
121 | 121 |
|
| 122 | + const newParams = new Map; |
122 | 123 | params |
123 | 124 | .entries() |
124 | 125 | .filter(([k, _]) => !CUSTOM_PARAMS.includes(k)) |
125 | | - .forEach(([k, v]) => targetUrl.searchParams.set(k, v)); |
| 126 | + .forEach(([k, v]) => newParams.set(k, v)); |
126 | 127 |
|
127 | 128 | const query = queryFromForm || params.get('q') || ''; |
128 | | - targetUrl.searchParams.set('language', detectLanguage(query, params)); |
129 | | - targetUrl.searchParams.set('q', query); |
| 129 | + newParams.set('language', detectLanguage(query, params)); |
| 130 | + newParams.set('q', query); |
| 131 | + newParams.set('safesearch', params.get('safesearch') || '0'); |
130 | 132 |
|
131 | 133 | const timeout = Number(params.get('timeout')) || TIMEOUT; |
| 134 | + const getMethod = params.get('get') === '1'; |
132 | 135 | for (let i = 0; i < shuffledInstances.length; i++) setTimeout(_ => { |
133 | 136 | window.stop(); |
134 | 137 | const retryTargetUrl = new URL(targetUrl.toString()); |
135 | 138 | retryTargetUrl.host = new URL(shuffledInstances[i]).host; |
136 | 139 | if (i > 0) { |
137 | 140 | messageElement.innerHTML = `Attempt ${i + 1}/${shuffledInstances.length}: ${retryTargetUrl.host}`; |
138 | 141 | } |
139 | | - const href = retryTargetUrl.toString(); |
140 | | - console.log('trying', href); |
141 | | - if (queryFromForm) { |
142 | | - window.location.href = href; |
| 142 | + if (getMethod) { |
| 143 | + newParams.forEach((value, key) => retryTargetUrl.searchParams.set(key, value)); |
| 144 | + const href = retryTargetUrl.toString(); |
| 145 | + console.log('get', href); |
| 146 | + if (queryFromForm) { |
| 147 | + window.location.href = href; |
| 148 | + } else { |
| 149 | + window.location.replace(href); |
| 150 | + } |
143 | 151 | } else { |
144 | | - window.location.replace(href); |
| 152 | + const href = retryTargetUrl.toString(); |
| 153 | + console.log('post', href); |
| 154 | + const form = document.createElement('form'); |
| 155 | + form.method = 'POST'; |
| 156 | + form.action = href; |
| 157 | + newParams.forEach((value, key) => { |
| 158 | + const input = document.createElement('input'); |
| 159 | + input.type = 'hidden'; |
| 160 | + input.name = key; |
| 161 | + input.value = value; |
| 162 | + form.appendChild(input); |
| 163 | + }); |
| 164 | + document.body.appendChild(form); |
| 165 | + form.submit(); |
| 166 | + document.body.removeChild(form); |
145 | 167 | } |
146 | 168 | }, i * timeout); |
147 | 169 |
|
|
153 | 175 | if (params.has('q')) { |
154 | 176 | redirect(params); |
155 | 177 | } else { |
156 | | - const pageUrl = window.location.origin + window.location.pathname; |
157 | | - document.body.innerHTML = `<p>This page redirects to a <a rel="noopener noreferrer nofollow" href="${pageUrl}#q=" onclick="window.location.replace('${pageUrl}#q=')">random SearXNG</a> instance.</p><p>It also supports the <a rel="noopener noreferrer nofollow" target="_blank" href="https://docs.searxng.org/dev/search_api.html">GET parameters</a> (provided as an anchor for privacy) and the optional parameters:<p><p><ul><li><code>languages=en,fr,ru</code> to use automatic language filter derived from the query; default is <code>${LANGUAGES.join(',')}</code></li><li><code>fast=1</code> to force use of predefined instances (instead of fetching them using the <a rel="noopener noreferrer nofollow" target="_blank" href="https://searx.space/data/instances.json">API</a>); default is <code>0</code></li><li><code>instances=search.ononoki.org,searx.bndkt.io</code> to predefine the instances; default is <code>${INSTANCES.join(',')}</code></li><li><code>timeout</code> in milliseconds until attempting the next instance; default is <code>${TIMEOUT}</code></li><li><code>attempts</code> how many instances to try; default is <code>${ATTEMPTS}</code></li></ul></p><p>Example for <strong>browser search engine settings</strong> (<code>about:preferences#search</code> or <code>chrome://settings/search</code>): <code>${pageUrl}#q=%s&fast=1&image_proxy=True&categories=images&languages=en,fr,ru</code>.</p><p>For local use: <code>wget ${pageUrl} -O searxng.html</code>.</p><p>Additionally can be combined with the Violentmonkey <a rel="noopener noreferrer nofollow" target="_blank" href="https://userscripts.codonaft.com/searxng-redirect-on-failure.js">userscript</a> to turn search failures into redirects as well.</p>`; |
158 | | - |
159 | 178 | const input = document.createElement('input'); |
160 | 179 | input.name = 'q'; |
161 | 180 | input.placeholder = 'Search for...'; |
|
165 | 184 | input.autocorrect = 'off'; |
166 | 185 | input.dir = 'auto'; |
167 | 186 | input.style.width = '99%'; |
| 187 | + input.style.margin = '2rem 0 0 0'; |
168 | 188 |
|
169 | 189 | const form = document.createElement('form'); |
170 | 190 | form.addEventListener('submit', event => { |
|
174 | 194 |
|
175 | 195 | form.appendChild(input); |
176 | 196 | document.body.appendChild(form); |
| 197 | + |
| 198 | + const pageUrl = window.location.origin + window.location.pathname; |
| 199 | + const div = document.createElement('div'); |
| 200 | + div.innerHTML = ` |
| 201 | +<p>This page redirects to a <a rel="noopener noreferrer nofollow" href="${pageUrl}#q=" onclick="window.location.replace('${pageUrl}#q=')">random SearXNG</a> instance.</p> |
| 202 | +<p>It also supports the <a rel="noopener noreferrer nofollow" target="_blank" href="https://docs.searxng.org/dev/search_api.html">GET parameters</a> (provided as an anchor for privacy) and the optional parameters:</p> |
| 203 | +<ul> |
| 204 | + <li><code>languages</code> use automatic language filter derived from the query; comma-separated, default is <code>${LANGUAGES.join(',')}</code></li> |
| 205 | + <li><code>get</code> pass parameters to the instance using GET method; default is <code>0</code></li> |
| 206 | + <li><code>timeout</code> in milliseconds until attempting the next instance; default is <code>${TIMEOUT}</code></li> |
| 207 | + <li><code>attempts</code> how many instances to try; default is <code>${ATTEMPTS}</code></li> |
| 208 | + <li><code>fast</code> always use predefined instances (instead of fetching them using the <a rel="noopener noreferrer nofollow" target="_blank" href="${INSTANCES_URL}">API</a>); default is <code>0</code></li> |
| 209 | + <li><code>instances</code> predefined instances; comma-separated, default is <code>${INSTANCES.join(',')}</code>.</li> |
| 210 | +</ul> |
| 211 | +<p>Example for <strong>browser search engine settings</strong> (<code>about:preferences#search</code> or <code>chrome://settings/search</code>): <code>${pageUrl}#q=%s&fast=1&image_proxy=True&categories=images&languages=en,fr,ru</code>.</p> |
| 212 | +<p>For local use: <code>wget ${pageUrl} -O searxng.html</code>.</p> |
| 213 | +<p>Additionally can be combined with the Violentmonkey <a rel="noopener noreferrer nofollow" target="_blank" href="https://userscripts.codonaft.com/searxng-redirect-on-failure.js">userscript</a> to turn search failures into redirects as well.</p>`; |
| 214 | + document.body.appendChild(div); |
177 | 215 | } |
178 | 216 | }; |
179 | 217 |
|
|
0 commit comments