Chat Calculator

Press Tab To Do Math

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Chat Calculator
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Press Tab To Do Math
// @author       AlphB
// @match        https://www.milkywayidle.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=milkywayidle.com
// @require      https://cdn.jsdelivr.net/npm/[email protected]/dist/bundle.min.js
// @license      CC0
// ==/UserScript==

(function () {
    document.addEventListener('keydown', function (event) {
        if (event.key === 'Tab' && document.activeElement.classList.contains('Chat_chatInput__16dhX')) {
            event.preventDefault();
            const inputNode = document.activeElement;
            const parser = new exprEval.Parser();
            const originalValue = inputNode.value.trim();
            let result;
            if (originalValue.endsWith("=")) {
                try {
                    result = originalValue + String(parser.evaluate(originalValue.substring(0, originalValue.length - 1)));
                } catch (e) {
                    return;
                }
            } else {
                try {
                    result = parser.evaluate(originalValue);
                } catch (e) {
                    return;
                }
            }
            const tracker = inputNode._valueTracker;
            const previousValue = tracker ? tracker.getValue() : originalValue;
            inputNode.value = result;
            if (tracker) {
                tracker.setValue(previousValue);
                inputNode.dispatchEvent(new Event("input", {bubbles: true}));
            }
        }
    });
})();