Question

I am attempting to add a function to my Firefox extension to trigger an event to delete cookies from site B when a button on site A is clicked. Site A and B do not share a domain but site B is running in an iframe injected into site A. I need the click event in the Firefox content script to trigger an event either in the content script or the Firefox extension main to delete all of the cookies from site B.

I have the click listener assigned to the button and firing. I have already achieved this same effect in Google Chrome with an extension. I get an error about using components, but I could not find a solution to use instead of components. It only needs to work on Firefox 22+. I am using addon-sdk-1.14 to develop the extension.

ContentScript.js

function DeleteCookies() {
    var payload="Delete";
    self.port.emit("Delete", payload);
}

Main.js

var {Cc, Ci} = require("chrome");
pageMod.PageMod({
    include: "*",
    contentScriptFile: [ self.data.url("jquery-1.9.1.js")
                        ,self.data.url("script.js")],
    onAttach: function(worker) {
                  worker.port.on('Delete',function (){ DeleteCookies();});
              }
});

function DeleteCookies() {
    var cookieManager = Cc["@mozilla.org/cookiemanager;1"].getService(Ci.nsICookieManager);
    var domain= "siteB.com";
    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, cookie.blocked);
                cookie_count++;
            }
        }
    }
};
Was it helpful?

Solution

You can't access XPCOM from a content script. Use the port mechanism for communication between the content script and main.js, and do the cookie deletion from the latter.

OTHER TIPS

It appears the code that will correctly perform the task was edited into the question rather than be posted as an answer. Unfortunately, there appears to be a bug and it iterates over all cookies rather than just those in the domain from which you desire to delete. For the question Deleting cookies with JavaScript in Firefox extension, I modified the code in this question to be more efficient, and fixed the bug. Given that I already worked on the code, I am posting it here so that others don't need to go through figuring out that the code was edited into the question and finding the issues with the code.

The bug is that cookie.blocked is passed to cookieManager.remove() when there is no blocked attribute defined for nsICookie. What should be passed there is false, assuming that there is no intent to block cookies from that host. As it is, undefined is currently being passed.

Iterating over only those cookies from the host in question, rather than all cookies, is accomplished by using the nsICookieManager2 interface. Specifically the getCookiesFromHost() method.

The updated code is:

ContentScript.js

function DeleteCookies() {
    var payload="Delete";
    self.port.emit("Delete", payload);
}

Main.js

var {Cc, Ci} = require("chrome");
pageMod.PageMod({
    include: "*",
    contentScriptFile: [ self.data.url("jquery-1.9.1.js")
                        ,self.data.url("script.js")],
    onAttach: function(worker) {
                  worker.port.on('Delete',function (){ DeleteCookies();});
              }
});

var cookieManager = Cc["@mozilla.org/cookiemanager;1"].getService(Ci.nsICookieManager2);

function DeleteCookies() {
    DeleteAllCookiesForDomain("siteB.com");
};

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;
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top