Expected behaviour
For injected content script to run on specific pages that I set within my manifest.json
Current behaviour
Contentscript is injected on every page. Or atleast I seem to see errors in console for background indicating background.js:1 Uncaught (in promise) Error: Cannot access contents of url... "
. Maybe it’s working but I don’t want to see these errors!
Manifest.json
– I also tried host_permission
as I understand this is relevant permission for programmatic injection
{
"content_scripts": [
{
"matches": [
"https://example-01.au/*",
"https://example-03.com/*",
"https://example-02.eu/*"
],
"js": ["contentscript.js"]
}
],
"permissions": [
"tabs",
"contextMenus",
"offscreen",
"clipboardWrite",
"tabGroups",
"storage",
"scripting",
"activeTab"
],
"background": {
"service_worker": "background.js"
},
"host_permissions": [
"https://example-01.au/*",
"https://example-03.com/*",
"https://example-02.eu/*"
]
}
Background.js
chrome.tabs.onActivated.addListener((info) => {
chrome.tabs.get(info.tabId, (tab) => {
if (tab.url?.startsWith('chrome://')) return undefined;
chrome.scripting
.executeScript({
target: { tabId: info.tabId },
files: ['contentScript.js']
})
.then(() => {
console.log('Scripted Injected');
});
});
});
It’s not clear from the docs. But, do ineed to programmatically set up page matches within my background.js
?