I am trying to get a cookie specifically from a domain using this code:
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);
});
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>