views:

333

answers:

4

Hi,

I'm creating an extension for the Firefox browser. I would like to read a cookie which was set by an HTML page using JavaScript in the XUL file. Is it possible?

I tried using document.cookie, but it doesn't work:

function readCookie(name) {
  var ca = document.cookie.split(';');
  var nameEQ = name + "=";
  for(var i=0; i < ca.length; i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1, c.length); //delete spaces
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
  return "";
}

function createCookie(name, value, days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
    }
  else var expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function eraseCookie(name) {
  createCookie(name, "", -1);
}

Could you help me? Thanks in advance.

+3  A: 

You probably want to use the nsICookieService interface: https://developer.mozilla.org/en/Code%5Fsnippets/Cookies

(Found via the helpful search on Mozilla's Add-on Developer Hub: https://addons.mozilla.org/en-US/developers/search?q=cookie)

brianpeiris
Thank you for the reply. The links were useful :)
kanda
+1  A: 

You also might want to look at existing extensions that work with cookies, such as FireCookie.

Michael Paulukonis
Thank you for the reply.Do you know how to see the code of an extension bcoz its in xpi format
kanda
you're starting to get into a new question.... an .xpi is just zipped. open it with an extractor, and look at http://en.wikipedia.org/wiki/XPInstall to make your own, zip up the files, and change the extension.
Michael Paulukonis
A: 

There are at least two distinct ways of doing this:

Firstly, if you simply want to interact with the Firefox cookie store, you can use the nsICookieManager and nsICookieManager2 interfaces to query, add and remove cookies.

Secondly, if you're more comfortable with the document.cookie approach, and you want to deal with documents which are already loaded in the browser, you can do this, but the important thing to remember is to get the right document to work with. When you simply use document in XUL code, you are referring to the document object associated with the browser window in which you are running. Several pages might be loaded in different tabs within this window, each of which will have its own document object. As a result, you first need to find the document object that you are interested in. One way of doing this, for example, is to register to be notified of pages loads, and then to examine pages as they load to see whether they are of interest. For example, your XUL code might look like this:

function pageLoad (event) {
  if (event.originalTarget instanceof HTMLDocument) {
    var doc = event.originalTarget;

    if (doc.URL.match(/^https?:\/\/[^\/]*\.example\.com\//)) {
      ⋮
      doc.cookie = 'cookie_name' + "=" + value + ";expires=" +
                   (new Date(2050, 10, 23)).toGMTString();
      ⋮
    }
  }
}

var appcontent = document.getElementById("appcontent");  // locate browser

if (appcontent)
  appcontent.addEventListener("load", function (e) { pageLoad(e); }, true);

With this approach you can interact with just those cookies associated with that page using the mechanisms with which you are familiar, and without worrying about dealing with cookies associated with completely different pages.

Tim
+9  A: 

These code snippets may help

///By this method you can iterate throug each cookie by its name or value... 

function ReadCookie()   
{
var cookieMgr = Components.classes["@mozilla.org/cookiemanager;1"]
         .getService(Components.interfaces.nsICookieManager);

for (var e = cookieMgr.enumerator; e.hasMoreElements();) {
 var cookie = e.getNext().QueryInterface(Components.interfaces.nsICookie); 
  dump(cookie.host + ";" + cookie.name + "=" + cookie.value + "\n");

 ///Your code here to check your cookie or your implementation...
 ///You can use cookie name or value to select your cookie...

}
}

///If you want to read cookies by domain name you can use this code...

function GetCookie(){
try
{
alert("Getting Cookies");
    var ios = Components.classes["@mozilla.org/network/io-service;1"]
            .getService(Components.interfaces.nsIIOService);
var uri = ios.newURI("http://www.yoursite.com/", null, null);

var cookieSvc =
   Components.classes["@mozilla.org/cookieService;1"]
             .getService(Components.interfaces.nsICookieService);
var cookie = cookieSvc.getCookieString(uri, null);

///Your logic here...
}
catch (errorInfo)
{
    alert(errorInfo);
}

}

Hope these will help :)

sumit_programmer