Search code examples
htmllocal-storagexulxulrunner

Html 5 local storage in XUL application


I am trying to implement html5 local storage in a standalone xul application, I would like to use local-storage because I think the Xpcom way looks to complicated.

I have read that for a Firefox extension you can do:

document.content.localStorage.setItem("myvariable", "myvalue");
var item = document.content.localStorage.getItem("myvariable");
document.getElementById("result").innerHTML=item;

But this does not work for my standalone application, so how do I do it?


Solution

  • I have found a workaround here.

    var url = "http://example.com";
    var ios = Components.classes["@mozilla.org/network/io-service;1"]
              .getService(Components.interfaces.nsIIOService);
    var ssm = Components.classes["@mozilla.org/scriptsecuritymanager;1"]
              .getService(Components.interfaces.nsIScriptSecurityManager);
    var dsm = Components.classes["@mozilla.org/dom/storagemanager;1"]
              .getService(Components.interfaces.nsIDOMStorageManager);
    
    var uri = ios.newURI(url, "", null);
    var principal = ssm.getCodebasePrincipal(uri);
    var storage = dsm.getLocalStorageForPrincipal(principal, "");
    
    storage.setItem("chromekey", "chromevalue");
    

    What it does is that it pretends that it is example.com so Mozilla lets it assess loaclstorage.