【发布时间】:2013-11-30 01:21:42
【问题描述】:
在我的 chrome 扩展程序的一个背景文件中,我正在检查 cookie 并遇到了问题。在我的函数chrome.cookies.get({"url": domain, "name": name},function(cookie) {}); 中,我能够获取 cookie 对象并将其作为变量 cookie 传递给最后一个参数(匿名函数),并使用 cookie.value 访问它的值em>。
问题是我只能从父函数发送返回响应,而不能从 chrome.cookies.get 调用中发送嵌套匿名函数。
在下面的示例中,我能够返回带有 value1 的响应(通过另一个页面上的警报验证),但永远不会返回 value2... 完成后大量阅读我认为我将问题缩小到范围错误......我的意思是我的响应调用 sendResponse (父函数中的一个参数)没有被嵌套匿名函数访问.
function getCookies(request, sender, sendResponse, domain, name) {
chrome.cookies.get({"url": domain, "name": name}, function(cookie) {
sendResponse({"myvar":"value2"}); //DOES NOT WORK
if(cookie){
anotherFunction(cookie.value);
}else{
anotherFunction("0");
}
});
sendResponse({"myvar":"value1"}); // WORKS
}
所以基本上,我只需要找出一些方法将我的 sendResponse 参数推送到匿名函数;或重新创建不会导致此范围界定问题的类似方法。任何有关此问题的帮助或指导将不胜感激。提前感谢您的宝贵时间。
【问题讨论】:
标签: javascript google-chrome-extension scope arguments anonymous-function