简体   繁体   中英

Can't pass parent function argument to child anonymous function

In one of the background files of my chrome extension I'm checking for cookies and ran into a problem. In my function chrome.cookies.get({"url": domain, "name": name},function(cookie) {}); I am able to get the cookie object and pass it into the last parameter (an anonymous function) as the variable cookie and access it's value with cookie.value .

The problem is that I am only able to send a return response from the parent function and not the nested anonymous function from within the chrome.cookies.get call.

In my example below I am able to return a response (verified with an alert on another page) with value1 but value2 is never being returned... After doing a lot of reading I think I narrowed down the problem to a scoping error... By that I mean my response call sendResponse (a parameter in the parent function) is not being accessed by the nested anonymous function.

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
}

So basically, I just need to figure out some method to push my sendResponse parameter down to the anonymous function; or re-create a similar method that would not cause this scoping issue. Any assistance or guidance with this issue would be greatly appreciated. Thank you in advance for your time.

In reference to this issue Two way communication is returning error in Chrome Extension , did you returned true at the end of your onMessage listener? Worked in my case.

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

Hmm. How about forcing sendResponse into the 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");
    }
  });
}

Having completely not tested it, is there any reason you can't pass in a named callback instead?:

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 should still be in scope when cb gets called, no?

(I was just about to ask about it too, but I found your question)

just do this:

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

ps next time try adding tag to your questions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM