javascriptcssfirefoxgecko

Dynamic generation of stylesheets added to Firefox?


One can use the nsIStyleSheetService in Firefox to load and register stylesheets which apply in a similar way to userstyle.css; they are applied to every page loaded. However, this service takes sheetURI, which is a URI pointing to a static file to be included as a stylesheet.

Is there a way I could actually generate this stylesheet in Javascript instead, on-the-fly? Or would there be another interface which allowed me to register a JS callback to output the desired stylesheet to apply, instead of a sheet URI?


Solution

  • Generate the CSS in JS, then base64 encode it and prepend data:text/css;base64,
    Then, use this as the URI.

    For example: body{background:red;}
    Becomes: data:text/css;base64,Ym9keXtiYWNrZ3JvdW5kOnJlZDt9