21

I am trying to get a cookie specifically from a domain using this code:

<script language="javascript" type="text/javascript">

var ID;

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

getCookies("http://www.example.com", "id")
alert(ID);

</script>

The problem is that the alert always says undefined. However, if I change

ID = cookie.value;

to

alert(cookie.value);

it works properly. How do I save the value to use later?

Update: It appears that if I call alert(ID) from the chrome console after the script runs, it works. How can I set my code to wait until chrome.cookies.get finishes running?

Franz Payer
  • 4,069
  • 15
  • 53
  • 77

3 Answers3

40

Almost all Chrome API calls are asynchronous, so you need to use callbacks to run code in order:

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

//usage:
getCookies("http://www.example.com", "id", function(id) {
    alert(id);
});
serg
  • 109,619
  • 77
  • 317
  • 330
  • 1
    How can I modify it to save it as a variable? When i tried ID = id instead of alert(id), i still got undefined. – Franz Payer May 05 '11 at 03:23
  • 2
    @DazSlayer All your code that relies on asynchronous calls should be rewritten using callbacks. So if you need this ID in some other place you need to put that code inside a callback as well. Code here is not executed line by line. If you worked with ajax it is the same story, just think if you are sending ajax requests to chrome api. – serg May 05 '11 at 03:33
  • note that you should add **"manifest_version": 2** into **manifest.json** – Alex Mar 02 '13 at 23:40
  • where must I call getCookie (also where do i place that code)? in my background.js or my content.js? or can I put it in another script? i have an app.html which opens up like "chrome-extension://efmbliijihpahlhiamhmmfkbjkecofec/app.html" – Dean Van Greunen Jul 15 '21 at 08:47
5

Any code that depends on the result of the call to chrome.cookies.get() will have to be invoked from within the callback. In your example, just wait for the callback to fire before you show the alert:

<script language="JavaScript" type="text/javascript">

    var ID;

    function getCookies(domain, name) 
    {
        chrome.cookies.get({"url": domain, "name": name}, function(cookie) {
            ID = cookie.value;
            showId();
        });
    }

    function showId() {
        alert(ID);
    }

    getCookies("http://www.example.com", "id")        

</script>
David Mills
  • 2,385
  • 1
  • 22
  • 25
  • where must I call getCookie (also where do i place that code)? in my background.js or my content.js? or can I put it in another script? i have an app.html which opens up like "chrome-extension://efmbliijihpahlhiamhmmfkbjkecofec/app.html" – Dean Van Greunen Jul 15 '21 at 08:45
1

To get all cookies under a domain:

 const r = await chrome.cookies.getAll({ domain: domain }) //domain: stackoverflow.com

This script can be run from popup and background service worker but not a content script.

GorvGoyl
  • 42,508
  • 29
  • 229
  • 225