我需要在Firefox扩展程序的内容和后台脚本之间进行异步通信。后台脚本从内容接收消息。 MDN指示browser.runtime.sendMessage返回Promise,因此可以使用.than附加回调。但是,当尝试调用它时,出现错误消息: TypeError:send.than不是函数。
怎么了?
我正在使用Linux,Firefox v 60.0.1,通过about:debugging页面作为临时加载扩展程序。
Manifest.json:
{
"manifest_version": 2,
"name": "messagetest",
"version": "1.0",
"description": "Testing content to background message sending",
"icons": {
"32": "icons/bird.png"
},
"background": {
"scripts": ["background.js"]
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["content.js"]
}
],
"permissions": [
"<all_urls>"
]
}
content.js:
function handleResponse(msg)
{
console.log("Response HANDLED: " + msg);
}
function handleError(e)
{
console.log("ERROR HANDLED");
}
var sending = browser.runtime.sendMessage({request: "messagetest"});
sending.than(handleResponse, handleError);
background.js:
function onContentMessage(msg, sender, handleResponse)
{
console.log("MESSAGE RECEIVED");
handleResponse("RESPOND FROM BACKGROUND:");
}
browser.runtime.onMessage.addListener(onContentMessage);