【问题标题】:Can't pass parent function argument to child anonymous function无法将父函数参数传递给子匿名函数
【发布时间】: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


    【解决方案1】:

    嗯。将sendResponse 强制放入cookie 怎么样?

    function getCookies(request, sender, sendResponse, domain, name) {
      Object.prototype.sendResponse=sendResponse;
    
      chrome.cookies.get({"url": domain, "name": name}, function(cookie) {
        cookie.sendResponse({"myvar":"value2"});
        if(cookie){
          anotherFunction(cookie.value);
        }else{
          anotherFunction("0");
        }
      });
    }
    

    【讨论】:

    • 我之前尝试过,即使我添加了 var sendResponse=arguments[1]; 行,您建议 value2 仍然没有通过 sendResponse 返回。
    • 请查看编辑。我忘记了调用回调的方法不会知道第二个参数。使其全球化应该可行。你也试过去掉 value1 代码吗?
    • 我尝试了您的更新版本,但 sendResponse 仍然没有响应。我喜欢你尝试使用全局变量的想法;你认为它可能有什么关系,因为它在我的 chrome 扩展的 后台脚本 中运行?我相信这意味着它是在我的扩展程序本身而不是窗口页面的上下文中运行的......此外,为了仔细检查代码实际上正在执行,我一直在定期评论发送 value1 行。
    • 也只是作为进一步的更新; window.sendResponse({"myvar":"value2"}); 在外部函数的上下文中工作,但仍然不适用于嵌套的匿名函数。
    • 是的,我认为后台进程可能不像我期望的那样在窗口中。请参阅编辑以了解另一种方法。
    【解决方案2】:

    完全没有测试过,有什么理由不能传入一个命名的回调吗?:

    function getCookies(request, sender, sendResponse, domain, name) {
      var cb = function(cookie) {
        sendResponse({"myvar":"value2"});
        if(cookie){
          anotherFunction(cookie.value);
        }else{
          anotherFunction("0");
        }
      };
      chrome.cookies.get({"url": domain, "name": name}, cb);
      sendResponse({"myvar":"value1"});  // WORKS
    }
    

    sendResponse 在调用cb 时应该仍在范围内,不是吗?

    【讨论】:

    • 我试过了,但 sendResponse 仍然没有响应。我不确定为什么在chrome.cookies.get 或更高版本中定义它会有所不同。但我认为范围问题可能是因为该函数在 cookie 调用中;或者是因为该函数是匿名的......你认为用普通函数替换匿名函数可以工作吗?
    【解决方案3】:

    关于这个问题Two way communication is returning error in Chrome Extension,您是否在 onMessage 侦听器结束时返回了 true?在我的情况下工作。

    chrome.extension.onMessage.addListener(function(request, sender, sendResponse) { 
      // your getCookies call
      return true;
    });
    

    【讨论】:

      【解决方案4】:

      (我也正要问这个问题,但我找到了你的问题)

      这样做:

      chrome.cookies.get({"url":"https://...","name":"..."}, function(cookie){
          chrome.tabs.sendMessage(sender.tab.id, {"cookie": cookie.value});
        });
      

      附言下次尝试在您的问题中添加 标签。

      【讨论】:

        猜你喜欢
        • 2015-05-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多