Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get url associated with a cookie in chrome extension?

So I am using chrome.cookies.getAll({}, function(c){console.log(c);}) to get all the cookies stored on the system. However, if I need to process the resulting cookie to either delete or whatever, I need a URL associated with each cookie. The URL strangely is not in the cookie structure: http://developer.chrome.com/extensions/cookies.html#type-Cookie

Anyone know how to get the URL associated with a cookie?

like image 295
FurtiveFelon Avatar asked Oct 11 '25 21:10

FurtiveFelon


2 Answers

You can build the URL from the information you've got from getAll():

var cookie; // one single cookie from the array

var url = '';
// get prefix, like https://www.
url += cookie.secure ? 'https://' : 'http://';
url += cookie.domain.charAt(0) == '.' ? 'www' : '';

// append domain and path
url += cookie.domain;
url += cookie.path;

console.log(url); // something like "https://www.stackoverflow.com/"
like image 75
dan-lee Avatar answered Oct 15 '25 21:10

dan-lee


The domain property gives you the domain associated with the cookie. And path gives you the path inside that domain. From the Cookie API Test Extension:

function removeCookie(cookie) {
  var url = "http" + (cookie.secure ? "s" : "") + "://" + cookie.domain +
            cookie.path;
  chrome.cookies.remove({"url": url, "name": cookie.name});
}
like image 21
rsanchez Avatar answered Oct 15 '25 21:10

rsanchez