55 lines
2.2 KiB
JavaScript
55 lines
2.2 KiB
JavaScript
"use strict";
|
|
// =============================================================================
|
|
// Pure web-search API via DuckDuckGo HTML endpoint. No API key required.
|
|
// =============================================================================
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.webSearch = webSearch;
|
|
async function webSearch(query) {
|
|
const trimmed = query.trim();
|
|
if (!trimmed)
|
|
return { error: 'No query provided' };
|
|
const url = `https://html.duckduckgo.com/html/?q=${encodeURIComponent(trimmed)}`;
|
|
try {
|
|
const res = await fetch(url, {
|
|
headers: {
|
|
'User-Agent': 'Mozilla/5.0 (compatible; VIBN-Atlas/1.0)',
|
|
Accept: 'text/html',
|
|
},
|
|
signal: AbortSignal.timeout(15000),
|
|
});
|
|
if (!res.ok) {
|
|
return { error: `Search failed with status ${res.status}` };
|
|
}
|
|
const html = await res.text();
|
|
const titles = [];
|
|
for (const m of html.matchAll(/class="result__a"[^>]*href="[^"]*"[^>]*>(.*?)<\/a>/gs)) {
|
|
const title = m[1].replace(/<[^>]+>/g, '').trim();
|
|
if (title)
|
|
titles.push(title);
|
|
}
|
|
const snippets = [];
|
|
for (const m of html.matchAll(/class="result__snippet"[^>]*>(.*?)<\/a>/gs)) {
|
|
const snippet = m[1].replace(/<[^>]+>/g, '').trim();
|
|
if (snippet)
|
|
snippets.push(snippet);
|
|
}
|
|
const count = Math.min(6, Math.max(titles.length, snippets.length));
|
|
const results = [];
|
|
for (let i = 0; i < count; i++) {
|
|
const title = titles[i] || '';
|
|
const snippet = snippets[i] || '';
|
|
if (title || snippet)
|
|
results.push(`**${title}**\n${snippet}`);
|
|
}
|
|
if (results.length === 0)
|
|
return { error: 'No results found' };
|
|
const text = results.join('\n\n');
|
|
const truncated = text.length > 5000 ? text.slice(0, 5000) + '\n\n[...results truncated]' : text;
|
|
return { query: trimmed, results: truncated };
|
|
}
|
|
catch (err) {
|
|
const message = err instanceof Error ? err.message : String(err);
|
|
return { error: `Search request failed: ${message}` };
|
|
}
|
|
}
|