electron
electron copied to clipboard
[Bug]: Chrome Extension V3 service_worker - Supported APIs do not function
Preflight Checklist
- [X] I have read the Contributing Guidelines for this project.
- [X] I agree to follow the Code of Conduct that this project adheres to.
- [X] I have searched the issue tracker for a bug report that matches the one I want to file, without success.
Electron Version
13.6.9, 18.2.0, 20.0.0-nightly.20220505
What operating system are you using?
macOS
Operating System Version
macOS Monterey 12.3.1
What arch are you using?
x64
Last Known Working Electron version
N/A
Expected Behavior
For successful messages to be sent to and from the Service Worker in the extension.
The test case provided uses Electron's supported Chrome APIs as defined here: https://www.electronjs.org/docs/latest/api/extensions#loading-extensions
- A content page is supplied by this extension which simply calls "chrome.runtime.sendMessage" to send a message.
- A service worker is supplied by this extension which calls
- "chrome.runtime.onInstalled" to print a logging statement
- "chrome.runtime.onMessage.addListener" to listen for messages, and to send a response.
Expected to see the log message show up on any web page:
I'm a content script, and I'm going to try to send a message to anyone that is listening.
Hello sender, I received your message.
With the ELECTRON_ENABLE_LOGGING=1 env set, expected to see:
[xxx:INFO:CONSOLE(1)] "Hello, I'm a service worker", source: chrome-extension:///<some_chrome_extension_id>/background.js
[xxx:INFO:CONSOLE(1)] "Hello, I was just installed.", source: chrome-extension:///<some_chrome_extension_id>/background.js
[xxx:INFO:CONSOLE(1)] "Hello, I'm a service worker and I just received a message.", source: chrome-extension:///<some_chrome_extension_id>/background.js
Actual Behavior
Actual logs seen on any web page:
I'm a content script, and I'm going to try to send a message to anyone that is listening.
Actual logs seen in terminal:
With the ELECTRON_ENABLE_LOGGING=1 env set, expected to see:
[xxx:INFO:CONSOLE(1)] "Hello, I'm a service worker", source: chrome-extension:///<some_chrome_extension_id>/background.js
Testcase Gist URL
https://gist.github.com/schetle/8ea7451215b89c4cc28506f96e9f384a
Additional Information
By January of this upcoming year, Manifest V2 extensions will cease to function (please see: https://developer.chrome.com/docs/extensions/mv3/mv2-sunset/)
Part of the migration process to get from a V2 -> V3 extension requires that background pages are replaced with service workers (please see: https://developer.chrome.com/docs/extensions/mv3/intro/mv3-migration/#background-service-workers)
@schetle can you please provide an example manifest for this fiddle? As is it will error becuase the manifest is missing.
@codebytere The manifest is already there. I could not use Electron Fiddle to create this, as the Fiddle application does not allow .json files.
https://gist.github.com/schetle/8ea7451215b89c4cc28506f96e9f384a#file-manifest-json
Any thoughts on this? Or do you have any pointers in a direction where I can investigate where it might be failing? Thank you!
Have you specified the scripting(maybe tabs) permission on the manisfest file?
Manifest.json
"permissions" : ["scripting","tabs"]
And also if the chrome extension api works the same way in electron the you need to send an object instead of a text message
content.js
chrome.runtime.sendMessage({data:"Hello from content script"}, function(response) { console.log(
I have receipted a response: ${response}); });
Finnaly the reply must be returned(also with a JSON object) like this:
background.js
chrome.runtime.onMessage.addListener((message, sender, reply) => { console.log(
Hello, I'm a service worker and I just received a message.`);
return reply({ 'data' :"Hello sender, I received your message."});
}); `
@wsldr1 The extension code posted in the gist link above works in 2 scenarios:
- The extension works as expected in Chrome, and is what the expected behavior is based on.
- The extension works in the same application (in the Gist above), when you downgrade the Manifest version to v2, and redefine the unmodified background script as the old style background script, rather than the new Service Worker.
I'd rather not get too deep into the extension code itself, because it feels almost off-topic, as is shown by the extension code working both in Chrome and Electron (background pages, not service workers).
You can sendMessage, as well as "reply" with a string object, as it is "JSON-ifiable" (see.: https://developer.chrome.com/docs/extensions/reference/runtime/#method-sendMessage). That's why it works in both environments above.
You don't explicitly have to return from an onMessage handler, as either bool or undefined are acceptable return values from onMessage, and not explicitly returning is the same as returning undefined. (see: https://developer.chrome.com/docs/extensions/reference/runtime/#event-onMessage)
Are you suggesting that Service Workers in Electron might require different permissions than standard background pages do in Electron? I can certainly try changing the permissions if Service Workers inherently require different permissions in Electron, which would deviate from Chrome's behavior.
Hiya,
Is there an update on this bug or whether there is support for v3 manifest planned any time soon?
This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. If you have any new additional information—in particular, if this is still reproducible in the latest version of Electron or in the beta—please include it with your comment!
bump
In my extension, content script send message the background(serverWorker) can not receive the message... It works well on chrome browser I don't know how to resolve it
This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. If you have any new additional information—in particular, if this is still reproducible in the latest version of Electron or in the beta—please include it with your comment!
bump
@wsldr1 The extension code posted in the gist link above works in 2 scenarios:
- The extension works as expected in Chrome, and is what the expected behavior is based on.
- The extension works in the same application (in the Gist above), when you downgrade the Manifest version to v2, and redefine the unmodified background script as the old style background script, rather than the new Service Worker.
I'd rather not get too deep into the extension code itself, because it feels almost off-topic, as is shown by the extension code working both in Chrome and Electron (background pages, not service workers).
You can sendMessage, as well as "reply" with a string object, as it is "JSON-ifiable" (see.: https://developer.chrome.com/docs/extensions/reference/runtime/#method-sendMessage). That's why it works in both environments above.
You don't explicitly have to return from an onMessage handler, as either bool or undefined are acceptable return values from onMessage, and not explicitly returning is the same as returning undefined. (see: https://developer.chrome.com/docs/extensions/reference/runtime/#event-onMessage)
Are you suggesting that Service Workers in Electron might require different permissions than standard background pages do in Electron? I can certainly try changing the permissions if Service Workers inherently require different permissions in Electron, which would deviate from Chrome's behavior.
Wait, does that mean that only Manifest V2 Extensions work on Electron? This is a huge problem since Chrome is deprecating the Manifest V2. Isn't there any work around for that?
Manifest V3 uses Promises, and it seems sendMessage is also affected with the change. You should rewrite your sendMessage call:
https://developer.chrome.com/docs/extensions/mv3/promises/#compare-to-callback
Change your code in content.js to:
chrome.runtime.sendMessage("Hello from content script").then((response) => { console.log("I have receipted a response: " + response); });
Hey folks - see https://github.com/electron/electron/issues/37876#issuecomment-1656808686