为什么在内容脚本中未定义chrome.cookies? [英] Why is chrome.cookies undefined in a content script?

查看:557
本文介绍了为什么在内容脚本中未定义chrome.cookies?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无论何时我尝试使用 chrome.cookies.get()函数从cookie读取数据,我都会收到以下错误消息:

  TypeError:无法读取未定义的属性'get'。 

我在我的content.js文件中调用函数,它只会在twitter.com上运行(这部分工作)。



这是我的清单文件:

  {
的manifest_version:2,

的名称: Twitter的MultiSignin,
的描述: 在twiter征,
的版本: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
}
}

这是我的内容。 js(它总是在Twitter页面上发出警报,以便工作正常):

  $(function(){
alert ('got here');
尝试{
chrome.cookies.get({url:'https://twitter.com',name:'auth_token'} ,函数(cookie){
alert(cookie.value);
});
} catch(err){
//不做任何事
alert(err);

try {
chrome.cookies.get({url:'https://twitter.com',name:'twid'},
function(cookie){
如果(饼干){
twid_raw = cookie.value;
twid = twid_raw.split( '=')[1]
警报(twid);
}
else {
alert(not found);
}
});
} catch(err){
// do nothing
}
})


'的div类= h2_lin>解决方案

引用关于内容脚本文档


[内容脚本不能]使用chrome。* API(除了chrome.extension的部分)
< blockquote>

因此,要使用 chrome.cookies API,您需要从后台页面进行操作,与内容脚本进行通信如果需要的话。


Whenever I try to read from a cookie using the chrome.cookies.get() function I get this error:

TypeError: Cannot read property 'get' of undefined.

I am calling the function in my content.js file, and it will only run on twitter.com(that part works).

Here is my manifest file:

{
  "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"
  }
}

Here is my content.js (it always alerts on the twitter page so that works fine):

$(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
    }
})

解决方案

Quoting the docs about content scripts:

[Content scripts cannot] Use chrome.* APIs (except for parts of chrome.extension)

So, to use chrome.cookies API, you need to do so from a background page, communicating with the content script if needed.

这篇关于为什么在内容脚本中未定义chrome.cookies?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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