Fix search provider

This commit is contained in:
Cohee 2024-06-21 22:31:34 +03:00
parent f2cc66d414
commit e3714e9b6a
1 changed files with 26 additions and 15 deletions

View File

@ -147,25 +147,36 @@ router.post('/searxng', jsonParser, async (request, response) => {
console.log('SearXNG query', baseUrl, query);
const url = new URL(baseUrl);
const params = new URLSearchParams();
params.append('q', query);
params.append('format', 'html');
url.pathname = '/search';
url.search = params.toString();
const mainPageUrl = new URL(baseUrl);
const mainPageRequest = await fetch(mainPageUrl, { headers: visitHeaders });
const result = await fetch(url, {
method: 'POST',
headers: visitHeaders,
});
if (!result.ok) {
const text = await result.text();
console.log('SearXNG request failed', result.statusText, text);
if (!mainPageRequest.ok) {
console.log('SearXNG request failed', mainPageRequest.statusText);
return response.sendStatus(500);
}
const data = await result.text();
const mainPageText = await mainPageRequest.text();
const clientHref = mainPageText.match(/href="(\/client.+\.css)"/)?.[1];
if (clientHref) {
const clientUrl = new URL(clientHref, baseUrl);
await fetch(clientUrl, { headers: visitHeaders });
}
const searchUrl = new URL('/search', baseUrl);
const searchParams = new URLSearchParams();
searchParams.append('q', query);
searchUrl.search = searchParams.toString();
const searchResult = await fetch(searchUrl, { headers: visitHeaders });
if (!searchResult.ok) {
const text = await searchResult.text();
console.log('SearXNG request failed', searchResult.statusText, text);
return response.sendStatus(500);
}
const data = await searchResult.text();
return response.send(data);
} catch (error) {
console.log('SearXNG request failed', error);