Why is chrome.cookies undefined in the content script?

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 call the function in the content.js file and it will only work on twitter.com (this 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 warns on a Twitter page that it 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 } }) 
+6
google-chrome google-chrome-extension content-script
source share
1 answer

Specifying Content Scripting Documents :

[Content scripts cannot] Use chrome. * API (excluding chrome.extension parts)

So, in order to use the chrome.cookies API, you need to do this from the man page, if necessary by linking to the content of the script.

+10
source share

All Articles