Reputation: 23161
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
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
Reputation: 1786
I've tested it and can't reproduce it. Please post the extensions you are using.
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
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).
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