chrome-api icon indicating copy to clipboard operation
chrome-api copied to clipboard

Does worker_proxy support manifest v3?

Open xiaohk opened this issue 2 years ago • 9 comments

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 avatar Aug 17 '23 14:08 xiaohk

@xiaohk did you solve it?

alganzory avatar Sep 23 '23 00:09 alganzory

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 avatar Sep 23 '23 20:09 Rob--W

@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>"]
		}
	]
}

alganzory avatar Sep 24 '23 12:09 alganzory

@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>"]
		}
	]
}

add worker.js file in web_accessible_resources -> resources array, then you can use it.

Shourya-BrainAlive avatar May 10 '24 20:05 Shourya-BrainAlive