Number Simplifier

Simplifies reading big numbers.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Number Simplifier
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Simplifies reading big numbers.
// @author       Paran
// @licence      MIT
// @match        *://*/*
// @grant        none
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// ==/UserScript==
(function() {
    'use strict';

    // Create a context menu item
    document.addEventListener('contextmenu', function(event) {
        let selectedText = window.getSelection().toString().trim();
        if (isValidNumber(selectedText)) {
            let numberInShortForm = convertNumberToShortForm(selectedText);
            showPopup(numberInShortForm, event.pageX, event.pageY);
        }
    });

    // Also handle double-click
    document.addEventListener('dblclick', function(event) {
        let selectedText = window.getSelection().toString().trim();
        if (isValidNumber(selectedText)) {
            let numberInShortForm = convertNumberToShortForm(selectedText);
            showPopup(numberInShortForm, event.pageX, event.pageY);
        }
    });

    function isValidNumber(text) {
        return /^[\d,.\$\£\€\¥\₹]+$/.test(text.replace(/,/g, '').replace(/[\$\£\€\¥\₹]/g, ''));
    }

    function convertNumberToShortForm(num) {
        num = parseFloat(num.replace(/,/g, '').replace(/[\$\£\€\¥\₹]/g, ''));
        if (num >= 1e12) {
            return (num / 1e12).toFixed(1) + 'T';
        } else if (num >= 1e9) {
            return (num / 1e9).toFixed(1) + 'B';
        } else if (num >= 1e6) {
            return (num / 1e6).toFixed(1) + 'M';
        } else if (num >= 1e3) {
            return (num / 1e3).toFixed(1) + 'K';
        } else {
            return num.toString();
        }
    }

    function showPopup(text, x, y) {
        let popup = document.createElement('div');
        popup.style.position = 'absolute';
        popup.style.left = `${x}px`;
        popup.style.top = `${y + 20}px`;
        popup.style.background = '#fff9c4';
        popup.style.border = '1px solid #ffd700';
        popup.style.padding = '10px';
        popup.style.borderRadius = '4px';
        popup.style.boxShadow = '0px 0px 10px rgba(255, 215, 0, 0.5)';
        popup.style.zIndex = '1000';
        popup.style.transform = 'translateX(-50%)';
        popup.innerText = text;

        let arrow = document.createElement('div');
        arrow.style.position = 'absolute';
        arrow.style.left = '50%';
        arrow.style.top = '-10px';
        arrow.style.width = '0';
        arrow.style.height = '0';
        arrow.style.borderLeft = '10px solid transparent';
        arrow.style.borderRight = '10px solid transparent';
        arrow.style.borderBottom = '10px solid #fff9c4';
        arrow.style.transform = 'translateX(-50%)';
        popup.appendChild(arrow);

        document.body.appendChild(popup);

        setTimeout(() => {
            popup.remove();
        }, 1000);
    }
})();