v3.6.6: search extraction parses both nestings (Bing a-in-h2, Google h3-in-a) so native web_search returns sources
This commit is contained in:
@@ -1370,22 +1370,24 @@ const FETCH_EXTRACT = `(async () => {
|
||||
const SEARCH_EXTRACT = `(() => {
|
||||
const clean = (s) => String(s || '').replace(/\\s+/g, ' ').trim();
|
||||
const out = []; const seen = new Set();
|
||||
// Engine-agnostic organic parse: a result headline is a heading inside a
|
||||
// link on Google and Bing alike — layout class names drift, this doesn't.
|
||||
for (const a of document.querySelectorAll('a[href]')) {
|
||||
const heading = a.querySelector('h2, h3');
|
||||
if (!heading) continue;
|
||||
let url = a.href;
|
||||
if (url.startsWith('/url?')) { const m = url.match(/[?&]url=([^&]+)/); if (m) { try { url = decodeURIComponent(m[1]); } catch {} } }
|
||||
if (!/^https?:/i.test(url) || seen.has(url)) continue;
|
||||
const title = clean(heading.innerText);
|
||||
const selfUrl = (u) => /^https?:\\/\\/(?:www\\.)?(?:bing|google|duckduckgo)\\./i.test(u);
|
||||
// Organic result headlines come in both directions now: heading inside the
|
||||
// link (Google: a > h3) AND link inside the heading (Bing: h2 > a). Match
|
||||
// both — engine class names drift, the heading/link nesting does not.
|
||||
for (const node of document.querySelectorAll('h2 a, h3 a, a h2, a h3')) {
|
||||
if (out.length >= 20) break;
|
||||
const link = node.matches('a') ? node : node.closest('a');
|
||||
if (!link) continue;
|
||||
let url = link.href;
|
||||
if (/^\\/(url|link)\\?/.test(url)) { const m = url.match(/[?&](?:url|q)=([^&]+)/); if (m) { try { url = decodeURIComponent(m[1]); } catch {} } }
|
||||
if (!/^https?:/i.test(url) || seen.has(url) || selfUrl(url)) continue;
|
||||
const title = clean(link.innerText);
|
||||
if (!title || title.length < 4) continue;
|
||||
const box = a.closest('li,div.g,div.tF2Cxc,li.b_algo') || a.parentElement;
|
||||
const box = link.closest('li,div.g,div.tF2Cxc,li.b_algo,div.b_title,div[class*=result]') || link.parentElement;
|
||||
let snippet = box ? clean(box.innerText) : '';
|
||||
if (snippet.startsWith(title)) snippet = snippet.slice(title.length).trim();
|
||||
seen.add(url);
|
||||
out.push({ url, title: title.slice(0, 200), ...(snippet ? { snippet: snippet.slice(0, 400) } : {}) });
|
||||
if (out.length >= 20) break;
|
||||
}
|
||||
return { sources: out, page: { url: location.href, title: clean(document.title) } };
|
||||
})()`;
|
||||
|
||||
Reference in New Issue
Block a user