【发布时间】:2017-01-13 20:07:20
【问题描述】:
尝试将扩展程序从 chrome 转换为 edge,但我遇到了带有监听器的 browser.runtime.sendmessage() 问题。为了简化事情,我创建了一个简单的 hello world 扩展。这个新扩展适用于 chrome,但不适用于边缘。有人知道我需要改变什么吗?
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);
}
});
【问题讨论】:
-
我不确定,但我遇到了这个问题,看起来 Edge 需要刷新页面才能发送消息。
-
在chrome中,你应该使用
chrome.*api,而在Edge中,你应该使用browser.*api。 -
据我所知,我在边缘或 chrome 中使用浏览器或 chrome 并不重要,但我没有意识到我是混合和匹配的。周一回去工作时,我会让它们都一样
-
当我把它们都做成“浏览器”时,它就可以工作了。
标签: javascript microsoft-edge microsoft-edge-extension