@@ -19,8 +19,16 @@ export class SentienceBrowser {
1919 ) { }
2020
2121 async start ( ) : Promise < void > {
22- // Get extension source path
23- const repoRoot = path . resolve ( __dirname , '../../..' ) ;
22+ // Get extension source path (relative to project root)
23+ // Handle both ts-node (src/) and compiled (dist/src/) cases
24+ let repoRoot : string ;
25+ if ( __dirname . includes ( 'dist' ) ) {
26+ // Compiled: dist/src/ -> go up 3 levels to project root (Sentience/)
27+ repoRoot = path . resolve ( __dirname , '../../..' ) ;
28+ } else {
29+ // ts-node: src/ -> go up 2 levels to project root (Sentience/)
30+ repoRoot = path . resolve ( __dirname , '../..' ) ;
31+ }
2432 const extensionSource = path . join ( repoRoot , 'sentience-chrome' ) ;
2533
2634 if ( ! fs . existsSync ( extensionSource ) ) {
@@ -32,7 +40,7 @@ export class SentienceBrowser {
3240
3341 // Create temporary extension bundle
3442 const tempDir = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , 'sentience-ext-' ) ) ;
35- this . extensionPath = tempDir ;
43+ this . extensionPath = tempDir ; // tempDir is already a string
3644
3745 // Copy extension files
3846 const filesToCopy = [
@@ -61,8 +69,8 @@ export class SentienceBrowser {
6169 this . browser = await chromium . launch ( {
6270 headless : this . headless ,
6371 args : [
64- `--load-extension=${ tempDir . name } ` ,
65- `--disable-extensions-except=${ tempDir . name } ` ,
72+ `--load-extension=${ tempDir } ` ,
73+ `--disable-extensions-except=${ tempDir } ` ,
6674 ] ,
6775 } ) ;
6876
@@ -74,8 +82,28 @@ export class SentienceBrowser {
7482 // Create page
7583 this . page = await this . context . newPage ( ) ;
7684
77- // Wait for extension
78- await this . waitForExtension ( ) ;
85+ // Navigate to a real page so extension can inject
86+ // Extension content scripts only run on actual pages (not about:blank)
87+ // Use a simple page that loads quickly
88+ await this . page . goto ( 'https://example.com' , { waitUntil : 'domcontentloaded' } ) ;
89+
90+ // Give extension time to initialize (WASM loading is async)
91+ await this . page . waitForTimeout ( 1000 ) ;
92+
93+ // Wait for extension to load
94+ if ( ! ( await this . waitForExtension ( ) ) ) {
95+ // Extension might need more time, try waiting a bit longer
96+ await this . page . waitForTimeout ( 2000 ) ;
97+ if ( ! ( await this . waitForExtension ( ) ) ) {
98+ throw new Error (
99+ 'Extension failed to load after navigation. Make sure:\n' +
100+ '1. Extension is built (cd sentience-chrome && ./build.sh)\n' +
101+ '2. All files are present (manifest.json, content.js, injected_api.js, pkg/)\n' +
102+ '3. Check browser console for errors\n' +
103+ `4. Extension path: ${ tempDir } `
104+ ) ;
105+ }
106+ }
79107 }
80108
81109 private copyDirectory ( src : string , dest : string ) : void {
@@ -97,27 +125,36 @@ export class SentienceBrowser {
97125 }
98126 }
99127
100- private async waitForExtension ( timeout : number = 10000 ) : Promise < boolean > {
128+ private async waitForExtension ( timeout : number = 15000 ) : Promise < boolean > {
101129 if ( ! this . page ) return false ;
102130
103131 const start = Date . now ( ) ;
104132 while ( Date . now ( ) - start < timeout ) {
105133 try {
106134 const result = await this . page . evaluate ( ( ) => {
107- return (
108- typeof ( window as any ) . sentience !== 'undefined' &&
109- typeof ( window as any ) . sentience . snapshot === 'function'
110- ) ;
135+ // Check if sentience API exists
136+ if ( typeof ( window as any ) . sentience === 'undefined' ) {
137+ return { ready : false , reason : 'window.sentience not defined' } ;
138+ }
139+ // Check if snapshot function exists
140+ if ( typeof ( window as any ) . sentience . snapshot !== 'function' ) {
141+ return { ready : false , reason : 'snapshot function not available' } ;
142+ }
143+ // Check if WASM module is loaded
144+ if ( ( window as any ) . sentience_registry === undefined ) {
145+ return { ready : false , reason : 'registry not initialized' } ;
146+ }
147+ return { ready : true } ;
111148 } ) ;
112149
113- if ( result ) {
150+ if ( result && ( result as any ) . ready ) {
114151 return true ;
115152 }
116153 } catch ( e ) {
117- // Ignore errors during initialization
154+ // Continue waiting on errors
118155 }
119156
120- await new Promise ( ( resolve ) => setTimeout ( resolve , 100 ) ) ;
157+ await new Promise ( ( resolve ) => setTimeout ( resolve , 200 ) ) ;
121158 }
122159
123160 return false ;
0 commit comments