Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is chrome.cookies undefined in a content script?

Whenever I try to read from a cookie using the chrome.cookies.get() function I get this error:

TypeError: Cannot read property 'get' of undefined.

I am calling the function in my content.js file, and it will only run on twitter.com(that part works).

Here is my manifest file:

{
  "manifest_version": 2,

  "name": "Twitter-MultiSignin",
  "description": "twiter sign in",
  "version": "1.0",
  "permissions": [ "cookies", "alarms" , "http://*/*", "https://*/*", "storage"],
  "content_scripts": [{
    "matches": ["http://twitter.com/*","https://twitter.com/*"],
    "js": ["jquery.js","content.js"]
  }],
  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
  }
}

Here is my content.js (it always alerts on the twitter page so that works fine):

$(function() {
    alert('got here');
    try{
        chrome.cookies.get({ url: 'https://twitter.com', name: 'auth_token' }, function (cookie){
            alert(cookie.value);
        });
    }catch(err){
        //do nothing
        alert(err);
    }
    try{
        chrome.cookies.get({ url: 'https://twitter.com', name: 'twid' },
        function (cookie) {
            if (cookie) {
              twid_raw = cookie.value;
              twid = twid_raw.split('=')[1]
              alert(twid);
            }
            else{
                alert("not found");
            }
        });
    }catch(err){
        //do nothing
    }
})
like image 393
parasm Avatar asked Apr 13 '14 01:04

parasm


People also ask

How do I add cookies to Chrome extensions?

chrome. cookies. set({ url: "http://example.com/", name: "CookieVar", value: "123", expirationDate: 3600 });

Can extensions access cookies?

With the Cookies API your extensions have access to capabilities similar to those used by websites to store and read cookies.

Can Chrome extensions have cookies?

One one hand, yes, cookies are available in Chrome extensions.


1 Answers

In case someone skipped this, be sure to add "cookies" to permissions.

like image 104
jbodily Avatar answered Sep 27 '22 17:09

jbodily