chrome-devtools-mcp-for-extension 0.9.2 → 0.9.3

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,59 +11,100 @@ 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 waitForFrameByUrlMatch(cdp, pattern, timeoutMs = 5000) {
14
- await cdp.send('Page.enable');
15
- await cdp.send('Runtime.enable');
16
- await cdp.send('DOM.enable');
17
- // Strategy: Use Page.getFrameTree and match by pattern
18
- const start = Date.now();
19
- while (Date.now() - start < timeoutMs) {
20
- try {
21
- const tree = await cdp.send('Page.getFrameTree');
22
- const hit = findFrameByPattern(tree.frameTree, pattern);
23
- if (hit)
24
- return hit;
25
- }
26
- catch (e) {
27
- // Frame tree may not be ready yet, continue waiting
28
- }
29
- // Wait a bit before retry
30
- await new Promise(resolve => setTimeout(resolve, 100));
31
- }
32
- throw new Error(`Timeout waiting for iframe by url match: ${pattern}`);
33
- function findFrameByPattern(node, rx) {
34
- if (node?.frame?.url && rx.test(node.frame.url)) {
35
- return { frameId: node.frame.id, frameUrl: node.frame.url };
36
- }
37
- for (const c of node.childFrames ?? []) {
38
- const r = findFrameByPattern(c, rx);
39
- if (r)
40
- return r;
41
- }
42
- return null;
43
- }
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
+ });
44
21
  }
45
- export async function inspectIframe(cdp, urlPattern, waitMs = 5000) {
46
- await cdp.send('Runtime.enable');
47
- await cdp.send('DOM.enable');
48
- await cdp.send('Log.enable');
49
- const { frameId, frameUrl } = await waitForFrameByUrlMatch(cdp, urlPattern, waitMs);
50
- const { executionContextId } = await cdp.send('Page.createIsolatedWorld', {
51
- frameId,
52
- worldName: 'mcp',
53
- // grantUniveralAccess is a known CDP option in some builds. It's optional here.
22
+ export async function waitForExtensionChildTarget(cdp, pattern, timeoutMs = 8000) {
23
+ const start = Date.now();
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)));
54
49
  });
55
- const { result } = await cdp.send('Runtime.evaluate', {
56
- contextId: executionContextId,
50
+ }
51
+ export async function inspectIframe(cdp, urlPattern, waitMs = 8000) {
52
+ // Enable OOPIF auto-attach
53
+ await enableOopifAutoAttach(cdp);
54
+ // Wait for child target (extension iframe)
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', {
57
61
  expression: 'document.documentElement.outerHTML',
58
62
  returnByValue: true,
59
63
  });
64
+ const html = String(htmlResult?.result?.value ?? '');
60
65
  return {
61
- frameId,
62
- frameUrl,
63
- html: String(result.value ?? ''),
66
+ frameId: child.targetId, // Use targetId as frameId for OOPIF
67
+ frameUrl: child.url,
68
+ html,
64
69
  };
65
70
  }
71
+ async function sendToChildSession(cdp, sessionId, method, params) {
72
+ const id = Math.floor(Math.random() * 1000000);
73
+ const message = JSON.stringify({ id, method, params });
74
+ // Send message to child target
75
+ await cdp.send('Target.sendMessageToTarget', { sessionId, message });
76
+ // Wait for response from child target
77
+ return await new Promise((resolve, reject) => {
78
+ const timeout = setTimeout(() => {
79
+ cleanup();
80
+ reject(new Error(`Timeout waiting for response from child session: ${method}`));
81
+ }, 5000);
82
+ const onMessage = (e) => {
83
+ if (e.sessionId === sessionId) {
84
+ try {
85
+ const response = JSON.parse(e.message);
86
+ if (response.id === id) {
87
+ cleanup();
88
+ if (response.error) {
89
+ reject(new Error(`CDP error: ${JSON.stringify(response.error)}`));
90
+ }
91
+ else {
92
+ resolve(response.result);
93
+ }
94
+ }
95
+ }
96
+ catch (err) {
97
+ // Ignore parse errors for other messages
98
+ }
99
+ }
100
+ };
101
+ const cleanup = () => {
102
+ clearTimeout(timeout);
103
+ cdp.off('Target.receivedMessageFromTarget', onMessage);
104
+ };
105
+ cdp.on('Target.receivedMessageFromTarget', onMessage);
106
+ });
107
+ }
66
108
  export async function patchAndReload(cdp, extensionPath, patches) {
67
109
  for (const p of patches) {
68
110
  const abs = path.join(extensionPath, p.file);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chrome-devtools-mcp-for-extension",
3
- "version": "0.9.2",
3
+ "version": "0.9.3",
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",