1

I have searched a lot for deleting all or a specific cookie with JavaScript. There are lots of posts that say its not possible 100%, or you can not delete cookies with HttpOnly flag. Then the question is how the Cookies Manager+ Firefox extension can delete cookies with JavaScript? Or how the Delete All Cookies From JavaScript Chrome extension lets programmers delete cookies by sending postMessage to his extension?

I am developing a Firefox extension and need to delete some cookies from a website.

How do I delete cookies in a Firefox extension?

Makyen
  • 31,849
  • 12
  • 86
  • 121
nAviD
  • 2,784
  • 1
  • 33
  • 54
  • Firefox and Chrome extensions run in a different JavaScript environment with higher privileges. The limitations imposed on web content are not necessarily applicable. That's how. – Alexander O'Mara Feb 02 '16 at 00:21
  • Did you look at the documentation for Firefox extensions? – epascarello Feb 02 '16 at 01:09
  • Given that you have found examples of extensions that accomplish what you are concerned about, did you look at the code they are using to do the task? That should make it very clear how it is done. – Makyen Feb 02 '16 at 03:56
  • Related: [How do I delete cross site cookies with firefox 22+ extension?](http://stackoverflow.com/questions/17930499/how-do-i-delete-cross-site-cookies-with-firefox-22-extension). That question contains what appears to be fully functional code to delete cookies. – Makyen Feb 02 '16 at 04:04

2 Answers2

2

As Alexander O'Mara mentioned in a comment, Chrome and Firefox JavaScript extensions run in a context that has higher privileges than JavaScript that is included on webpages. At that higher privilege level they are permitted to make changes to cookies. Your confusion probably is that you are reading webpages that discuss what is possible for JavaScript that is running from a webpage.

While I have not tested it, the following code appears to do what you want:

var {Cc, Ci} = require("chrome");

function DeleteAllCookiesForDomain( domain ) {
    var cookieManager = Cc["@mozilla.org/cookiemanager;1"].getService(Ci.nsICookieManager);
    var iter = cookieManager.enumerator;
    var cookie_count = 0;
    while (iter.hasMoreElements()) {
        var cookie = iter.getNext();
        if (cookie instanceof Ci.nsICookie) {
            if (domain.indexOf(cookie.host.toUpperCase()) != -1) {
                cookieManager.remove(cookie.host, cookie.name, cookie.path, false);
                cookie_count++;
            }
        }
    }
    return cookie_count;
};

The above code is slightly modified from code found in How do I delete cross site cookies with Firefox 22+ extension?.

However, the above code is inefficient as it iterates over all cookies, not just those from the domain you are interested in deleting. The following uses the nsICookieManager2 interface to iterate only over those cookies for the domain which you are deleting:

//For SDK
var {Cc, Ci} = require("chrome");
var cookieManager = Cc["@mozilla.org/cookiemanager;1"].getService(Ci.nsICookieManager2);
//*/
/*//For restartless/bootstrap/overlay
Components.utils.import("resource://gre/modules/Services.jsm");
var cookieManager = Services.cookies;
//*/

function DeleteAllCookiesForDomain( domain ) {
    var iter = cookieManager.getCookiesFromHost(domain);
    var cookie_count = 0;
    while (iter.hasMoreElements()) {
        var cookie = iter.getNext();
        if (cookie instanceof Ci.nsICookie) {
            cookieManager.remove(cookie.host, cookie.name, cookie.path, false);
            cookie_count++;
        }
    }
    return cookie_count;
};
Community
  • 1
  • 1
Makyen
  • 31,849
  • 12
  • 86
  • 121
1

Just use browsingData API I found it very friendly and easy to use

In your background script

var removalOptions = {hostnames: ['example.com', 'www.example.com']};
var dataTypeSet = {cookies: true}; // , indexedDB: true, localStorage:true
browser.browsingData.remove(removalOptions , dataTypeSet);

Note: I used "browsingData" and "storage" permissions in the mainfest.json file of the extension

Accountant م
  • 6,975
  • 3
  • 41
  • 61