1

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

清单.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 
  }
}

背景.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);
    }
});
4

1 回答 1

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()。

于 2019-10-16T15:16:36.013 回答