边缘扩展browser.runtime.sendmessage()

时间:2017-01-13 20:07:21

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

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

的manifest.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);
    }
});

1 个答案:

答案 0 :(得分: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()。