Skip to content

[Bug]: Debugger not hit/paused in DevTools when using CDP and dynamically creating IFrame element #35478

Closed

Description

@pmadhur

Preflight Checklist

Electron Version

20.1.0

What operating system are you using?

Windows

Operating System Version

Windows 11 Pro, Version: 21H2, Build: 22000.856

What arch are you using?

x64

Last Known Working Electron version

No response

Expected Behavior

Debugger should be hit/paused in DevTools even when using webContents.debugger.attach() and creating iframe element on the fly.

Actual Behavior

Debugger not hit/paused in DevTools.

Testcase Gist URL

https://gist.github.com/46ecbbc25cef72196f06fbc852ab397a

Additional Information

After running the test case gist in Electron Fiddle, click the "Create IFrame" button. You would observe that debugger is not hit/paused in DevTools. Now comment line 19 in main.js, run the test case again, click the "Create IFrame" button, you would observe that the debugger is hit/paused in DevTools.

Activity

  1. pmadhur commented on Aug 28, 2022

    @pmadhur
    Author

    Seems to be similar to #34938, not sure if it exactly the same problem. My problem is with Debugger domain, and #34938 seems to be with Page domain.

  2. pmadhur commented on Aug 31, 2022

    @pmadhur
    Author

    I tested the same with Chrome using chrome-remote-interface. Chrome works as expected. Here the same test case (Chrome Version: 104.0.5112.102, Node Version: 16.15.0) with Chrome:

    Create a test directory, c:\test, open command prompt in this directory and execute:

    npm install chrome-remote-interface

    Create file, c:\test\test.js with following contents:

    const CDP = require('chrome-remote-interface');
    
    async function start() {
        let client;
        try {
            client = await CDP();
            const {Debugger, Page} = client;
            Debugger.on('paused', () => {
                console.log('debugger paused!');
            });
            Debugger.on('resumed', () => {
                console.log('debugger resumed!');
            });
    
            await Debugger.enable();
            await Page.enable();
            await Page.navigate({url: 'file:///c:/test/test.html'});
        } catch (err) {
            console.error(err);
        }
    }
    
    start();
    

    Create file c:\test\test.html with following contents:

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8">
        <title>Hello World!</title>
    </head>
    <body>
        <button id="btnCreate">Create IFrame</button>
        <script>
            const btnCreate = document.getElementById('btnCreate');
            btnCreate.addEventListener('click', () => {
                const iframeEl = document.createElement('iframe');
                iframeEl.style.width = '500px';
                iframeEl.style.height = '500px';
                document.body.appendChild(iframeEl);
                debugger;
            });
        </script>
    </body>
    </html>
    

    Start Chrome with following arguments:

    chrome.exe --remote-debugging-port=9222 --user-data-dir=c:/test/remote-profile

    Open DevTools in Chrome, open command prompt and navigate to c:\test and execute:

    node test.js

    In Chrome, click the "Create IFrame" button, you would observe that the debugger is hit/paused, and in the console you see the CDP events are also triggered.

    So, it seems to be a problem with Electron.

  3. moved this to Unsorted Issues in 21-x-yon Sep 7, 2022
  4. moved this from Unsorted Issues to Does Not Block Stable in 21-x-yon Sep 7, 2022
  5. pmadhur commented on Oct 2, 2022

    @pmadhur
    Author

    Closing as there are other issues (#34260 and #34938) which have the same problem with iframe. I could do a workaround by remotely attaching debugger via Chrome DevTools Protocol.

  6. MrHuangJser commented on Mar 17, 2023

    @MrHuangJser

    i have same issue, but i passed the probrolem with another way. here is the simple code

    // create a invisible BrowserWindow, it's not the main window
    const debuggerWin = new BrowserWindow();
    const proxyDebugger = debuggerWin.webContents.debugger;
    proxyDebugger.attach();
    // use debuggerWin to proxy all webContents events and commands
    proxyDebugger.sendCommand("Target.setDiscoverTargets", { discover: true });
    let targetContentsId: string;
    proxyDebugger.on("message", (...[method, params]) => {
      if (method === "Target.targetCreated") {
        const {
          targetInfo: { targetId },
        } = params;
        targetContentsId = targetId
        proxyDebugger.sendCommand("Target.attachToTarget", { targetId, flatten: true })
          .then(({ sessionId }) => {
            // then you can send any command to the target or listen messages from target
            // eg: proxyDebugger.sendCommand("Emulation.setTouchEmulationEnabled", { enabled: true }, sessionId);
          });
      }
    
      // listen messages from target
      if (method === 'Target.receivedMessageFromTarget') {
        if (targetContentsId === params.targetId) {
          // do something
        }
      }
    })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions