blob: 4d9e8928a3313f9e6e3d7210568b88f3b9396836 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
const PAGE_TEXT_LIMIT = 4000;
export const buildSerpExtractionExpr = (): string => `
(() => {
const main = document.querySelector('#search, #rso, #main') || document.body;
const headings = Array.from(main.querySelectorAll('a h3'));
const seen = new Set();
const anchors = [];
for (const h3 of headings) {
const a = h3.closest('a[href]');
if (!a) continue;
const href = a.href;
if (!href || seen.has(href)) continue;
seen.add(href);
const container =
a.closest('div.g') ||
a.closest('div[data-hveid]') ||
a.parentElement?.parentElement ||
a.parentElement;
const containerText = container ? container.innerText || '' : '';
const heading = h3.innerText || h3.textContent || '';
const snippet = containerText.replace(heading, '').replace(/\\s+/g, ' ').trim().slice(0, 500);
anchors.push({ href, heading, snippet });
}
const pageText = (document.body.innerText || '').slice(0, ${PAGE_TEXT_LIMIT});
return JSON.stringify({ anchors, pageText });
})()
`;
|