CSS rules utilities

functions allowing to get the CSS rules applied to an element

As of 2020-01-15. See the latest version.

This script should not be not be installed directly. It is a library for other scripts to include with the meta directive // @require https://update.greasyfork.org/scripts/394970/765728/CSS%20rules%20utilities.js

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         CSS rules utilities
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  functions allowing to get the CSS rules applied to an element
// @author       CoStiC
// @match        *://*/*
// @grant        none
// ==/UserScript==

function findCssRules(el) {
    var sheets = document.styleSheets, ret = [];
    el.matches = el.matches || el.webkitMatchesSelector || el.mozMatchesSelector || el.msMatchesSelector || el.oMatchesSelector;
    for (var i in sheets) {
        var rules = sheets[i].rules || sheets[i].cssRules;
        for (var r in rules) {
            if (el.matches(rules[r].selectorText)) {
                ret.push(rules[r].cssText);
            }
        }
    }
    return ret;
}

function modCssRules(el, newRule) {
    var elHover = "";
    var sheets = document.styleSheets;
    if (el !== null) {
        el.matches = el.matches || el.webkitMatchesSelector || el.mozMatchesSelector || el.msMatchesSelector || el.oMatchesSelector;
        for (var sheet in sheets) {
            var rules = sheets[sheet].rules || sheets[sheet].cssRules;
            for (var rule in rules) {
                if (el.matches(rules[rule].selectorText)) {

                    for (let prop in newRule.cssNormal) { rules[rule].style[prop] = newRule.cssNormal[prop] }
                    if (typeof newRule.cssHover !== 'undefined') {
                        var rul = "";
                        for (let propHover in newRule.cssHover) {
                            rul += `${propHover}:${newRule.cssHover[propHover]} !important;`;
                        };
                        elHover = `${rules[rule].selectorText}:hover{${rul}}`;
                    }
                } else {};
            }
        }
    }

    if (elHover !== "") {
        let newHoverStyle = document.createElement('style'),
            hoverRule = document.createTextNode(elHover);
        newHoverStyle.appendChild(hoverRule);
        document.head.appendChild(newHoverStyle)
        //sheets[sheets.length - 1].insertRule(elHover);
    };
}