keybr colored keyzones

show colored keyzones on keyboard while practicing

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

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

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

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.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name         keybr colored keyzones
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  show colored keyzones on keyboard while practicing
// @author       Akash Mugu
// @match        https://www.keybr.com/
// @grant        none
// ==/UserScript==

// window.onSelectorReady
window.onSelectorReady = function ready(selector, cb) {
    var document = window.document;
    var cbCalled = false;

    function check() {
        var el = document.querySelector(selector);

        if (el && !cbCalled) {
            cbCalled = true;
            cb(el);
        }
    }

    var observer = new MutationObserver(check);

    observer.observe(document.documentElement, {
        childList: true,
        subtree: true
    });

    check();
};

// window.observeAttrs
window.observeAttrs = function(el, attrName, cb) {
    var observer = new MutationObserver(function(mutationsList) {
        for (var i = 0; i < mutationsList.length; i++) {
            var mutation = mutationsList[i];
            if (
                mutation.type === "attributes" &&
                mutation.attributeName === attrName
            ) {
                cb();
            }
        }
    });

    observer.observe(el, { attributes: true });
};

// main
(function() {
    "use strict";

    // constants
    var keyboardSelector =
        "#root > section > div.Practice-keyboard.l--normal > div > svg > svg.Keyboard-layout";

    window.onSelectorReady(keyboardSelector, function(kb) {
        var keys = kb.querySelectorAll("svg.KeyboardKey");

        keys.forEach(function(el) {
            window.observeAttrs(el, "class", function() {
                var datazone = el.dataset.zone;
                var classStr = el.getAttribute("class");
                var classArr = classStr.split(" ");
                var classHasZone = classStr.includes("zone");

                function setDataZoneFromClass() {
                    el.dataset.zone = classArr.find(function(c) {
                        return c.includes("zone");
                    });
                }

                function setDataZoneToClass() {
                    el.setAttribute(
                        "class",
                        classArr.concat(datazone).join(" ")
                    );
                }

                if (classHasZone && !datazone) {
                    setDataZoneFromClass();
                }

                if (!classHasZone && datazone) {
                    setDataZoneToClass();
                }
            });
        });
    });
})();