谁能向我解释一下为什么输出是:“x is: 0 y is: undefined”??
浏览器操作.js
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {msg: "test"}, function(x,y){
document.getElementById("status").innerHTML = "x is: " + x + " y is: " + y;
});
});
内容脚本.js
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse){
if(request.msg == "test"){
sendResponse(0,0);
}
});
最佳答案
根据
-
chrome.tabs.sendMessage(integer tabId, any message, object options, function responseCallback)
-
chrome.runtime.onMessage.addListener(function callback)
在responseCallback
和sendResponse
函数中,只有一个参数,即json响应对象。调用 sendResponse
时可能会删除额外的参数(我猜)
正如我们在 JavaScript 中所知,未提供的参数将变为 未定义
,这就是为什么在访问 时会得到
在 未定义
>ychrome.tabs.sendMessage
的回调中。
关于javascript - Chrome 扩展回调传递两个值,其中一个显示未定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38618980/