Microsoft Edge中内容脚本和后台脚本之间的通信 [英] Communication between content script and background script in Microsoft Edge

查看:187
本文介绍了Microsoft Edge中内容脚本和后台脚本之间的通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的内容脚本和测试回显扩展程序的后台脚本之间存在基于 browser.runtime.connect 的通信问题。我已在 github 上发布了我的代码和扩展包。

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.

此扩展程序的简短说明。您可以在这个GitHub测试页面。内容脚本使用 browser.runtime.connect 连接到后台脚本,并在单击提交按钮时通过 browser.runtime.Port 对象发送消息。后台脚本接收消息,将其转换为大写并发回。然后内容脚本接收修改后的消息并将其显示在页面上(收到的消息字段)。

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).

当我将其添加到边缘使用加载扩展按钮,如Microsoft Edge文档中所述。因此,如果您使用加载的扩展程序打开测试页,您应该会在收到的消息字段中看到HELLO FROM BACKGROUND SCRIPT文本。

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. 安装此扩展程序来自包

  2. 打开测试页。后台脚本无法正常工作(收到的消息字段为空)并且此脚本未收到已发送的消息...页面重新加载或关闭扩展程序的内容无效。

  3. 打开此扩展程序的后台页面。然后此页面可能会关闭。

  4. 重新加载测试页

  5. 扩展程序开始正常运行...

  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…

注意:此测试扩展基于MDN上发布的 runtime.connect 示例(对不起,但我不能插入两个以上的链接......)。

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...

推荐答案

这是设计的,因为您的扩展清单指定持久标志为false。在Microsoft Edge上,后台脚本的默认行为是非持久性的。

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.

这篇关于Microsoft Edge中内容脚本和后台脚本之间的通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆