Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing a cookie from within a Chrome Extension

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.com"
expirationDate: 1364393586
hostOnly: false
httpOnly: false
name: "PREF"
path: "/"
secure: false
session: false
storeId: "0"
value: "ID=8<snip>u"
like image 871
Motti Avatar asked Mar 28 '11 14:03

Motti


People also ask

Can extensions steal cookies?

How could this API be abused? A malicious extension could steal cookies from sites the user visits. The API also exposes HttpOnly cookie data to the extension.

Can extensions read cookies?

With the Cookies API your extensions have access to capabilities similar to those used by websites to store and read cookies. The API's features give extensions the ability to store information on a site-by-site basis.

What does removing a cookie do?

When you delete cookies from your computer, you erase information saved in your browser, including your account passwords, website preferences, and settings. Deleting your cookies can be helpful if you share your computer or device with other people and don't want them to see your browsing history.


1 Answers

After 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;
}
like image 187
Motti Avatar answered Oct 12 '22 23:10

Motti