问题
I have a problem with communication based on browser.runtime.connect between a content script and a background script of my test echo extension. I have published my code and extension package on github.
Short description of this extension. You can run the extension on this GitHub test page. The content script connects to the background script using browser.runtime.connect and sends a message via browser.runtime.Port object when “Submit” button is clicked. The background script receives the message, converts it to upper case and sends back. Then content script receives the modified message and displays it on the page (received message field).
This extension works fine when I add it to Edge using Load extension button as described in the Microsoft Edge documentation. So if you open the test page with the loaded extension you should see “HELLO FROM BACKGROUND SCRIPT” text in the received message field.
But there is a strange problem if this extension is installed from a package… The extension doesn’t work until I open the background page of this extension and reload the test page… So you can try to reproduce it:
- Install this extension from the package
- Open the test page. The background script doesn’t work properly (received message field is empty) and this script doesn’t receive sent messages… Page reloading or turning off\on of the extension doesn’t help.
- Open background page of this extension. Then this page may be closed.
- Reload the test page
- Extension starts to work properly…
Note: this test extension is based on the runtime.connect example published on MDN (sorry, but I can not insert more than two links...).
Could you help me to solve this problem? Maybe I missed something in the documentation...
回答1:
This is by design since your extension manifest specified persistent flag as false. On Microsoft Edge, the default behavior for background script is non-persistent.
来源:https://stackoverflow.com/questions/44234322/communication-between-content-script-and-background-script-in-microsoft-edge