Skip to content Skip to sidebar Skip to footer

How To Set Custom Cookies Using Firefox Add-on Sdk (using Services From Firefox Add-on Sdk)

I am working on creating a Firefox Add-on SDK extension that would set custom cookies (two cookies with name and value attributes) on Firefox v41, with jpm. Essentially, if I open

Solution 1:

Within the Firefox Add-on SDK, you can gain access to Services using the following:

var { Services }  = require("resource://gre/modules/Services.jsm");

However, your use of Services.cookies.add() is erroneous in that you are not passing enough arguments to the method. Here is something that works:

var { Services }  = require("resource://gre/modules/Services.jsm");
let is_secure = false;
let is_http_only = false;
let is_session = false;
let expiry_date = Math.floor(Date.now()/1000 + 3600*24); //Time in seconds 1 day from now.Services.cookies.add(".host.example.com", "/cookie-path", "cookie_name", "cookie_value",
                     is_secure, is_http_only, is_session, expiry_date);

This will result in a cookie that looks like:

Example cookie panel

In order to use some methods of Services.cookies (nsICookieManager2) you will also need to require Chrome Authority. For instance, to enumerate through the cookies you could do:

var { Cc, Cu, Ci} = require("chrome");
let cookieEnumerator = Services.cookies.getCookiesFromHost("google.com");
while (cookieEnumerator.hasMoreElements()) {
  let cookie = cookieEnumerator.getNext().QueryInterface(Ci.nsICookie2); 
  console.log(cookie.host + ";" + cookie.name + "=" + cookie.value + "\n");
}

Post a Comment for "How To Set Custom Cookies Using Firefox Add-on Sdk (using Services From Firefox Add-on Sdk)"