Does worker_proxy support manifest v3?
Hello Rob!
Thank you so much for the super creative and cool proxy worker_proxy.
I'm trying it in my extension that uses manifest v3. I followed the instruction in README and changed the background page to the following:
"background": {
"service_worker": "worker_proxy.js"
},
It seems my content script can load the web worker now! However, the worker doesn't send message to the content script :( I used the example worker script from the example, but I can't see the message Hello from worker! in the console.
Do you have any tips on using it with manifest v3? Thanks!!
Jay
@xiaohk did you solve it?
Thanks for the ping: I missed the earlier notification.
The snippet in its current form does not work in a Service-worker based extension, because the extension.getBackgroundPage() method doesn't work.
To fix that, modify the following part: https://github.com/Rob--W/chrome-api/blob/3080559bb29ed0f2ceb421b85d3288b8148a9d91/worker_proxy/worker_proxy.js#L54-L55
Change from:
if (chrome.extension.getBackgroundPage &&
chrome.extension.getBackgroundPage() === window) {
To
if (chrome.extension.getBackgroundPage ?
chrome.extension.getBackgroundPage() === window : typeof importScripts === "function" && typeof chrome == "object") {
The new part detects the service worker environment. Specifically it detects the importScripts method that is only available to (web) workers, plus the chrome namespace available to extension workers.
@Rob--W , Thanks for the update,
I have tried the example and incorporating the fix you mentioned in this thread but I am getting Uncaught DOMException: Failed to construct 'Worker': Script at 'chrome-extension://jfhpnabmpfabcelbbindafmahknbgodm/worker.js' cannot be accessed from origin 'https://www.instagram.com'. at chrome-extension://jfhpnabmpfabcelbbindafmahknbgodm/content.js:1:14
For context, my extension is supposed to run on any page, I don't know if there's anything in particular that I need to change about the manifest or so (I am kinda new to chrome extensions development), here's the current manifest:
{
"manifest_version": 3,
"name": "web worker",
"version": "1.0",
"permissions": ["activeTab", "scripting"],
"host_permissions": ["<all_urls>"],
"background": {
"service_worker": "worker_proxy.js"
},
"content_scripts": [
{
"js": ["content.js", "worker_proxy.js"],
"matches": ["<all_urls>"],
"run_at": "document_end"
}
],
"web_accessible_resources": [
{
"resources": [ "worker_proxy.html" ],
"matches": ["<all_urls>"]
}
]
}
@Rob--W , Thanks for the update,
I have tried the example and incorporating the fix you mentioned in this thread but I am getting
Uncaught DOMException: Failed to construct 'Worker': Script at 'chrome-extension://jfhpnabmpfabcelbbindafmahknbgodm/worker.js' cannot be accessed from origin 'https://www.instagram.com'. at chrome-extension://jfhpnabmpfabcelbbindafmahknbgodm/content.js:1:14For context, my extension is supposed to run on any page, I don't know if there's anything in particular that I need to change about the manifest or so (I am kinda new to chrome extensions development), here's the current manifest:
{ "manifest_version": 3, "name": "web worker", "version": "1.0", "permissions": ["activeTab", "scripting"], "host_permissions": ["<all_urls>"], "background": { "service_worker": "worker_proxy.js" }, "content_scripts": [ { "js": ["content.js", "worker_proxy.js"], "matches": ["<all_urls>"], "run_at": "document_end" } ], "web_accessible_resources": [ { "resources": [ "worker_proxy.html" ], "matches": ["<all_urls>"] } ] }
add worker.js file in web_accessible_resources -> resources array, then you can use it.