I am writing a Firefox extension and I am using their Add-on SDK; but I can't figure out how to inject a local CSS file from the data folder into the webpage. It would be great if there were a way to do it via page_mod
package.

- 300
- 3
- 15

- 496
- 9
- 18
2 Answers
As of Add-on SDK 1.14 there's experimental (API may change) support for this in the page-mod module:
var pageMod = require("sdk/page-mod").PageMod({
include: "*",
contentStyleFile: require("sdk/self").data.url("my-style.css")
});
See Modifying Web Pages Based on URL for an elaborate guide to using page-mod.
There's a page on the Addon SDK's wiki discussing issues with the current implementation, although it seems a bit outdated.
Under the hood it uses nsIDOMWindowUtils.loadSheet() to add the stylesheet without touching the page's DOM. (This API was added in Firefox 18, see bug 737003. Before that you had to use nsIStyleSheetService which was similar, but not tab-specific.)
Before that you could use the page-mod's content script to insert the link or style element (example). [edit] thanks to lwburk's comment, here's a more elaborate elaborate description in Greasemonkey Hacks: Tips & Tools for Remixing the Web with Firefox By Mark Pilgrim: "Alter a Page's Style" section.

- 31,095
- 13
- 107
- 185
-
It is impossible for me to use XPCOM since i am using the appbuilder SDK – italiano40 Dec 04 '11 at 17:58
-
1@italiano40: you keep referencing "appbuilder SDK". If you mean add-on SDK, please use the correct term. It is possible to use XPCOM components in the add-on SDK (`require('chrome')`), although it's an advanced topic and will invalidate the forward compatibility guarantees the add-on SDK normally provides. – Nickolay Dec 04 '11 at 19:12
-
1See also: http://books.google.com/books?id=KWRE2C_S4YsC&pg=PA19&lpg=PA19&dq=mark+pilgrim+add+Global+Style&source=bl&ots=SnfX5Ad2WO&sig=CKoF9A0Ala96vphTeU4mK2dbMe4&hl=en&ei=GJ3iTpeJKKK2sQLBkMDvBQ&sa=X&oi=book_result&ct=result&resnum=1&ved=0CCQQ6AEwAA#v=onepage&q&f=false – Wayne Dec 09 '11 at 23:44
To insert CSS from main.js one can now use "page-mod":
var data = require("sdk/self").data;
var pageMod = require("sdk/page-mod");
pageMod.PageMod({
include: "*.org",
contentStyleFile: data.url("my-page-mod.css")
});

- 5,106
- 4
- 25
- 41
-
Thanks for mentioning this! I updated my answer for the newer version of the Addon SDK. – Nickolay May 01 '13 at 01:12