【问题标题】:Edge Extension browser.runtime.sendmessage()边缘扩展 browser.runtime.sendmessage()
【发布时间】:2017-01-13 20:07:20
【问题描述】:

尝试将扩展程序从 chrome 转换为 edge,但我遇到了带有监听器的 browser.runtime.sendmessage() 问题。为了简化事情,我创建了一个简单的 hello world 扩展。这个新扩展适用于 chrome,但不适用于边缘。有人知道我需要改变什么吗?

ma​​nifest.json

{
  "manifest_version": 2,
  "name": "My Cool Extension",
  "author" : "Sandbox",
  "version": "0.1",
  "content_scripts": [ {
    "all_frames": true,
    "js": [ "jquery-3.1.1.min.js", "content_script.js" ],
    "matches": [ "http://*/*", "https://*/*", "file://*/*" ]
  } ],
  "permissions": [ "http://*/*", "https://*/*", "storage" ],
  "background": {
    "scripts": [
      "jquery-3.1.1.min.js",
      "background.js"
    ], 
    "persistent": true 
  }
}

background.js

chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    console.log(request.greeting);
    if (request.greeting == "hello")
      sendResponse({farewell: "goodbye"});
  });

content_scripts.js

browser.runtime.sendMessage({greeting: "hello"}, function(response) {
    if (response != undefined) {
        console.log(response.farewell);
    }
});

【问题讨论】:

  • 我不确定,但我遇到了这个问题,看起来 Edge 需要刷新页面才能发送消息。
  • 在chrome中,你应该使用chrome.* api,而在Edge中,你应该使用browser.* api。
  • 据我所知,我在边缘或 chrome 中使用浏览器或 chrome 并不重要,但我没有意识到我是混合和匹配的。周一回去工作时,我会让它们都一样
  • 当我把它们都做成“浏览器”时,它就可以工作了。

标签: javascript microsoft-edge microsoft-edge-extension


【解决方案1】:

Edge 的正确语法在这里:Edge Supported APIs

在注释部分:

For Microsoft Edge, all extension APIs are under the browser namespace, 
e.g. browser.browserAction.disable().
Microsoft Edge extension APIs use callbacks, not promises.

使用 browser.runtime.sendMessage() 代替 chrome.runtime.sendMessage()。

【讨论】:

  • 在我弄清楚这一点后将近 3 年,但感谢您抽出宝贵时间回答。
猜你喜欢
  • 2018-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多