CSS rules utilities

functions allowing to get the CSS rules applied to an element

As of 15.01.2020. See ბოლო ვერსია.

ეს სკრიპტი არ უნდა იყოს პირდაპირ დაინსტალირებული. ეს ბიბლიოთეკაა, სხვა სკრიპტებისთვის უნდა ჩართეთ მეტა-დირექტივაში // @require https://updategreasyfork.deno.dev/scripts/394970/765728/CSS%20rules%20utilities.js.

// ==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);
    };
}