d-_-b
d-_-b

Reputation: 23161

chrome.scripting.executeScript fails to run on preexisting tabs with `tabs` and `<all_urls>` host permission

I have a chrome MV3 extension that has the following manifest.json:

{
  "manifest_version": 3,
  "permissions": [
    "scripting",
    "tabs"
  ],
  "host_permissions": ["<all_urls>"]
}

When I install the extension and have existing tabs, I am unable to execute scripts on those existing tabs:

chrome.scripting.executeScript({
  target: {tabId: 1305686273}, 
  func: function(){ console.log('hi'); }
});

I get this error in my background service worker script:

Cannot access contents of the page. Extension manifest must request permission to access the respective host.

My understanding is that the tabs permission lets me query all tabs without user gesture, and the "host_permissions": ["<all_urls>"] should let me execute scripts on all tabs without user gesture.

Am I wrong, or is there something missing to allow scripting on pages without user gesture?

P.S. running chrome.tabs.get(1305686273).then(console.log) works as expected.

Upvotes: 3

Views: 2544

Answers (3)

d-_-b
d-_-b

Reputation: 23161

chrome.scripting seems to throw this error for tabs that existed prior to installing the extension.

Cannot access contents of the page. Extension manifest must request permission to access the respective host.

Scripting works as expected for any newly created tab, regardless of if the user has clicked the extension.

I've found a workaround is to call chrome.tabs.reload(tabId) on those preexisting tabs, and then chrome.scripting will work as expected.

Going to raise a bug with the chrome team.

Upvotes: 6

Norio Yamamoto
Norio Yamamoto

Reputation: 1786

I've tested it and can't reproduce it. Please post the extensions you are using.

  1. Open tab1 and tab2 without installing extensions. enter image description here

  2. Install extensions. enter image description here

manifest.json

{
  "manifest_version": 3,
  "name": "hoge",
  "version": "1.0",
  "permissions": [
    "scripting"
  ],
  "host_permissions": [
    "<all_urls>"
  ],
  "background": {
    "service_worker": "background.js"
  }
}

background.js

chrome.tabs.query({ url: "https://*/*" }, (tabs) => {
  for (const { id } of tabs) {
    console.log("tabId=" + id);
    chrome.scripting.executeScript({
      target: { tabId: id },
      func: function () { console.log('hi'); }
    });
  }
});

Upvotes: 2

Thomas Mueller
Thomas Mueller

Reputation: 678

Which Chrome version are you using?

I can't reproduce your problem on Manjaro Linux, with Chromium Version 107.0.5304.87 (Official Build) Arch Linux (64-Bit).

  1. Start Chrome
  2. Open Manjaro.org and CNN.com and Stackoverflow.com
  3. Open chrome://extensions/
  4. Disable all extensions
  5. Load the the MCVE below
  6. Open the consoles for all tabs, except chrome://extensions/
  7. Every console shows the text "hi"

manifest.json

{
    "name": "executeScript",
    "version": "1.0.0",
    "manifest_version": 3,
    "background": {
        "service_worker": "background.js"
    },  
    "host_permissions": ["<all_urls>"],
    "permissions": [
        "scripting",
        "tabs"
    ]
}

background.js

(async () => {
    let tabs = await chrome.tabs.query({});
    console.log(tabs);
    tabs.forEach(tab => {
        if (!tab.url.startsWith("chrome://")) {
            console.log(tab.url, "1");
            chrome.scripting.executeScript({
                target: {tabId: tab.id}, 
                func: function() { console.log('hi'); }
            }).catch(error => {
                console.log(`executeScript failed on ${tab.id} ${tab.url}`, error);
            }); 
            console.log(tab.url, "2");
        }
    });
})();

Upvotes: -1

Related Questions