Chrome扩展消息传递:响应未发送我试图在内容脚本和扩展之间传递消息。以下是我的内容-脚本chrome.runtime.sendMessage({type: "getUrls"}, function(response) {
console.log(response)});在我的背景剧本里chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.type == "getUrls"){
getUrls(request, sender, sendResponse)
}});function getUrls(request, sender, sendResponse){
var resp = sendResponse;
$.ajax({
url: "http://localhost:3000/urls",
method: 'GET',
success: function(d){
resp({urls: d})
}
});}现在,如果我在Ajax调用之前在getUrls函数中,响应被成功发送,但是在Ajax调用的成功方法中,当我发送响应时它没有发送它,当我开始调试时,我可以看到端口在下面的代码中为空sendResponse功能。
4 回答

撒科打诨
TA贡献1934条经验 获得超2个赞
chrome.extension.onMessage.addListener(function (request, sender, sendResponseParam) {if (request.method == "method1") { handleMethod1(sendResponse);}
handleMethod1
handleMethod1
chrome.extension.onMessage.addListener(function (request, sender, sendResponseParam) {var responseStatus = { bCalled: false }; function sendResponse(obj) { //dummy wrapper to deal with exceptions and detect async try { sendResponseParam(obj); } catch (e) { //error handling } responseStatus.bCalled= true;}if (request.method == "method1") { handleMethod1(sendResponse);}else if (request.method == "method2") { handleMethod2(sendResponse);}...if (!responseStatus.bCalled) { //if its set, the call wasn't async, else it is. return true;}});
添加回答
举报
0/150
提交
取消