aboutsummaryrefslogtreecommitdiff
path: root/src/domains/search/web-search.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/domains/search/web-search.ts')
-rw-r--r--src/domains/search/web-search.ts30
1 files changed, 29 insertions, 1 deletions
diff --git a/src/domains/search/web-search.ts b/src/domains/search/web-search.ts
index 9327f15..553135c 100644
--- a/src/domains/search/web-search.ts
+++ b/src/domains/search/web-search.ts
@@ -13,7 +13,6 @@ import {
waitForIsolatedLoad,
} from "../isolated-tab";
import { renderExpandableText } from "../render";
-import { buildSerpExtractionExpr } from "./extract";
import { classifySerp, parseGoogleSerp, type SearchResult, type SerpExtraction } from "./google-serp";
const LOAD_TIMEOUT_MS = 15_000;
@@ -25,6 +24,35 @@ const WebSearchArgs = Type.Object({
),
});
+const PAGE_TEXT_LIMIT = 4000;
+
+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 });
+ })()
+`;
+
const buildSerpUrl = (query: string, limit: number): string =>
`https://www.google.com/search?q=${encodeURIComponent(query)}&num=${limit}&hl=en`;