Skip to content

Commit 519ccc6

Browse files
committed
add scripts
1 parent dd5d21d commit 519ccc6

1 file changed

Lines changed: 57 additions & 50 deletions

File tree

scripts/capture-logs.js

Lines changed: 57 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
11
const CDP = require('chrome-remote-interface');
22

3-
async function attachToTarget(targetId) {
4-
try {
5-
const client = await CDP({port: 9222, target: targetId});
6-
const {Log, Runtime} = client;
7-
8-
await Log.enable();
9-
await Runtime.enable();
10-
11-
Log.entryAdded((params) => {
12-
const {entry} = params;
13-
console.log(`[WORKER:LOG:${entry.level}] ${entry.text}`);
14-
});
15-
16-
Runtime.consoleAPICalled((params) => {
17-
const args = params.args.map(arg => arg.value || arg.description || '').join(' ');
18-
console.log(`[WORKER:CONSOLE:${params.type}] ${args}`);
19-
});
20-
21-
console.error(`CDP: Attached to worker ${targetId}`);
22-
} catch (err) {
23-
console.error(`CDP: Failed to attach to worker ${targetId}: ${err.message}`);
24-
}
3+
async function setupLogging(client, label) {
4+
const {Log, Runtime} = client;
5+
6+
await Log.enable();
7+
await Runtime.enable();
8+
9+
Log.entryAdded((params) => {
10+
const {entry} = params;
11+
console.log(`[${label}:LOG:${entry.level}] ${entry.text}`);
12+
});
13+
14+
Runtime.consoleAPICalled((params) => {
15+
const args = params.args.map(arg => arg.value || arg.description || '').join(' ');
16+
console.log(`[${label}:CONSOLE:${params.type}] ${args}`);
17+
});
18+
19+
// Also catch exceptions
20+
Runtime.exceptionThrown((params) => {
21+
console.log(`[${label}:EXCEPTION] ${JSON.stringify(params.exceptionDetails)}`);
22+
});
23+
24+
console.error(`CDP: Logging enabled for ${label}`);
2525
}
2626

2727
async function captureLogs() {
2828
// Retry connection forever - CI timeout will protect us
29-
let client;
29+
let browser;
3030
let attempt = 0;
31-
while (!client) {
31+
while (!browser) {
3232
try {
33-
client = await CDP({port: 9222});
33+
browser = await CDP({port: 9222});
3434
} catch (err) {
3535
attempt++;
3636
if (attempt % 10 === 0) {
@@ -40,43 +40,50 @@ async function captureLogs() {
4040
}
4141
}
4242

43-
const {Log, Runtime, Target} = client;
43+
// Set up logging on browser-level connection too
44+
await setupLogging(browser, 'browser');
4445

45-
// Enable logging on main page
46-
await Log.enable();
47-
await Runtime.enable();
46+
const {Target} = browser;
4847

49-
Log.entryAdded((params) => {
50-
const {entry} = params;
51-
console.log(`[LOG:${entry.level}] ${entry.text}`);
52-
});
48+
// Discover all targets
49+
await Target.setDiscoverTargets({discover: true});
5350

54-
Runtime.consoleAPICalled((params) => {
55-
const args = params.args.map(arg => arg.value || arg.description || '').join(' ');
56-
console.log(`[CONSOLE:${params.type}] ${args}`);
57-
});
51+
// Attach to each page target we find
52+
const attachToPage = async (targetId, targetType) => {
53+
try {
54+
// Create a new CDP session for this target
55+
const {sessionId} = await Target.attachToTarget({targetId, flatten: true});
56+
console.error(`CDP: Attached to ${targetType} ${targetId} (session: ${sessionId})`);
5857

59-
// Listen for new targets (workers)
60-
await Target.setDiscoverTargets({discover: true});
58+
// Connect to this specific target
59+
const client = await CDP({port: 9222, target: targetId});
60+
await setupLogging(client, targetType);
61+
} catch (err) {
62+
console.error(`CDP: Failed to attach to ${targetId}: ${err.message}`);
63+
}
64+
};
6165

62-
Target.targetCreated(async (params) => {
63-
const {targetInfo} = params;
64-
console.error(`CDP: New target: ${targetInfo.type} - ${targetInfo.targetId}`);
65-
if (targetInfo.type === 'worker' || targetInfo.type === 'service_worker' || targetInfo.type === 'shared_worker') {
66-
await attachToTarget(targetInfo.targetId);
66+
// Listen for new targets
67+
Target.targetCreated(async ({targetInfo}) => {
68+
console.error(`CDP: New target: ${targetInfo.type} - ${targetInfo.url || targetInfo.targetId}`);
69+
if (targetInfo.type === 'page' || targetInfo.type === 'worker' || targetInfo.type === 'iframe') {
70+
await attachToPage(targetInfo.targetId, targetInfo.type);
6771
}
6872
});
6973

70-
// Also check existing targets
74+
// Attach to existing targets
7175
const {targetInfos} = await Target.getTargets();
7276
for (const info of targetInfos) {
73-
console.error(`CDP: Existing target: ${info.type} - ${info.targetId}`);
74-
if (info.type === 'worker' || info.type === 'service_worker' || info.type === 'shared_worker') {
75-
await attachToTarget(info.targetId);
77+
console.error(`CDP: Existing target: ${info.type} - ${info.url || info.targetId}`);
78+
if (info.type === 'page' || info.type === 'worker' || info.type === 'iframe') {
79+
await attachToPage(info.targetId, info.type);
7680
}
7781
}
7882

79-
console.error('CDP: Connected and listening for logs (including workers)...');
83+
console.error('CDP: Setup complete, listening for all targets...');
8084
}
8185

82-
captureLogs();
86+
captureLogs().catch(err => {
87+
console.error('CDP: Fatal error:', err);
88+
process.exit(1);
89+
});

0 commit comments

Comments
 (0)