最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Accessing Cookies in Chrome Extension - Stack Overflow

programmeradmin0浏览0评论

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",
 "/",
 "/",
 "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 !== "plete"){
    console.log("not yet");
    return;
}
if (tab.url.toLowerCase().indexOf("youtube/watch") === -1){
    console.log("you are not on a YouTube video");
    return;
}

chrome.cookies.getAll({domain: "www.youtube"}, 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 binations like www.youtube, youtube, with no luck.

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./",
 "http://www.youtube./",
 "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 !== "plete"){
    console.log("not yet");
    return;
}
if (tab.url.toLowerCase().indexOf("youtube./watch") === -1){
    console.log("you are not on a YouTube video");
    return;
}

chrome.cookies.getAll({domain: "www.youtube."}, 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 binations like www.youtube., youtube., https://www.youtube. with no luck.

Share Improve this question asked Jan 18, 2014 at 4:23 Thread7Thread7 1,0902 gold badges14 silver badges29 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

for future users: youtube. use ".youtube." 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."}, function(cookies) {
  //...
});

you can clearly see cookies domain using default chrome developer tools

I figured it out. In my manifest I was asking for permission on www.youtube. but the cookies I was trying to read were on simply youtube. without the www. Adding the plain youtube. to the permissions in manifest fixed it.

发布评论

评论列表(0)

  1. 暂无评论