Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

debugger.sendCommand broken on Windows but not on Mac?

113 views
Skip to first unread message

Adnan Khan

unread,
Jul 19, 2024, 4:18:37 PM7/19/24
to Chromium Extensions
Hello all, I'm facing pretty much strange issue. Few months back I pushed update to my extension that would use below code to generate PDF out of HTML. It used to work fine for all operating system but from last 1 week, customers are complaining that its not working.

I use Windows machine, for me the below is broken and it throws error
```Error: Cannot access a chrome-extension:// URL of different extension```

interestingly, same flow works on Mac(80 percent of time). 

Now, it simply just sends back Base64 but it can not even attach debugger on Windows.

Site, I'm trying to do is credentials protected but it looks like this
```https://dealer.cudl.com/CUDL#/DealQueue/Applications```
Can it be because of the URL has `#`? If yes then why it works for Mac and not on Windows.

If I try to execute the code immediately like maybe in 1 second, it works and then breaks midway of saying ```debugged detached```. 


Code:
```

chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
  if (message.action === "capturePDF") {
    console.log("Message recieved for Capture PDF: ", message)
    const tabId = sender.tab.id;
    console.log("Tab ID: ", tabId, "Sender- :", sender)
    chrome.debugger.attach({ tabId: tabId }, "1.2", function () {
      chrome.debugger.sendCommand(
        { tabId: tabId },
        "Page.printToPDF",
        {},
        function (pdfResult) {
          console.log("PDF Result: ", pdfResult)
          chrome.debugger.detach({ tabId: tabId });

          if (chrome.runtime.lastError) {
            sendResponse({ error: chrome.runtime.lastError });
          } else {
            sendResponse({ data: pdfResult.data });
          }
        }
      );
    });

    return true; // Keeps the message channel open for the async response
  }
});
```

woxxom

unread,
Jul 20, 2024, 12:42:31 AM7/20/24
to Chromium Extensions, Adnan Khan
Judging by the error message there's an iframe of another extension in the tab, which prevents the use of chrome.debugger API: https://crbug.com/40753571

Adnan Khan

unread,
Jul 20, 2024, 5:07:43 AM7/20/24
to Chromium Extensions, woxxom, Adnan Khan
There was just one empty iframe on the page which I removed manually just before the debugger process but same error still.
`Cannot access a chrome-extension:// URL of different extension`

woxxom

unread,
Jul 20, 2024, 5:20:26 AM7/20/24
to Chromium Extensions, Adnan Khan, woxxom
Assuming the tab is the built-in PDF viewer, there's a hidden iframe for a built-in chrome extension that shows the PDF. You won't see it by default in devtools unless you restart chrome with --show-component-extension-options command line.

To remove it you'll have to remove the entire PDF viewer:

if (!document.body.removeChild(document.querySelector('body > embed'))) {
  document.body.replaceWith(document.body.cloneNode(true));
}

Adnan Khan

unread,
Jul 20, 2024, 11:45:51 AM7/20/24
to Chromium Extensions, woxxom, Adnan Khan
It wasn't built in PDF but I disabled 2 other extensions (VPN and LastPass.) and somehow it worked this time.

Now what should be the best work around for this? So far, I will ask customers not to use other extension but I really am looking to fix this issue through some other alternative?

woxxom

unread,
Jul 20, 2024, 12:02:05 PM7/20/24
to Chromium Extensions, Adnan Khan, woxxom
The only workaround is to use a content script to remove all iframes.

You'll need to check all elements in the document and inside closed shadow roots. Note that in addition to the iframes with `src` attribute pointing to another extension, you'll need to check iframes without any `src` attribute or various same-origin `src` like about:blank, javascript:... because another extension may have navigated the iframe internally without changing the src attribute. Something like this:

removeIframes(document);

function removeIframes(root) {
  for (let el of root.querySelectorAll('*')) {
    const wnd = el.contentWindow;
    if (wnd && (
      /^($|chrome-extension|about|javascript|blob)/.test(el.src)
      && !Object.getOwnPropertyDescriptor(wnd.location, 'href').get
    )) {
      el.remove();
    } else if ((el = el.shadowRoot || chrome.dom.openOrClosedShadowRoot(el))) {
      removeIframes(el);
    }
  }
}

Reply all
Reply to author
Forward
0 new messages