I want to remove a Cookie (according to some criteria) from within a Chrome Extension.
According to the documentation of chrome.cookies.remove
it expects an object with the fields url
(The URL associated with the cookie) and name
(The name of the cookie to remove).
Now a cookie has the following fields: name, value, domain, hostOnly, path, secure, httpOnly, session, expirationDate, storeId
but no url
. How do I get the URL of a specific cookie so I can remove it?
For reference one of my cookies looks like this:
domain: ".google"
expirationDate: 1364393586
hostOnly: false
httpOnly: false
name: "PREF"
path: "/"
secure: false
session: false
storeId: "0"
value: "ID=8<snip>u"
I want to remove a Cookie (according to some criteria) from within a Chrome Extension.
According to the documentation of chrome.cookies.remove
it expects an object with the fields url
(The URL associated with the cookie) and name
(The name of the cookie to remove).
Now a cookie has the following fields: name, value, domain, hostOnly, path, secure, httpOnly, session, expirationDate, storeId
but no url
. How do I get the URL of a specific cookie so I can remove it?
For reference one of my cookies looks like this:
domain: ".google."
expirationDate: 1364393586
hostOnly: false
httpOnly: false
name: "PREF"
path: "/"
secure: false
session: false
storeId: "0"
value: "ID=8<snip>u"
Share
Improve this question
asked Mar 28, 2011 at 14:45
MottiMotti
115k56 gold badges194 silver badges274 bronze badges
2 Answers
Reset to default 7After some trial and error here's how I get the URL, this seems to work for everything (except perhaps file://
)
function extrapolateUrlFromCookie(cookie) {
var prefix = cookie.secure ? "https://" : "http://";
if (cookie.domain.charAt(0) == ".")
prefix += "www";
return prefix + cookie.domain + cookie.path;
}
I passed http://www.google.
as url and it worked. Maybe it just wants any url that matches domain
pattern.
chrome.cookies.remove({url:"http://www.google.", name: "PREF"});
(you also need to have domain permission for google.)