Javascript chrome.runtime.sendMessage无法进行泛化
案例1 我使用此代码段向我的后台页面发送请求,并接收消息Javascript chrome.runtime.sendMessage无法进行泛化,javascript,google-chrome,google-chrome-extension,chrome-runtime,Javascript,Google Chrome,Google Chrome Extension,Chrome Runtime,案例1 我使用此代码段向我的后台页面发送请求,并接收消息 chrome.runtime.sendMessage({details: "Command1"}, function(response) { console.log(response.farewell); }); 当它被称为“独立”时,它可以完美地工作 以下是响应请求的我的背景页面代码:- if(typeof(request.details) != "undefined"){ console.
chrome.runtime.sendMessage({details: "Command1"}, function(response) {
console.log(response.farewell);
});
当它被称为“独立”时,它可以完美地工作
以下是响应请求的我的背景页面代码:-
if(typeof(request.details) != "undefined"){
console.log("Detail Array was asked for !");
sendResponse({farewell: "Some Data here"});
console.log("Respo 1 sent");
}
正如预期的那样,在从内容脚本页面调用details:command1时,我在内容脚本中得到了预期的输出“这里有一些数据”
案例2
我试图将其推广到许多请求中,因此,我对我的内容脚本做了如下更改:-
function sendMessage(toSendKey, toSendVal, funcName){
chrome.runtime.sendMessage({toSendKey: toSendVal}, function(response) {
console.log("Response received");
respoObt = response.farewell;
console.log(respoObt);
funcName(respoObt);
});
}
function doNothing(data){
console.log(data);
}
sendMessage("details", "Command1", doNothing);
现在,在使用适当的参数调用函数时,我可以在我的后台页面中看到两个控制台日志(“Detail Array was Asquired!”,“Respo 1 sent”),确认请求已到达,但返回的响应无法到达内容脚本页面
可能的罪魁祸首是什么?我遗漏了什么吗?新的背景代码是什么?你还在寻找请求。详细信息吗?@dandavis是的,我是!如果您仍在查找request.details,则需要在您发送的新邮件信封上弹出该消息,否则该条件将始终失败,并且后台不会发生任何事情。发布坏代码以获得关于坏代码的帮助。但是背景代码似乎正在接收输入。它与第一种情况下的完全相同