Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing Cookies in Chrome Extension

I'm trying to write a chrome extension that works with YouTube and need to access some of YouTube's cookie information. I cant seem to get my extension to see any cookies. (Even though I can see them under resources in the "Inspect Element" developer portion of Chrome).

I'm pretty sure I've set up permissions correctly in the manifest 2 file because when I take out the "cookies" permission just to test it I get an error saying "Cannot call method 'getAll'". My current problem is just that no cookies are returned by the callback function.

{
"manifest_version": 2,
"name": "YouTube Viewer",
 "description": "This extension is for YouTube videos.",
 "version": "1.7",

 "icons": {
 "128": "ytblack.png"
 },

 "permissions": [
 "cookies",
 "https://www.youtube.com/",
 "http://www.youtube.com/",
 "tabs",
 "storage"
 ],

 "background": {
   "scripts": ["bootstrap.js"],
   "persistent": false
  },

 "page_action": {
 "default_title": "YT View",
 "default_icon": "ytblack.png",
 "default_popup": "popup.html"
 }

}

My manifest calls the bootstrap.js. Inside bootstrap.js there is a call to another file ytview.js but I'm not concerned with that. The code in that is working fine. But inside bootstrap.js my cookies.length is returning as 0 when I look at my "background page" console. The log for "Callback for cookies came in fine." fires correctly. But then it says "cookies.length=0". Like I said, I know the cookies exist because I can see them in the resources.

chrome.tabs.onUpdated.addListener(function(id, info, tab){

// decide if we're ready to inject content script
if (tab.status !== "complete"){
    console.log("not yet");
    return;
}
if (tab.url.toLowerCase().indexOf("youtube.com/watch") === -1){
    console.log("you are not on a YouTube video");
    return;
}

chrome.cookies.getAll({domain: "www.youtube.com"}, function(cookies) {
console.log('Callback for cookies came in fine.');
console.log('cookies.length=' + cookies.length);        
    for(var i=0; i<cookies.length;i++) {
      console.log('cookie=' + cookies[i].name);
    }
  });
chrome.tabs.executeScript(null, {"file": "ytview.js"});

});

Any ideas why no cookies are being returned? Maybe something with "domain" in the .getAll statement? I've tried lots of combinations like www.youtube.com, youtube.com, https://www.youtube.com with no luck.

like image 394
Thread7 Avatar asked Oct 26 '25 21:10

Thread7


1 Answers

for future users: youtube.com use ".youtube.com" as cookie domain to allow the site to share cookies across all youtube subdomains so in your example you should use domain name without 'www' subdomain for example:

chrome.cookies.getAll({domain: "youtube.com"}, function(cookies) {
  //...
});

you can clearly see cookies domain using default chrome developer tools

enter image description here

like image 60
Fareed Alnamrouti Avatar answered Oct 28 '25 12:10

Fareed Alnamrouti



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!