DartCounter Calculator

Support maths for DartCounter score entry.

当前为 2024-03-04 提交的版本,查看 最新版本

// ==UserScript==
// @name         DartCounter Calculator
// @author       mrdarts180
// @namespace    http://dartcounter.net/
// @version      1.1
// @license      MIT
// @description  Support maths for DartCounter score entry.
// @match        http*://dartcounter.net/*
// @icon         https://dartcounter.net/favicon-32x32.png
// @require      https://unpkg.com/mathjs/lib/browser/math.js
// @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 math.evaluate(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);

})();