I am unable to inject script to the locally opened web page due to the permission error. It's strange because I have requested already permission for "<all_urls">.
This is small extension with service worker that will open page, injest script to this page and interract with it.
Here is my manifest.json:
{
"name": "Test",
"version": "0.1",
"incognito": "split",
"manifest_version": 3,
"background": {
"service_worker": "test.js",
"type": "module"
},
"permissions": [
"activeTab",
"scripting",
"tabs"
],
"host_permissions":
[
"*://*/*",
"<all_urls>"
]
}
Here is my service worker script test.js:
"use strict";
async function Startup() {
const tab = await chrome.tabs.create({url: chrome.runtime.getURL("test.html"), active: false});
console.log(`Page opened`);
await new Promise(resolve => {
chrome.scripting.executeScript({
files: [`testInjection.js`],
target: {
tabId: curPage
},
},
() => { resolve(true); });
});
console.log(`Script injected`);
}
Startup();
Here is my script to be injected testInjection.js:
"use strict";
function SendMainScriptMessage(packet, processResponse) { chrome.runtime.sendMessage(packet);
}
async function InjectedCodeListener(request, sender, sendResponse) {
SendMainScriptMessage({id: "Started"});
report.innerText = request.text;
SendMainScriptMessage({id: "Initialized"});
}
}
chrome.runtime.onMessage.addListener(InjectedCodeListener);
Finally, here is my web page test.html:
<html>
<body>
<p id="report"></p>
<button id="click">Open</button>
</body>
</html>
Here is Console output:
Page opened
Unchecked runtime.lastError: Cannot access contents of url "chrome-extension://lokbdjdhbldkdmccaekalojjmjejonkg/test.html". Extension manifest must request permission to access this host.
Script injected
Here is my environment:
- Windows 10 x64
- Chrome Version 106.0.5249.91 (Official Build) (64-bit)