Amazon Force Currency Selector (Interactive)

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

スクリプトをインストールするには、Tampermonkey, GreasemonkeyViolentmonkey のような拡張機能のインストールが必要です。

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

スクリプトをインストールするには、TampermonkeyViolentmonkey のような拡張機能のインストールが必要です。

スクリプトをインストールするには、TampermonkeyUserscripts のような拡張機能のインストールが必要です。

このスクリプトをインストールするには、Tampermonkeyなどの拡張機能をインストールする必要があります。

このスクリプトをインストールするには、ユーザースクリプト管理ツールの拡張機能をインストールする必要があります。

(ユーザースクリプト管理ツールは設定済みなのでインストール!)

このスタイルをインストールするには、Stylusなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus などの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus tなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

(ユーザースタイル管理ツールは設定済みなのでインストール!)

このスクリプトの質問や評価の投稿はこちら通報はこちらへお寄せください
// ==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);
    };

})();