chrome-devtools-mcp-for-extension 0.9.2 → 0.9.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// src/tools/iframe-popup-tools.ts
|
|
2
2
|
// Tools for inspecting & editing in-page iframe popups via CDP.
|
|
3
3
|
// These tools enable direct access to iframe-embedded extension popups.
|
|
4
|
+
// Uses OOPIF (Out-Of-Process iFrame) detection via Target.setAutoAttach.
|
|
4
5
|
import fs from 'node:fs/promises';
|
|
5
6
|
import path from 'node:path';
|
|
6
7
|
export async function findExtensionIdViaTargets(cdp) {
|
|
@@ -10,58 +11,135 @@ export async function findExtensionIdViaTargets(cdp) {
|
|
|
10
11
|
throw new Error('Extension service worker not found');
|
|
11
12
|
return new URL(ext.url).host;
|
|
12
13
|
}
|
|
13
|
-
export async function
|
|
14
|
-
await cdp.send('
|
|
15
|
-
await cdp.send('
|
|
16
|
-
|
|
17
|
-
|
|
14
|
+
export async function enableOopifAutoAttach(cdp) {
|
|
15
|
+
await cdp.send('Target.setDiscoverTargets', { discover: true });
|
|
16
|
+
await cdp.send('Target.setAutoAttach', {
|
|
17
|
+
autoAttach: true,
|
|
18
|
+
waitForDebuggerOnStart: false,
|
|
19
|
+
flatten: true, // Essential for OOPIF detection
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
export async function waitForExtensionChildTarget(cdp, pattern, timeoutMs = 8000) {
|
|
18
23
|
const start = Date.now();
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
const
|
|
23
|
-
if (
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
24
|
+
return await new Promise((resolve, reject) => {
|
|
25
|
+
let resolved = false;
|
|
26
|
+
const onAttach = (e) => {
|
|
27
|
+
const url = e?.targetInfo?.url || '';
|
|
28
|
+
if (pattern.test(url)) {
|
|
29
|
+
resolved = true;
|
|
30
|
+
cleanup();
|
|
31
|
+
resolve({
|
|
32
|
+
sessionId: e.sessionId,
|
|
33
|
+
targetId: e.targetInfo.targetId,
|
|
34
|
+
url,
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
const onTimeout = () => {
|
|
39
|
+
if (!resolved) {
|
|
40
|
+
cleanup();
|
|
41
|
+
reject(new Error(`Timeout: extension popup child target not found (pattern: ${pattern})`));
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
const cleanup = () => {
|
|
45
|
+
cdp.off('Target.attachedToTarget', onAttach);
|
|
46
|
+
};
|
|
47
|
+
cdp.on('Target.attachedToTarget', onAttach);
|
|
48
|
+
setTimeout(onTimeout, Math.max(0, timeoutMs - (Date.now() - start)));
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
export async function inspectIframe(cdp, urlPattern, waitMs = 8000) {
|
|
52
|
+
// Try OOPIF detection first
|
|
53
|
+
try {
|
|
54
|
+
await enableOopifAutoAttach(cdp);
|
|
55
|
+
const child = await waitForExtensionChildTarget(cdp, urlPattern, waitMs);
|
|
56
|
+
// Enable Page/Runtime in child session
|
|
57
|
+
await sendToChildSession(cdp, child.sessionId, 'Page.enable', {});
|
|
58
|
+
await sendToChildSession(cdp, child.sessionId, 'Runtime.enable', {});
|
|
59
|
+
// Evaluate outerHTML in child session
|
|
60
|
+
const htmlResult = await sendToChildSession(cdp, child.sessionId, 'Runtime.evaluate', {
|
|
61
|
+
expression: 'document.documentElement.outerHTML',
|
|
62
|
+
returnByValue: true,
|
|
63
|
+
});
|
|
64
|
+
const html = String(htmlResult?.result?.value ?? '');
|
|
65
|
+
return {
|
|
66
|
+
frameId: child.targetId,
|
|
67
|
+
frameUrl: child.url,
|
|
68
|
+
html,
|
|
69
|
+
};
|
|
31
70
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
71
|
+
catch (oopifError) {
|
|
72
|
+
// Fallback: Try regular iframe via Page.getFrameTree
|
|
73
|
+
await cdp.send('Page.enable');
|
|
74
|
+
const { frameTree } = await cdp.send('Page.getFrameTree');
|
|
75
|
+
const findFrame = (node) => {
|
|
76
|
+
if (urlPattern.test(node.frame.url)) {
|
|
77
|
+
return node.frame;
|
|
78
|
+
}
|
|
79
|
+
if (node.childFrames) {
|
|
80
|
+
for (const child of node.childFrames) {
|
|
81
|
+
const found = findFrame(child);
|
|
82
|
+
if (found)
|
|
83
|
+
return found;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return null;
|
|
87
|
+
};
|
|
88
|
+
const frame = findFrame(frameTree);
|
|
89
|
+
if (!frame) {
|
|
90
|
+
throw new Error(`Iframe not found (tried both OOPIF and regular iframe): ${urlPattern}`);
|
|
36
91
|
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
92
|
+
// Execute in the frame context
|
|
93
|
+
await cdp.send('Runtime.enable');
|
|
94
|
+
const htmlResult = await cdp.send('Runtime.evaluate', {
|
|
95
|
+
expression: 'document.documentElement.outerHTML',
|
|
96
|
+
returnByValue: true,
|
|
97
|
+
contextId: frame.id,
|
|
98
|
+
});
|
|
99
|
+
const html = String(htmlResult?.result?.value ?? '');
|
|
100
|
+
return {
|
|
101
|
+
frameId: frame.id,
|
|
102
|
+
frameUrl: frame.url,
|
|
103
|
+
html,
|
|
104
|
+
};
|
|
43
105
|
}
|
|
44
106
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
107
|
+
async function sendToChildSession(cdp, sessionId, method, params) {
|
|
108
|
+
const id = Math.floor(Math.random() * 1000000);
|
|
109
|
+
const message = JSON.stringify({ id, method, params });
|
|
110
|
+
// Send message to child target
|
|
111
|
+
await cdp.send('Target.sendMessageToTarget', { sessionId, message });
|
|
112
|
+
// Wait for response from child target
|
|
113
|
+
return await new Promise((resolve, reject) => {
|
|
114
|
+
const timeout = setTimeout(() => {
|
|
115
|
+
cleanup();
|
|
116
|
+
reject(new Error(`Timeout waiting for response from child session: ${method}`));
|
|
117
|
+
}, 5000);
|
|
118
|
+
const onMessage = (e) => {
|
|
119
|
+
if (e.sessionId === sessionId) {
|
|
120
|
+
try {
|
|
121
|
+
const response = JSON.parse(e.message);
|
|
122
|
+
if (response.id === id) {
|
|
123
|
+
cleanup();
|
|
124
|
+
if (response.error) {
|
|
125
|
+
reject(new Error(`CDP error: ${JSON.stringify(response.error)}`));
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
resolve(response.result);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
catch (err) {
|
|
133
|
+
// Ignore parse errors for other messages
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
const cleanup = () => {
|
|
138
|
+
clearTimeout(timeout);
|
|
139
|
+
cdp.off('Target.receivedMessageFromTarget', onMessage);
|
|
140
|
+
};
|
|
141
|
+
cdp.on('Target.receivedMessageFromTarget', onMessage);
|
|
59
142
|
});
|
|
60
|
-
return {
|
|
61
|
-
frameId,
|
|
62
|
-
frameUrl,
|
|
63
|
-
html: String(result.value ?? ''),
|
|
64
|
-
};
|
|
65
143
|
}
|
|
66
144
|
export async function patchAndReload(cdp, extensionPath, patches) {
|
|
67
145
|
for (const p of patches) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "chrome-devtools-mcp-for-extension",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.4",
|
|
4
4
|
"description": "MCP server for Chrome extension development with Web Store automation. Fork of chrome-devtools-mcp with extension-specific tools.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": "./build/src/index.js",
|