This commit is contained in:
Tommy D. Rossi
2025-11-15 13:57:05 +01:00
parent 39750a231a
commit 36da6a1737
2 changed files with 49 additions and 36 deletions
+15 -14
View File
@@ -51,7 +51,7 @@ class SimplifiedExtension {
if (!this._connection) { if (!this._connection) {
debugLog('No existing connection, creating new relay connection'); debugLog('No existing connection, creating new relay connection');
debugLog('Waiting for server at http://localhost:9988...'); debugLog('Waiting for server at http://localhost:9988...');
// Wait for server to be available // Wait for server to be available
while (true) { while (true) {
try { try {
@@ -63,11 +63,11 @@ class SimplifiedExtension {
await new Promise(resolve => setTimeout(resolve, 1000)); await new Promise(resolve => setTimeout(resolve, 1000));
} }
} }
debugLog('Server is ready, creating WebSocket connection to:', RELAY_URL); debugLog('Server is ready, creating WebSocket connection to:', RELAY_URL);
const socket = new WebSocket(RELAY_URL); const socket = new WebSocket(RELAY_URL);
debugLog('WebSocket created, initial readyState:', socket.readyState, '(0=CONNECTING, 1=OPEN, 2=CLOSING, 3=CLOSED)'); debugLog('WebSocket created, initial readyState:', socket.readyState, '(0=CONNECTING, 1=OPEN, 2=CLOSING, 3=CLOSED)');
await new Promise<void>((resolve, reject) => { await new Promise<void>((resolve, reject) => {
let timeoutFired = false; let timeoutFired = false;
const timeout = setTimeout(() => { const timeout = setTimeout(() => {
@@ -78,7 +78,7 @@ class SimplifiedExtension {
debugLog('Socket protocol:', socket.protocol); debugLog('Socket protocol:', socket.protocol);
reject(new Error('Connection timeout')); reject(new Error('Connection timeout'));
}, 5000); }, 5000);
socket.onopen = () => { socket.onopen = () => {
if (timeoutFired) { if (timeoutFired) {
debugLog('WebSocket opened but timeout already fired!'); debugLog('WebSocket opened but timeout already fired!');
@@ -88,7 +88,7 @@ class SimplifiedExtension {
clearTimeout(timeout); clearTimeout(timeout);
resolve(); resolve();
}; };
socket.onerror = (error) => { socket.onerror = (error) => {
debugLog('WebSocket onerror during connection:', error); debugLog('WebSocket onerror during connection:', error);
debugLog('Error type:', error.type); debugLog('Error type:', error.type);
@@ -98,7 +98,7 @@ class SimplifiedExtension {
reject(new Error('WebSocket connection failed')); reject(new Error('WebSocket connection failed'));
} }
}; };
socket.onclose = (event) => { socket.onclose = (event) => {
debugLog('WebSocket onclose during connection setup:', { debugLog('WebSocket onclose during connection setup:', {
code: event.code, code: event.code,
@@ -111,7 +111,7 @@ class SimplifiedExtension {
reject(new Error(`WebSocket closed: ${event.reason || event.code}`)); reject(new Error(`WebSocket closed: ${event.reason || event.code}`));
} }
}; };
debugLog('Event handlers set, waiting for connection...'); debugLog('Event handlers set, waiting for connection...');
}); });
@@ -146,7 +146,7 @@ class SimplifiedExtension {
const targetInfo = await this._connection.attachTab(tabId); const targetInfo = await this._connection.attachTab(tabId);
debugLog('attachTab completed, storing in connectedTabs map'); debugLog('attachTab completed, storing in connectedTabs map');
this._connectedTabs.set(tabId, targetInfo.targetId); this._connectedTabs.set(tabId, targetInfo.targetId);
await this._updateIcon(tabId, 'connected'); await this._updateIcon(tabId, 'connected');
debugLog(`=== Successfully connected to tab ${tabId} ===`); debugLog(`=== Successfully connected to tab ${tabId} ===`);
} catch (error: any) { } catch (error: any) {
@@ -170,19 +170,19 @@ class SimplifiedExtension {
private async _disconnectTab(tabId: number): Promise<void> { private async _disconnectTab(tabId: number): Promise<void> {
debugLog(`=== Disconnecting tab ${tabId} ===`); debugLog(`=== Disconnecting tab ${tabId} ===`);
if (!this._connectedTabs.has(tabId)) { if (!this._connectedTabs.has(tabId)) {
debugLog('Tab not in connectedTabs map, ignoring disconnect'); debugLog('Tab not in connectedTabs map, ignoring disconnect');
return; return;
} }
debugLog('Calling detachTab on connection'); debugLog('Calling detachTab on connection');
this._connection?.detachTab(tabId); this._connection?.detachTab(tabId);
this._connectedTabs.delete(tabId); this._connectedTabs.delete(tabId);
debugLog('Tab removed from connectedTabs map'); debugLog('Tab removed from connectedTabs map');
await this._updateIcon(tabId, 'disconnected'); await this._updateIcon(tabId, 'disconnected');
debugLog('Connected tabs remaining:', this._connectedTabs.size); debugLog('Connected tabs remaining:', this._connectedTabs.size);
if (this._connectedTabs.size === 0 && this._connection) { if (this._connectedTabs.size === 0 && this._connection) {
debugLog('No tabs remaining, closing relay connection'); debugLog('No tabs remaining, closing relay connection');
@@ -247,7 +247,7 @@ class SimplifiedExtension {
private _onTabRemoved = async (tabId: number): Promise<void> => { private _onTabRemoved = async (tabId: number): Promise<void> => {
debugLog('Tab removed event for tab:', tabId, 'is connected:', this._connectedTabs.has(tabId)); debugLog('Tab removed event for tab:', tabId, 'is connected:', this._connectedTabs.has(tabId));
if (!this._connectedTabs.has(tabId)) return; if (!this._connectedTabs.has(tabId)) return;
debugLog(`Connected tab ${tabId} was closed, disconnecting`); debugLog(`Connected tab ${tabId} was closed, disconnecting`);
await this._disconnectTab(tabId); await this._disconnectTab(tabId);
}; };
@@ -259,4 +259,5 @@ class SimplifiedExtension {
}; };
} }
new SimplifiedExtension(); // @ts-ignore
globalThis.state = new SimplifiedExtension();
+34 -22
View File
@@ -1,43 +1,55 @@
import playwright from 'playwright-core'; import playwright from 'playwright-core'
async function main() { async function main() {
const cdpEndpoint = `ws://localhost:9988/cdp/${Date.now()}` const cdpEndpoint = `ws://localhost:9988/cdp/${Date.now()}`
const browser = await playwright.chromium.connectOverCDP(cdpEndpoint); const browser = await playwright.chromium.connectOverCDP(cdpEndpoint)
const contexts = browser.contexts(); const contexts = browser.contexts()
console.log(`Found ${contexts.length} browser context(s)`); console.log(`Found ${contexts.length} browser context(s)`)
// Sleep 200 ms // Sleep 200 ms
await new Promise(resolve => setTimeout(resolve, 200)); await new Promise((resolve) => setTimeout(resolve, 200))
for (const context of contexts) { for (const context of contexts) {
const pages = context.pages(); const pages = context.pages()
console.log(`Context has ${pages.length} page(s):`); console.log(`Context has ${pages.length} page(s):`)
for (const page of pages) { for (const page of pages) {
const url = page.url(); const url = page.url()
console.log(`\nPage URL: ${url}`); console.log(`\nPage URL: ${url}`)
const html = await page.content(); const html = await page.content()
const lines = html.split('\n').slice(0, 3); const lines = html.split('\n').slice(0, 3)
console.log('First 3 lines of HTML:'); console.log('First 3 lines of HTML:')
lines.forEach((line, i) => { lines.forEach((line, i) => {
console.log(` ${i + 1}: ${line}`); console.log(` ${i + 1}: ${line}`)
}); })
// Watch for browser console logs and log them in Node.js // Watch for browser console logs and log them in Node.js
page.on('console', msg => { page.on('console', (msg) => {
console.log(`Browser log: [${msg.type()}] ${msg.text()}`); console.log(`Browser log: [${msg.type()}] ${msg.text()}`)
}); })
console.log(`running eval`) console.log(`running eval`)
// Evaluate a sum in the browser and log something from inside the browser context // Evaluate a sum in the browser and log something from inside the browser context
const sumResult = await page.evaluate(() => { const sumResult = await page.evaluate(() => {
console.log('Logging from inside browser context!'); console.log('Logging from inside browser context!')
return 1 + 2 + 3; return 1 + 2 + 3
}); })
console.log(`Sum result evaluated in browser: ${sumResult}`); console.log(`Sum result evaluated in browser: ${sumResult}`)
if ((page as any)._snapshotForAI) {
const snapshot = await (page as any)._snapshotForAI()
const snapshotStr =
typeof snapshot === 'string'
? snapshot
: JSON.stringify(snapshot)
console.log(
'First 100 chars of _snapshotForAI():',
snapshotStr.slice(0, 100),
)
} else {
console.log('_snapshotForAI is not available on this page.')
}
} }
} }
} }
main() main()