我正在尝试在下面 contentscript
之间发送消息background script
内容脚本.js
chrome.extension.sendMessage({ type : "some" }, function(response) {
anotherFunction( response.data );
return true;
});
function anotherFunction(data){
// Some code here
chrome.extension.sendMessage({ type : "someOther" }, function(response) {
console.log( response.data ); // Failed to get Response
return true;
});
}
背景.js
chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
switch(request.type){
case "some":
sendResponse({ data : "Some Response" });
return true;
break;
case "someOther":
// Here I am getting an error. Error is given below
sendResponse({ data : "Some Response" });
break;
}
});
错误
无法发送响应:如果要在侦听器返回后发送响应,则 chrome.runtime.onMessage 侦听器必须返回 true
我该如何解决这个问题。?