views:

610

answers:

3

I am working on a Firefox add-on that will allow users (all of whom are part of a specific group; this add-on is very limited in audience scope) to see the status of their authentication cookie from the status bar. We all have to authenticate to access work-related sites, but we get no warning when the cookie expires, so this causes annoying and sometimes drastic interrupts in workflow. Eventually, this add on will allow us to submit our credentials from the status bar without having to go to do any reloads or redirects, but for now, I just want to see it show the status.

I have been looking at the Mozilla developer pages at nsICookie, nsICookie2, nsICookieManager, etc, and it doesn't make very clear sense how any of it fits into javascript or XUL or anything else.

Ideally, I'd just like a way for the javascript to go outside of the document and get the cookie string for a domain I specify. If I could do that, it would allow the code to possibly be ported over to other browsers (Safari and Chrome, in particular). But if this must be browser specific, then I would at least like to know the method for checking if the cookie exists in Firefox without any bells and whistles of setting or removing.

Simply put, I want a way to say:

 if cookieExists("sample.com", CookieName) {
      alert('You're signed in!');
 else {
      alert('Go sign in, you fool!');
      }

What is the easiest/most-portable way of doing this (browser-side, of course)?

Thanks!

+4  A: 

you can use jquery plugin for cookie handling

http://www.stilbuero.de/2006/09/17/cookie-plugin-for-jquery/

or simply through javascript : http://www.quirksmode.org/js/cookies.html

Vikram
I didn't see anyway to get the cookies for another domain, though. But further reading suggests this isn't possible, for security reasons.
That is correct you can not access cookies outside of the current domain.
jtyost2
+1  A: 

Here's a nice tutorial for working with cookies in javascript. Using the functions from that tutorial, you could probably do something like this:

if readCookie(yourCookieName != "") {
      alert("You're signed in!");
 else {
      alert("Go sign in, you fool!");
}

Here are the cookie functions:

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);
}
Jose Basilio
+5  A: 

I have been looking at the Mozilla developer pages at nsICookie, nsICookie2, nsICookieManager, etc, and it doesn't make very clear sense how any of it fits into javascript or XUL or anything else.

access to all cookies from Firefox extension is possible and uses the nsICookieManager and nsICookie interfaces. From javascript code in your extension, you access the cookie manager with

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

and than you can iterate through all stored cookies

var enum = cookieManager.enumerator;
while (enum.hasMoreElements()){
   var cookie = enum.getNext();
   if (cookie instanceof Components.interfaces.nsICookie){
      // commands
   }
}

now, when having reference to cookie object you can check its properties

cookie.host
cookie.name
cookie.value
...

defined in nsICookie interface. This code is Firefox specific and can be run as a browser extension or signed script. Hope my explanation helped a bit.

Below I present some links on using JS XPCOM interfaces in extensions:

  1. JS XPCOM
  2. Using cookies
Rafael