Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting CSS with a Firefox Extension

I'm building a Firefox extension that adds HTML elements to certain pages of a website. I want to have it insert a custom CSS file to style those elements. It works if I insert tags with the CSS right on the page, but that's a less than ideal solution.

Is there anyway to get it to load and parse a CSS file, as if I used the tag in the header, or am I stuck inlining it somehow?

like image 577
MattBelanger Avatar asked Apr 21 '10 17:04

MattBelanger


People also ask

How do I only use CSS in Firefox?

Method 1: This method uses Mozilla specific extension to add CSS property. This extension supply the CSS property only in Firefox browser. Method 2: The -moz-appearance CSS property is used in Gecko (Firefox) to display an element using platform-native styling based on the operating system's theme.

Which CSS property is not supported by the Firefox browser?

CSS flow relative overflow-block and overflow-inline properties are not supported by all browsers. CSS flow relative padding-block- and padding-inline- properties are not supported by all browsers. The page-break-after: CSS property values avoid, left and right are not supported by Firefox.

What are the Firefox extensions for CSS?

Mozilla CSS extensions. Jump to: Mozilla applications such as Firefox support a number of special Mozilla extensions to CSS, including properties, values, pseudo-elements and pseudo-classes, at-rules, and media queries. These extensions are prefixed with -moz-.

How to target the Firefox browser with CSS?

The targeted CSS will works only for targeted browser. Target the Firefox browser with CSS in many ways. Some of them are discussed below: Method 1: This method uses Mozilla specific extension to add CSS property. This extension supply the CSS property only in Firefox browser. <p>A Computer Science portal for geeks.

Why can't I inject CSS in Firefox?

This means that you can't inject CSS into any of the browser's built-in pages, such as about:debugging, about:addons, or the page that opens when you open a new empty tab. Firefox resolves URLs in injected CSS files relative to the CSS file itself, rather than to the page it's injected into.

What is Mozilla CSS?

Mozilla CSS extensions Mozilla applications such as Firefox support a number of special Mozilla extensions to CSS, including properties, values, pseudo-elements and pseudo-classes, at-rules, and media queries. These extensions are prefixed with -moz-. Mozilla-only properties and pseudo-classes (avoid using on websites)


1 Answers

chrome:// won't work because the page you are inserting into isn't allowed to access files outside of it's domain (including chrome URIs). This is true even you are the one inserting the link, because the link is still executed in the context of the target page. Instead you have two options:

You can define a resource protocol alias in your manifest and then use a resource URI to access the CSS. For example, the following chrome.manifest will allow you to insert the css as resource://myextresource/myfile.css:
content myext content/
resource myextresource content/css/
See MDN Chrome registration for more info. Also see How can a Firefox extension inject a local css file into a webpage? for a similar question.

Or, you can add the CSS as a USER_SHEET using the following. This will make your CSS available across all pages, so be sure you use very unique selectors. One caveat with this approach is that the page CSS has precedence over user sheets. You can use !important to override that, but the page CSS can still trump you if it uses !important as well.

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(url, null, null);
sss.loadAndRegisterSheet(uri, sss.USER_SHEET);
like image 167
studgeek Avatar answered Sep 21 '22 13:09

studgeek