Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is an old JS function that usually worked well for me (Before <a href="https://addons.mozilla.org/en-US/firefox/addon/stylish/" rel="nofollow">Stylish</a> and <a href="https://addons.mozilla.org/en-US/firefox/addon/greasemonkey/" rel="nofollow">Greasemonkey</a>).</p> <p>Note that plain JS has security restrictions from accessing some stylesheets. A FF add-on can get around that, but then you need to also beware of corrupting browser-chrome styles.</p> <pre><code>function replaceStyleRuleByName (sStyleName, sNewRule) { var iNumStyleSheets = document.styleSheets.length; var bDebug = 0; if (bDebug) console.log ('There are ' + iNumStyleSheets + ' style sheets.'); for (iStyleS_Idx=0; iStyleS_Idx &lt; iNumStyleSheets; iStyleS_Idx++) { var iNumRules = 0; var zStyleSheet = document.styleSheets[iStyleS_Idx]; if (zStyleSheet) { /*---WARNING! This next line can throw an uncaught exception! Error: uncaught exception: [Exception... "Access to restricted URI denied" code: "1012" nsresult: "0x805303f4 (NS_ERROR_DOM_BAD_URI)" location: ... ...] */ //--- try/catch for cross domain access issue. try { var zRules = zStyleSheet.cssRules; if (zRules) { iNumRules = zRules.length; } } catch (e) {// Just swallow the error for now. } } if (bDebug) console.log ("Style sheet " + iStyleS_Idx + " has " + iNumRules + " ACCESSIBLE rules and src: " + zStyleSheet.href); //for (var iRuleIdx=iNumRules-1; iRuleIdx &gt;= 0; --iRuleIdx) for (var iRuleIdx=0; iRuleIdx &lt; iNumRules; ++iRuleIdx) { if (zRules[iRuleIdx].selectorText == sStyleName) { zStyleSheet.deleteRule (iRuleIdx); if (bDebug) console.log (sNewRule); if (sNewRule != null) { zStyleSheet.insertRule (sStyleName + sNewRule, iRuleIdx); } //return; //-- Sometimes changing just the first rule is not enough. } } //--- Optional: Punt and add the rule, cold, to any accessible style sheet. if (iNumRules &gt; 0) { if (sNewRule != null) { try { zStyleSheet.insertRule (sStyleName + sNewRule, iRuleIdx); } catch(e) {// Just swallow the error for now. } } } } return; } </code></pre> <p><br> <strong>Sample Usage:</strong></p> <pre><code>replaceStyleRuleByName ('body', '{line-height: 1.5;}' ); replaceStyleRuleByName ('#adBox', '{display: none;}' ); replaceStyleRuleByName ('.BadStyle', null ); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload