简体   繁体   中英

Cookies permission in Chrome extension doesn't work

I'm having problem with my Chrome extension when I try adding cookies permission. First, the manifest file is like this

"permissions": [
    "cookies",
    "https://api.box.com/*", 
    "https://www.box.com/api/*",
    "https://dl.boxcloud.com/*",
    "tabs",
    "identity",
  ],

But when I click to the permission properties of my extension in chrome://extensions/, there is no cookies permission showing up. I'm not sure whether it is problematic or not, please help me clarify.

The main problem is, when I run the code:

getCookies("https://www.box.com", "tokens", function(tokens){
        console.log("Token returned");     
});

function getCookies(domain, name, callback) {
    console.log("Getting cookies...");
    chrome.cookies.get({"url": domain, "name": name}, function(cookie) {
        if(callback){
            console.log("Done getting cookies, calling back...");                    
            callback(cookie.value);
        }
    });
}

The callback function is never called, thus the Token returned message is never printed. I could see the "Done getting cookies, calling back.. message in the console. So why is this happening? Please help me resolve it...

There are two problems with the code:

  1. If a cookie is not found, chrome.cookies.get callback is called with cookie == null .

    Therefore, calling callback(cookie.value) is an error and doesn't get executed.

  2. The cookie is not found, because the URL you're passing, https://www.box.com , does not match the permission pattern "https://www.box.com/api/*" . You need to either change the pattern or the URL you're passing to chrome.cookies.get .

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