What’s my bloody session ID

While having a wander over at xulplanet.com I stumbled across the Cookie Manager Interface (Interface to the Gecko engine that is).

We have a simple HTML page in work that we use to emulate Payment Service Providers[PSP] (like Paypal or SecureTrading). It’s a right pain to test as we have to edit the page each time we test a different site. We also have to find our session ID from that sites cookie if we want to test it.

I wrote a few functions to allow me to populate a select drop down with the cookie domains (and session IDs) for a filtered list of all my cookies. On Page load the drop down is genereated and onChange of that dropdown the forms are updated with the chosen sites SessionID and the forms actions is also changed. Pretty neat and saves me about 10 clicks everytime I need to test my payment emails or that the PSP integration is working.

[code lang=”javascript”]
function _loadCookies()
{

//need to ask for permission to look at the users cookies
try {
netscape.security.PrivilegeManager.enablePrivilege(“UniversalXPConnect”);
} catch (e) {
alert(“Permission to save file was denied.”);
}
var nsICookie = Components.interfaces.nsICookie;
var _cm = Components.classes[“@mozilla.org/cookiemanager;1”]
.getService(Components.interfaces.nsICookieManager);

var _ds = Components.classes[“@mozilla.org/intl/scriptabledateformat;1″]
.getService(Components.interfaces.nsIScriptableDateFormat);
var _hosts = {};
var e = _cm.enumerator;
var hostCount = { value: 0 };

while (e.hasMoreElements()) {
var cookie = e.getNext();
if (cookie && cookie instanceof Components.interfaces.nsICookie)
{

var strippedHost = _makeStrippedHost(cookie.host);
if (!(strippedHost in _hosts) || !_hosts[strippedHost])
{
_hosts[strippedHost] = { cookies : [],
rawHost : strippedHost,
level : 0,
open : false,
container : true };

++hostCount.value;
var c = _makeCookieObject(strippedHost, cookie);
_hosts[strippedHost].cookies.push(c);
}
}
else
break;
}

var _select = document.createElement(‘SELECT’)
for (var host in _hosts) {
var cookies = _hosts[host].cookies;
var hostName =_hosts[host].rawHost;
var rExp = /kf/gi;
var rExpResults = hostName.match(rExp);
if(rExpResults && rExpResults.length > 0)
{
for (var _cookie in cookies)
{
var _option = document.createElement(‘OPTION’)
_option.innerHTML = _hosts[host].rawHost;
_option.value = cookies[_cookie].value;
_select.appendChild(_option);
}

}
}
_select.id=”siteURLChange”;
_select.onchange = changeActionAndSessID;

var URLForm = document.getElementById(‘siteurl’);
var URLInput = document.getElementById(‘url_site’);
URLInput.style.display = “none”;
insertAfter(URLForm, _select, URLInput);
changeActionAndSessID();
}

function _makeStrippedHost(aHost)
{
var formattedHost = aHost.charAt(0) == “.” ? aHost.substring(1, aHost.length) : aHost;
return formattedHost.substring(0, 4) == “www.” ? formattedHost
.substring(4, formattedHost.length) : formattedHost;
}

function _makeCookieObject(aStrippedHost, aCookie)
{
var host = aCookie.host;
var formattedHost = host.charAt(0) == “.” ? host.substring(1, host.length) : host;
var c = { name : aCookie.name,
value : aCookie.value,
isDomain : aCookie.isDomain,
host : aCookie.host,
rawHost : aStrippedHost,
path : aCookie.path,
isSecure : aCookie.isSecure,
expires : aCookie.expires,
level : 1,
container : false };
return c;
}
[/code]

I hope this code helps someone wanting to view cookies from a certain domain or from a filtered list.