views:

74

answers:

1

How to set cookie under my firefox addon?

function setCookie(name, value, expires, path, domain, secure) {
    document.cookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}
setCookie("foo", "bar");

This simple js is not set cookie in firefox addon, but in web-page work well.

+1  A: 

From the add-on you can use the cookie manager XPCOM:

    var ios = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
    var cookieUri = ios.newURI("http://www.yourplacewhereyouwanttosetthecookie.com/", null, null);
    var cookieSvc = Components.classes["@mozilla.org/cookieService;1"].getService(Components.interfaces.nsICookieService);

    cookieSvc.setCookieString(cookieUri, null, "your_key=your_value;", null);

You can find more details here:

https://developer.mozilla.org/en/Code_snippets/Cookies#Setting_a_cookie

fms
+1: This code really works, thanks frns :)
sumit_programmer