我想从background.html中读取我的全局变量,并使用以下代码:
chrome.extension.sendRequest()我在我的计时器函数中运行这段代码...每次它返回我未定义的。如果我在上述函数中放入一条警告消息,那么它会显示我变量。如果我没有使用alert(),那么它就是未定义的。那么我如何等待上面的函数返回正确的值,...据我所知,我需要等待回应...但是我怎么才能做到这一点呢?我试着在上面的函数中使用setInterval,但是没有帮到我。如何从background.html中读取我的全局值...我需要一段代码。
chrome.extension.sendRequest({cmd: "myvalue"}, function(response) {
myglobalvalue = response;
if(response==1){
alert("response: "+response); // alert show me right value
}
});
if(myglobalvalue)doFunc(); //myglobalvalue is undefenited发布于 2012-03-06 21:37:09
发布于 2012-03-07 02:20:02
chrome.extension.sendRequest接受一个回调函数,该函数将与响应数据一起触发。
chrome.extension.sendRequest({ data: 'send data' }, function(response) {
console.log(response);
// the var has been returned and you can use it here or call another function.
});发布于 2012-03-07 04:08:51
我建立了这个,以提供一个弹出式和背景页面之间的连续双向通信。“如何使用”一节告诉你该怎么做。只需在您的弹出和背景页面中包括socket.js,连接将自动为您创建。
只需使用:
Socket.postMessage(namespace, literal, method, [args]);这将允许您触发可以回调数据的函数。
https://stackoverflow.com/questions/9582795
复制相似问题