@@ -87,6 +87,39 @@ export async function findTextRect(
8787 // Limit max_results to prevent performance issues
8888 const limitedMaxResults = Math . min ( maxResults , 100 ) ;
8989
90+ // CRITICAL: Wait for extension injection to complete (CSP-resistant architecture)
91+ // The new architecture loads injected_api.js asynchronously, so window.sentience
92+ // may not be immediately available after page load
93+ try {
94+ await page . waitForFunction (
95+ ( ) => typeof ( window as any ) . sentience !== 'undefined' ,
96+ { timeout : 5000 }
97+ ) ;
98+ } catch ( e ) {
99+ // Gather diagnostics if wait fails
100+ const diag = await page . evaluate ( ( ) => ( {
101+ sentience_defined : typeof ( window as any ) . sentience !== 'undefined' ,
102+ extension_id : document . documentElement . dataset . sentienceExtensionId || 'not set' ,
103+ url : window . location . href
104+ } ) ) . catch ( ( ) => ( { error : 'Could not gather diagnostics' } ) ) ;
105+
106+ throw new Error (
107+ `Sentience extension failed to inject window.sentience API. ` +
108+ `Is the extension loaded? Diagnostics: ${ JSON . stringify ( diag ) } `
109+ ) ;
110+ }
111+
112+ // Verify findTextRect method exists (for older extension versions that don't have it)
113+ const hasFindTextRect = await page . evaluate (
114+ ( ) => typeof ( window as any ) . sentience . findTextRect !== 'undefined'
115+ ) ;
116+ if ( ! hasFindTextRect ) {
117+ throw new Error (
118+ 'window.sentience.findTextRect is not available. ' +
119+ 'Please update the Sentience extension to the latest version.'
120+ ) ;
121+ }
122+
90123 // Call the extension's findTextRect method
91124 const result = await page . evaluate (
92125 ( evalOptions ) => {
0 commit comments