Number Simplifier

Simplifies reading big numbers.

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

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

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==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);
    }
})();