Amazon Force Currency Selector (Interactive)

Forces the currency on Amazon and asks which currency to display, works on all Amazon domains

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

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

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name         Amazon Force Currency Selector (Interactive)
// @namespace    https://amazon.com/
// @version      1.1
// @license      MIT
// @description  Forces the currency on Amazon and asks which currency to display, works on all Amazon domains
// @match        https://*.amazon.*/*
// @run-at       document-start
// ==/UserScript==

(function() {
    // --- 1. Ask user for desired currency at page load ---
    let FORCED_CURRENCY = localStorage.getItem("amazonForcedCurrency");
    if (!FORCED_CURRENCY) {
        FORCED_CURRENCY = prompt("Enter your desired Amazon currency code (e.g., USD, COP, BRL, EUR):", "COP");
        if (FORCED_CURRENCY) {
            FORCED_CURRENCY = FORCED_CURRENCY.toUpperCase();
            localStorage.setItem("amazonForcedCurrency", FORCED_CURRENCY);
        } else {
            // Default to COP if user cancels
            FORCED_CURRENCY = "COP";
        }
    }

    const paramName = "currency";

    // List of main Amazon domains
    const AMAZON_DOMAINS = [
        ".amazon.com",
        ".amazon.com.br",
        ".amazon.com.mx",
        ".amazon.ca",
        ".amazon.co.uk",
        ".amazon.de",
        ".amazon.fr",
        ".amazon.it",
        ".amazon.es",
        ".amazon.in",
        ".amazon.co.jp",
        ".amazon.com.au"
    ];

    // --- 2. Force currency cookies every 500ms ---
    function forceCookies() {
        AMAZON_DOMAINS.forEach(domain => {
            document.cookie = `currencyPreference=${FORCED_CURRENCY}; path=/; domain=${domain}`;
            document.cookie = `i18n-prefs=${FORCED_CURRENCY}; path=/; domain=${domain}`;
        });
    }
    setInterval(forceCookies, 500);

    // --- 3. Rewrite any URL to include ?currency=YOUR_CURRENCY ---
    function ensureURLHasCurrency(url) {
        try {
            let u = new URL(url, location.href);
            if (u.searchParams.get(paramName) !== FORCED_CURRENCY) {
                u.searchParams.set(paramName, FORCED_CURRENCY);
            }
            return u.toString();
        } catch (e) {
            return url;
        }
    }

    // Modify all links on the page
    function fixLinks() {
        document.querySelectorAll("a[href]").forEach(a => {
            if (!a.__fixedCurrency) {
                a.href = ensureURLHasCurrency(a.href);
                a.__fixedCurrency = true;
            }
        });
    }
    document.addEventListener("DOMContentLoaded", fixLinks);
    document.addEventListener("mouseover", fixLinks);
    document.addEventListener("click", fixLinks);

    // --- 4. Intercept SPA URL changes ---
    const pushState = history.pushState;
    history.pushState = function() {
        arguments[2] = ensureURLHasCurrency(arguments[2]);
        return pushState.apply(this, arguments);
    };
    window.addEventListener("popstate", () => {
        if (!location.search.includes(`${paramName}=${FORCED_CURRENCY}`)) {
            location.replace(ensureURLHasCurrency(location.href));
        }
    });

    // --- 5. Intercept fetch/XHR to block Amazon attempts to change currency ---
    function interceptURL(original) {
        return function(url, ...rest) {
            return original.call(this, ensureURLHasCurrency(url), ...rest);
        };
    }
    window.fetch = interceptURL(window.fetch);

    const open = XMLHttpRequest.prototype.open;
    XMLHttpRequest.prototype.open = function(method, url, ...rest) {
        return open.call(this, method, ensureURLHasCurrency(url), ...rest);
    };

})();