devtools.inspectedWindow in the context of MV3 extension's service worker

174 views
Skip to first unread message

ka...@couponfollow.com

unread,
Aug 2, 2022, 10:47:19 AM8/2/22
to Chromium Extensions
I'm trying to create devtools extension that would add a new panel to devtools window opened in the context of an MV3 extension's service worker. 

So when you go to chrome://extensions, click the Service Worker link of your extension, open DevTools, I'd like to add a new panel and display some data from the inspected extension. For example, it's storage or manifest, or whatever.

The goal is to have a generic extension, so I don't want to communicate directly between extensions, I want to open my devtools extension in the context of any installed extension.

I started with the following manifest:

{
  "manifest_version": 3,
  ...
  "permissions": ["<all_urls>", "scripting", "tabs"],
  "devtools_page": "devtools.html",
  "background": {
    "service_worker": "background.js"
  },
}

end the devtools.html simply creates a panel, which runs the following script:

function evalInWindow(fn) {
  chrome.devtools.inspectedWindow.eval(
    "(" + fn.toString() + ")(chrome)",
    (value, isError) => {
      if (isError) {
        console.error(value);
      } else {
        console.log(value);
      }
    }
  );
}

evalInWindow(function (chrome) {
  console.log(chrome.runtime.getManifest()); // I expect the manifest of the inspected extension being logged here
  return "my string";
});

This doesn't work against MV3 extension, but works perfectly against MV2 extensions.
For MV3-based ones I get "Extension server error: Object not found: <top>" error.

I tried to execute a script:

chrome.scripting.executeScript(
  {
    func: () => {
      console.log("Injected script");
      return "injected script";
    },
    target: {
      tabId: -1, // no idea what ID to use here...
    },
  },
  function (result) {
    console.log(result);
  }
);

Doesn't work either. The TabId is the issue probably, I tried -1, 0, 1 etc but I get "Unchecked runtime.lastError: No tab with id: -1" and "evaluate: the main frame is not yet available".

Is there an API or any other way to create such an extension?

Best,
Kamil

wOxxOm

unread,
Aug 3, 2022, 10:30:02 AM8/3/22
to Chromium Extensions, ka...@couponfollow.com
This API doesn't support workers.
Reply all
Reply to author
Forward
0 new messages