每当我尝试使用chrome.cookies.get()
函数从cookie读取时,都会出现此错误:
TypeError: Cannot read property 'get' of undefined.
我正在我的content.js文件中调用该函数,它将仅在twitter.com上运行(该部分有效)。
这是我的 list 文件:
{
"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"
}
}
这是我的content.js(它总是在Twitter页面上发出警报,因此工作正常):
$(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
}
})
最佳答案
引用docs about content scripts:
因此,要使用chrome.cookies
API,您需要从后台页面进行操作,并在需要时与内容脚本进行通信。