How to set cookie under my firefox addon?

How to set a cookie for 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 does not set cookies in the Firefox addon, but it works well on the web page.

+4
source share
1 answer

From the add-in, you can use the XPCOM cookie manager:

  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); 

Here you can find more information:

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

+4
source

All Articles