views:

2300

answers:

5

Is there any way to access cookies from Chrome extension? this code

document.cookie.length

always returns - 0.

A: 

It seems Google prevents you from accessing document.cookie on user pages. This is most likely for security reasons.

Google seems to be designing their extension API with security in mind from the ground up. This means not having access to user preferences on web sites.

If you want to store user-settings in your extension, you can use a background page. Background pages have their own document.cookie object they can use that behaves the way a standard document.cookie behaves.

Dan Herbert
A: 

Even better, you can use the HTML5 Localstorage:

localStorage.setItem("itemid", "hello" );   // write
value = localStorage.getItem("itemid");     // read

If you really wanted to read the cookies of any site the user is watching, as Dan wrote, it is not possible, as really bad things could be done.

All you can get from a page is the page DOM content.

UVL
+5  A: 

It's the first time I read something really wrong on this site. Getting actual document cookies from an extension is INDEED possible.

you just need these two things in your manifest:

"content_scripts": [
    {
      "matches": ["http://*/*", "https://*/*"],
      "js": ["cookie_handler.js"]
    }
  ],
  "permissions": [
    "tabs",
    "http://*/*",
    "https://*/*"
  ],

your cookie_handler.js will be executed in the same context of every loader page/frame/iframe. try to put there a single line:

alert(document.cookie);

and you will see :)

Zibri
Does this mean that it is possible to make an extension like Permit Cookies from Firefox? That extension allows the user for each site to Allow always, Allow for session, Block, Remove. Or can such an extension not be made for Chrome?
Louise
+1  A: 

I did a little post on how to go about it, also explaining how to get the cookie info into to you background script.

http://ttcremers.blogspot.com/2010/01/accessing-site-cookies-from-chrome.html

Thomas Cremers
+1  A: 

If you're looking to manipulate cookie information without the user having to visit the site, (useful for something like FireFox's TACO), you're currently out of luck. Looks like Google's working on it though: they recently added a relatively complete cookie handler to the experimental API: chrome.experimental.cookies

Hopefully this will graduate to supported API soon.

Ian Wilkes