DartCounter Calculator

Support maths for DartCounter score entry.

目前為 2024-02-05 提交的版本,檢視 最新版本

// ==UserScript==
// @name         DartCounter Calculator
// @namespace    http://dartcounter.net/
// @version      1.0
// @license      MIT 
// @description  Support maths for DartCounter score entry.
// @match        http*://dartcounter.net/*
// @icon         https://dartcounter.net/favicon-32x32.png
// @grant        GM_addStyle
// ==/UserScript==

GM_addStyle("#score.w-32 { width: 32rem!important; }");

(function() {
    'use strict';

    var observer = new MutationObserver(function(mutations) {
        var scoreInput = document.getElementById("score");
        if (scoreInput) {
            scoreInput.type = 'text';
            scoreInput.disabled = false;
            scoreInput.setAttribute('autocomplete', 'off');
        }
    });
    observer.observe(document, {attributes: false, childList: true, characterData: false, subtree:true});

    document.addEventListener('keydown', (event) => {
        var scoreInput = document.getElementById("score");
        if (scoreInput && event.target == scoreInput) {
            if (event.keyCode == 13) {
                let calcScore = (str) => {
                    return Function(`'use strict'; return (${str})`)()
                };

                let scoreValue = scoreInput.value;
                if (scoreValue && scoreValue.length > 0) {
                    scoreInput.value = calcScore(scoreValue);
                    scoreInput.dispatchEvent(
                        new Event("input", { bubbles: true, cancelable: true })
                    );

                    event.stopPropagation();
                    scoreInput.dispatchEvent(new KeyboardEvent('keydown', {'key': 'Enter'}));
                }
            }
        }
    }, true);

})();