n8n-nodes-duckduckgo-search
Version:
A powerful and comprehensive n8n community node that seamlessly integrates DuckDuckGo search capabilities into your workflows. Search the web, find images, discover news, and explore videos - all with privacy-focused, reliable results.
52 lines (51 loc) • 1.98 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseHtmlResults = void 0;
function parseHtmlResults(html) {
const results = [];
const resultRegex = /<div[^>]*class="[^"]*result[^"]*"[^>]*>([\s\S]*?)<\/div>/gi;
let match;
while ((match = resultRegex.exec(html)) !== null) {
const resultDiv = match[1];
const titleRegex = /<a[^>]*class="[^"]*result__a[^"]*"[^>]*(?:href="([^"]*)"[^>]*)?>(.*?)<\/a>/i;
const titleMatch = titleRegex.exec(resultDiv);
if (!titleMatch)
continue;
const title = titleMatch[2] ? titleMatch[2].replace(/<[^>]*>/g, '').trim() : '';
const url = titleMatch[1] || '';
let snippet = '';
const snippetARegex = /<a[^>]*class="[^"]*result__snippet[^"]*"[^>]*>(.*?)<\/a>/i;
const snippetAMatch = snippetARegex.exec(resultDiv);
if (snippetAMatch) {
snippet = snippetAMatch[1].replace(/<[^>]*>/g, '').trim();
}
else {
const snippetDivRegex = /<div[^>]*class="[^"]*result__snippet[^"]*"[^>]*>(.*?)<\/div>/i;
const snippetDivMatch = snippetDivRegex.exec(resultDiv);
if (snippetDivMatch) {
snippet = snippetDivMatch[1].replace(/<[^>]*>/g, '').trim();
}
}
if (title && url && title.length > 0 && url.length > 0) {
results.push({
title: cleanHtml(title),
url,
snippet: cleanHtml(snippet),
source: 'html',
});
}
}
return results;
}
exports.parseHtmlResults = parseHtmlResults;
function cleanHtml(text) {
return text
.replace(/ /g, ' ')
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, "'")
.replace(/\s+/g, ' ')
.trim();
}