Search code examples
javascripthtmlxulrunner

How can I load a stylesheet dynamically in XULRunner?


When running in a web browser, I can do this to dynamically inject a stylesheet:

var link = document.createElement('link'),
    head = document.getElementsByTagName('HEAD')[0];

link.rel  = 'stylesheet';
link.type = 'text/css';
link.href = '/path/to/stylesheet.css';
head.appendChild(link);

Is there a way to do the same in XULRunner? I've seen you can use something called Components.interfaces.mozIJSSubScriptLoader to load JavaScript, but does the same capability exist for CSS?


Solution

  • You can try using the style sheet service. Something like:

    var sss = Cc["@mozilla.org/content/style-sheet-service;1"]
              .getService(Ci.nsIStyleSheetService);
    var ios = Cc["@mozilla.org/network/io-service;1"] .getService(Ci.nsIIOService);
    var uri = ios.newURI("chrome://pluginname/skin/notes.css", null, null);
    
    if(!sss.sheetRegistered(uri, sss.USER_SHEET)) {
        sss.loadAndRegisterSheet(uri, sss.USER_SHEET);
    }
    

    More examples.