In what scenarios does executeScript + activeTab work?

348 views
Skip to first unread message

Angus Zhu

unread,
May 26, 2022, 5:11:14 AM5/26/22
to Chromium Extensions

In what scenarios does executeScript + activeTab work?
I’ve been trying to replace <all_urls>  with activeTab    to execute scripts in pages:


//// manifest.json ////
{
    "name": "My extension",
    "manifest_version": 3,
    "permissions": [ "activeTab", "scripting"],
    "background": {
        "service_worker": "background.js"
    }
}


// background.js
chrome.tabs.onActivated.addListener(activeInfo => {
    const { tabId } = activeInfo;
    chrome.scripting.executeScript({
        target: { tabId, allFrames: true },
        files: ['content.js']
      });
});



However, this causes error:

Uncaught (in promise) Error: Cannot access contents of the page. Extension manifest must request permission to access the respective host.


But executeScript works in chrome.action.onClicked.addListener().

So I am wondering does this only work in certain scenarios or am I doing something wrong?

Juraj M.

unread,
May 26, 2022, 5:40:49 AM5/26/22
to Chromium Extensions, sweet...@gmail.com
User needs to invoke (interact with) your extension first, for example clicking the action button, using context menu item or pressing assigned command keyboard shortcut. Then you get host permission for the opened page where the user action happened.
More info:
Reply all
Reply to author
Forward
0 new messages