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) {
debugLog('No existing connection, creating new relay connection');
debugLog('Waiting for server at http://localhost:9988...');
// Wait for server to be available
while (true) {
try {
@@ -63,11 +63,11 @@ class SimplifiedExtension {
await new Promise(resolve => setTimeout(resolve, 1000));
}
}
debugLog('Server is ready, creating WebSocket connection to:', RELAY_URL);
const socket = new WebSocket(RELAY_URL);
debugLog('WebSocket created, initial readyState:', socket.readyState, '(0=CONNECTING, 1=OPEN, 2=CLOSING, 3=CLOSED)');
await new Promise<void>((resolve, reject) => {
let timeoutFired = false;
const timeout = setTimeout(() => {
@@ -78,7 +78,7 @@ class SimplifiedExtension {
debugLog('Socket protocol:', socket.protocol);
reject(new Error('Connection timeout'));
}, 5000);
socket.onopen = () => {
if (timeoutFired) {
debugLog('WebSocket opened but timeout already fired!');
@@ -88,7 +88,7 @@ class SimplifiedExtension {
clearTimeout(timeout);
resolve();
};
socket.onerror = (error) => {
debugLog('WebSocket onerror during connection:', error);
debugLog('Error type:', error.type);
@@ -98,7 +98,7 @@ class SimplifiedExtension {
reject(new Error('WebSocket connection failed'));
}
};
socket.onclose = (event) => {
debugLog('WebSocket onclose during connection setup:', {
code: event.code,
@@ -111,7 +111,7 @@ class SimplifiedExtension {
reject(new Error(`WebSocket closed: ${event.reason || event.code}`));
}
};
debugLog('Event handlers set, waiting for connection...');
});
@@ -146,7 +146,7 @@ class SimplifiedExtension {
const targetInfo = await this._connection.attachTab(tabId);
debugLog('attachTab completed, storing in connectedTabs map');
this._connectedTabs.set(tabId, targetInfo.targetId);
await this._updateIcon(tabId, 'connected');
debugLog(`=== Successfully connected to tab ${tabId} ===`);
} catch (error: any) {
@@ -170,19 +170,19 @@ class SimplifiedExtension {
private async _disconnectTab(tabId: number): Promise<void> {
debugLog(`=== Disconnecting tab ${tabId} ===`);
if (!this._connectedTabs.has(tabId)) {
debugLog('Tab not in connectedTabs map, ignoring disconnect');
return;
}
debugLog('Calling detachTab on connection');
this._connection?.detachTab(tabId);
this._connectedTabs.delete(tabId);
debugLog('Tab removed from connectedTabs map');
await this._updateIcon(tabId, 'disconnected');
debugLog('Connected tabs remaining:', this._connectedTabs.size);
if (this._connectedTabs.size === 0 && this._connection) {
debugLog('No tabs remaining, closing relay connection');
@@ -247,7 +247,7 @@ class SimplifiedExtension {
private _onTabRemoved = async (tabId: number): Promise<void> => {
debugLog('Tab removed event for tab:', tabId, 'is connected:', this._connectedTabs.has(tabId));
if (!this._connectedTabs.has(tabId)) return;
debugLog(`Connected tab ${tabId} was closed, disconnecting`);
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() {
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();
console.log(`Found ${contexts.length} browser context(s)`);
const contexts = browser.contexts()
console.log(`Found ${contexts.length} browser context(s)`)
// Sleep 200 ms
await new Promise(resolve => setTimeout(resolve, 200));
await new Promise((resolve) => setTimeout(resolve, 200))
for (const context of contexts) {
const pages = context.pages();
console.log(`Context has ${pages.length} page(s):`);
const pages = context.pages()
console.log(`Context has ${pages.length} page(s):`)
for (const page of pages) {
const url = page.url();
console.log(`\nPage URL: ${url}`);
const url = page.url()
console.log(`\nPage URL: ${url}`)
const html = await page.content();
const lines = html.split('\n').slice(0, 3);
console.log('First 3 lines of HTML:');
const html = await page.content()
const lines = html.split('\n').slice(0, 3)
console.log('First 3 lines of HTML:')
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
page.on('console', msg => {
console.log(`Browser log: [${msg.type()}] ${msg.text()}`);
});
page.on('console', (msg) => {
console.log(`Browser log: [${msg.type()}] ${msg.text()}`)
})
console.log(`running eval`)
// Evaluate a sum in the browser and log something from inside the browser context
const sumResult = await page.evaluate(() => {
console.log('Logging from inside browser context!');
return 1 + 2 + 3;
});
console.log(`Sum result evaluated in browser: ${sumResult}`);
console.log('Logging from inside browser context!')
return 1 + 2 + 3
})
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()