尝试将扩展从 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);
}
});