Price Rounder

Rounds prices ending in .90 or higher to the nearest whole number. Works on Amazon and most shopping sites I've tested.

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         Price Rounder
// @namespace    http://tampermonkey.net/
// @version      1.3
// @description  Rounds prices ending in .90 or higher to the nearest whole number. Works on Amazon and most shopping sites I've tested.
// @author       Tempo918
// @match        *://*/*
// @grant        GM_registerMenuCommand
// @grant        GM_getValue
// @grant        GM_setValue
// @license      MIT
// ==/UserScript==

(function () {
    'use strict';

    // === Settings ===
    const isEnabled = GM_getValue('roundingEnabled', true);

    GM_registerMenuCommand(isEnabled ? "Disable Price Rounding" : "Enable Price Rounding", () => {
        GM_setValue('roundingEnabled', !isEnabled);
        location.reload();
    });

    if (!isEnabled) return;

    // === Rounding Logic ===
    function shouldRound(num) {
        return num % 1 >= 0.90;
    }

    function roundPrice(num) {
        return shouldRound(num) ? Math.ceil(num) : num;
    }

    // === Amazon-Specific Handling ===
    function handleAmazonListPage() {
        const elems = document.getElementsByClassName("a-price-fraction");
        for (let i = 0; i < elems.length; i++) {
            const centsElem = elems[i];
            const wholeElem = centsElem.parentNode.querySelector(".a-price-whole");
            if (!wholeElem) continue;

            const price = parseFloat(wholeElem.textContent.replace(/[^\d]/g, '') + '.' + centsElem.textContent);
            const newPrice = roundPrice(price).toFixed(2);
            wholeElem.textContent = newPrice.slice(0, -3);
            centsElem.textContent = newPrice.slice(-2);
        }
    }

    function handleAmazonProductPage() {
        const elems = document.getElementsByClassName("apexPriceToPay");
        for (let i = 0; i < elems.length; i++) {
            const priceElem = elems[i].querySelector("span:nth-child(2)");
            if (!priceElem) continue;

            const price = parseFloat(priceElem.textContent.replace(/[^0-9.]/g, ''));
            priceElem.textContent = "$" + roundPrice(price).toFixed(2);
        }
    }

    // === Generic Price Rounding ===
    const priceRegex = /\b\d{1,3}(?:,\d{3})*(?:\.\d{2})\b/g;

    function isVisible(el) {
        return !!(el.offsetWidth || el.offsetHeight || el.getClientRects().length);
    }

    function roundPricesInNode(node) {
        if (node.nodeType === Node.TEXT_NODE && node.parentElement && isVisible(node.parentElement)) {
            const originalText = node.textContent;
            const newText = originalText.replace(priceRegex, match => {
                const num = parseFloat(match.replace(/,/g, ''));
                return shouldRound(num) ? Math.ceil(num).toString() : match;
            });
            if (newText !== originalText) {
                node.textContent = newText;
            }
        }
    }

    function scanPage() {
        const walker = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT, null, false);
        let node;
        while ((node = walker.nextNode())) {
            roundPricesInNode(node);
        }
    }

    // === Observe Dynamic Changes ===
    function observeMutations() {
        const observer = new MutationObserver(mutations => {
            for (const mutation of mutations) {
                mutation.addedNodes.forEach(node => {
                    if (node.nodeType === Node.TEXT_NODE) {
                        roundPricesInNode(node);
                    } else if (node.nodeType === Node.ELEMENT_NODE) {
                        node.querySelectorAll('*').forEach(el => {
                            el.childNodes.forEach(roundPricesInNode);
                        });
                    }
                });
            }
        });

        observer.observe(document.body, {
            childList: true,
            subtree: true
        });
    }

    // === Run Script ===
    window.addEventListener('load', () => {
        handleAmazonProductPage();
        handleAmazonListPage();
        scanPage();
        observeMutations();

        setInterval(() => {
            handleAmazonListPage();
        }, 2000);
    });
})();