views:

58

answers:

2

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?

+1  A: 

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);
BrunoLM
Just to clarify, did you try using "resource" as the answer on your link says (I ask because you linked to the question, not the answer, so I'm not completely sure what you're referring to by that.)
MatrixFrog
Yea, I tried the accepted solution over there, but it didn't work.
BrunoLM
Interesting. The resource: uri worked for me, but now I'm worried that it might fail for some users. What version of Firefox are you using?
MatrixFrog
@MatrixFrog: Latest Stable
BrunoLM
In that case, I would speculate that you did indeed mess up the resource URI somehow. On the other hand, if your way is working for you, I don't see much reason to change it now...
MatrixFrog
+1  A: 

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.

Felix Kling