Getting cookies in a google chrome extension

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?

14
задан Franz Payer 15 November 2015 в 12:16
поделиться