在Chrome扩展程序中访问Cookie [英] Accessing Cookies in Chrome Extension

查看:579
本文介绍了在Chrome扩展程序中访问Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个适用于YouTube的Chrome扩展程序,并且需要访问YouTube的部分Cookie信息。我似乎无法让我的扩展程序看到任何cookie。 (尽管我可以在Chrome的Inspect Element开发者部分的资源中看到它们)。



我很确定我在清单2文件,因为当我拿出cookies权限来测试它时,我收到一个错误,说无法调用方法'getAll'。

  {
manifest_version:2,
name:YouTube查看器,
说明:此扩展程序用于YouTube视频。,
版本:1.7,

图标:{
128:ytblack.png
},

权限:[
cookies,
https://www.youtube.com/,
http://www.youtube.com/,
标签,
存储
] ,

background:{
scripts:[bootstrap.js],
持久性:false
},

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


$ b

我的清单调用了bootstrap.js。在bootstrap.js里面有一个对另一个文件ytview.js的调用,但我不关心这个。该代码在工作正常。但是在bootstrap.js中,当我查看后台页面控制台时,我的cookies.length返回为0。 回拨cookie的日志没有问题。正确着火。但后来它说cookies.length = 0。就像我说的,我知道cookie存在,因为我可以在资源中看到它们。

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

//决定是否准备好注入内容脚本
if(tab.status!==complete){
console.log(not yet);
return;
}
if(tab.url.toLowerCase()。indexOf(youtube.com/watch)=== -1 ){
console.log(您不在YouTube视频中);
return;
}

chrome.cookies.getAll({domain: www.youtube.com},函数(cookies){
console.log('回调cookie正常工作');
console.log('cookies.length ='+ cookies.length );
for(var i = 0; i< cookies.length; i ++){
console.log('cookie ='+ cookies [i] .name);
}
$ b);
chrome.tabs.executeScript(null,{file:ytview.js});

});

任何想法为什么不返回cookie?也许在.getAll语句中带有domain的东西?我尝试了很多组合,例如www.youtube.com,youtube.com,


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.

解决方案

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

这篇关于在Chrome扩展程序中访问Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆