From 6f7d25644d49c48cf6facedb7d9dcc7b3407c975 Mon Sep 17 00:00:00 2001 From: rcholic Date: Mon, 29 Dec 2025 10:40:00 -0800 Subject: [PATCH] fix to findTextRect --- src/textSearch.ts | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/textSearch.ts b/src/textSearch.ts index b28ae641..6cf9101b 100644 --- a/src/textSearch.ts +++ b/src/textSearch.ts @@ -87,6 +87,39 @@ export async function findTextRect( // Limit max_results to prevent performance issues const limitedMaxResults = Math.min(maxResults, 100); + // CRITICAL: Wait for extension injection to complete (CSP-resistant architecture) + // The new architecture loads injected_api.js asynchronously, so window.sentience + // may not be immediately available after page load + try { + await page.waitForFunction( + () => typeof (window as any).sentience !== 'undefined', + { timeout: 5000 } + ); + } catch (e) { + // Gather diagnostics if wait fails + const diag = await page.evaluate(() => ({ + sentience_defined: typeof (window as any).sentience !== 'undefined', + extension_id: document.documentElement.dataset.sentienceExtensionId || 'not set', + url: window.location.href + })).catch(() => ({ error: 'Could not gather diagnostics' })); + + throw new Error( + `Sentience extension failed to inject window.sentience API. ` + + `Is the extension loaded? Diagnostics: ${JSON.stringify(diag)}` + ); + } + + // Verify findTextRect method exists (for older extension versions that don't have it) + const hasFindTextRect = await page.evaluate( + () => typeof (window as any).sentience.findTextRect !== 'undefined' + ); + if (!hasFindTextRect) { + throw new Error( + 'window.sentience.findTextRect is not available. ' + + 'Please update the Sentience extension to the latest version.' + ); + } + // Call the extension's findTextRect method const result = await page.evaluate( (evalOptions) => {