Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inject CSS located on /skin?

I want to inject a css file located on the skin folder in a browser page.

It is located on chrome://orkutmanager/skin/om.css, accessing manually show the file contents correctly.

I've tried this, but it's not working... What am I missing, or is it impossible?

like image 337
BrunoLM Avatar asked Nov 25 '25 16:11

BrunoLM


2 Answers

You can also use the nsIStyleSheetService:

loadCSS: function() {
     var sss = Components.classes["@mozilla.org/content/style-sheet-service;1"]
                .getService(Components.interfaces.nsIStyleSheetService);
     var ios = Components.classes["@mozilla.org/network/io-service;1"]
                .getService(Components.interfaces.nsIIOService);
     var uri = ios.newURI("chrome://addon/skin/style.css", null, null);

     if(!sss.sheetRegistered(uri, sss.USER_SHEET))
         sss.loadAndRegisterSheet(uri, sss.USER_SHEET);
}

If you use USER_SHEET, the website's own CSS rules have higher priority than yours. Using AGENT_SHEET, your CSS should have higher priority.
In any way I needed to enforce some rules by using hte !important keyword.

like image 186
Felix Kling Avatar answered Nov 28 '25 06:11

Felix Kling


I found this workaround. Read the file then inject it's contents...

function Read(file)
{
    var ioService=Components.classes["@mozilla.org/network/io-service;1"]
        .getService(Components.interfaces.nsIIOService);
    var scriptableStream=Components
        .classes["@mozilla.org/scriptableinputstream;1"]
        .getService(Components.interfaces.nsIScriptableInputStream);

    var channel=ioService.newChannel(file,null,null);
    var input=channel.open();
    scriptableStream.init(input);
    var str=scriptableStream.read(input.available());
    scriptableStream.close();
    input.close();
    return str;
}

var style = $("<style type='text/css' />");
style.html(Read("chrome://orkutmanager/skin/om.css"));
$("head").append(style);
like image 43
BrunoLM Avatar answered Nov 28 '25 07:11

BrunoLM



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!