2

I am trying to use chrome.cookies.get function to retrieve cookies based on a domain passed and I am getting the following error. Has anyone encountered the same issue ? please advise.

Actual CODE:

function getCookies(domain, name, callback) {
    chrome.cookies.get({"url": domain, "name": name}, function(cookie) {
        if(callback) {
            callback(cookie.value);
        }
    });
}

ERROR:

index.js:167 Uncaught (in promise) TypeError: Cannot read property 'get' of undefined
    at getCookies (index.js:167)
Johnson Samuel
  • 2,046
  • 2
  • 18
  • 29
  • 4
    Possible duplicate of [Why is chrome.cookies undefined in a content script?](https://stackoverflow.com/questions/23038032/why-is-chrome-cookies-undefined-in-a-content-script) – Dash Winterson Jan 11 '18 at 05:10
  • Thanks @DashWinterson – Johnson Samuel Jan 11 '18 at 05:32
  • @DashWinterson I see only this when I look for chrome variable values , {loadTimes: ƒ, csi: ƒ, app: undefined, extension: undefined, i18n: undefined, …} Am I missing anything ? Please advise. – Johnson Samuel Jan 11 '18 at 06:07

1 Answers1

3

Content scripts cannot Use chrome.* APIs but some parts of chrome.extension. You can use chrome.cookies.get in some backgorund script and use the cookie as you want.

However, in content script you can user "document.cookie" to get the cookies as scripted here.

Here is the official documentation for content script.

If you are facing other issues than just reading the cookies please mention in your question so we can help you.

Community
  • 1
  • 1
Aefits
  • 3,399
  • 6
  • 28
  • 46
  • I was able to achieve it using background script because like you said content script has some challenges using chrome.cookie helper methods. Thanks :) – Johnson Samuel Jan 13 '18 at 17:01