How To Dynamically Modify Css Rule Set (e.g. With A Class Selector) From Javascript Within Firefox Add-on Using Xul, Sdk Or Webextensions Techniques?
Solution 1:
5) There's things like document.styleSheets which doesn't seem to get what I want (unless I am mistaken?). Everything seems read-only.
This is the correct option. While the styleSheets
property is read-only (meaning you can't assign like this: document.styleSheets = val
), the stylesheet object you get does allow modifications.
Since you only care about one (modern!) browser, this is easier than Changing a CSS rule-set from Javascript in a cross-browser way. A trivial example:
functionchangeCSS() {
let myClassRule = document.styleSheets[0].cssRules[0];
if (myClassRule.style.color == "red")
myClassRule.style.color = "blue";
else
myClassRule.style.color = "red";
}
.my-class {
color: red;
}
<pclass="my-class">First paragraph</p><pclass="my-class">Second paragraph</p><buttononclick="changeCSS()">Change the stylesheet!</button>
You'll have to extend it to find the stylesheet and the rule you care about - the question linked above has a minimal example of doing that.
Solution 2:
3) There's the style sheet service, but I don't know if it's still in existence or will be obsoleted or removed (it's XPCOM based?), it's not part of Services.jsm, and it is a very blunt instrument that can merely load or unload an entire style sheet, when I merely want to modify one rule from one class definition.
Currently the other addon APIs are abstractions around the sheet service or windowutils.loadSheet. Of course mozilla may eventually remove it while maintaining the abstractions through other means. But as things look right now that still year(s) off in the the future.
The addon-sdk provides the option to load styles with its page-mod module
Webextensions support it either imperatively via tabs.insertCSS or declaratively in the manifest
But those are all meant for content frames. If you want to modify the browser chrome itself you'll have to use the xpcom APIs anyway, since there are no dedicated addon APIs targeting that.
Post a Comment for "How To Dynamically Modify Css Rule Set (e.g. With A Class Selector) From Javascript Within Firefox Add-on Using Xul, Sdk Or Webextensions Techniques?"