Communication between content script and background script in Microsoft Edge

给你一囗甜甜゛ 提交于 2019-12-07 20:09:22

问题


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:

  1. Install this extension from the package
  2. 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.
  3. Open background page of this extension. Then this page may be closed.
  4. Reload the test page
  5. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!