Google chrome 为什么内容脚本中没有定义chrome.cookies?
每当我尝试使用Google chrome 为什么内容脚本中没有定义chrome.cookies?,google-chrome,google-chrome-extension,content-script,Google Chrome,Google Chrome Extension,Content Script,每当我尝试使用chrome.cookies.get()函数读取cookie时,我都会遇到以下错误: TypeError: Cannot read property 'get' of undefined. 我在content.js文件中调用该函数,它将只在twitter.com上运行(该部分有效) 这是我的清单文件: { "manifest_version": 2, "name": "Twitter-MultiSignin", "description": "twiter sign
chrome.cookies.get()
函数读取cookie时,我都会遇到以下错误:
TypeError: Cannot read property 'get' of undefined.
我在content.js文件中调用该函数,它将只在twitter.com上运行(该部分有效)
这是我的清单文件:
{
"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页面上发出警报,这样就可以正常工作了):
引述:
[内容脚本无法]使用chrome.*API(chrome.extension部分除外)
因此,要使用
chrome.cookies
API,您需要从后台页面执行此操作,并在需要时与内容脚本进行通信。如果有人跳过此操作,请确保将“cookies”添加到权限中。那么我将如何实现它,因此,当用户打开Twitter时,它将获得cookies?@Oxygen-API意味着不需要打开Twitter就可以查询cookies。它们绑定到特定的URL,而不是实际打开的选项卡。你需要添加一个背景脚本,你可以随时从那里查询。哦,好的,但我只想在Twitter页面加载上阅读cookies,然后能够从Twitter页面中获取一些信息。有没有关于如何在后台脚本中实现的想法?@Oxygen-两者都可以。背景中与Cookie相关的内容,加载到Twitter中的内容脚本,并通过用户导航到的sendMessage
向背景发送信号。
$(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
}
})