Amazon URL Simplifier

Simplifies Amazon product page URLs to /dp/ASIN format.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name                Amazon URL Simplifier
// @name:ja             Amazon 商品URL 短縮ツール
// @namespace           https://greasyfork.org/users/1636783
// @version             1.0.0
// @description         Simplifies Amazon product page URLs to /dp/ASIN format.
// @description:ar      تبسيط عناوين URL لصفحات منتجات أمازون إلى صيغة /dp/ASIN.
// @description:de      Vereinfacht Amazon-Produktseiten-URLs auf das Format /dp/ASIN.
// @description:es      Simplifica las URL de las páginas de productos de Amazon al formato /dp/ASIN.
// @description:fr      Simplifie les URL des pages de produits Amazon au format /dp/ASIN.
// @description:it      Semplifica gli URL delle pagine dei prodotti Amazon nel formato /dp/ASIN.
// @description:ja      Amazonの商品ページURLを /dp/ASIN の形式に短縮・簡略化します。
// @description:nl      Vereenvoudigt Amazon-productpagina-URL's naar het /dp/ASIN-formaat.
// @description:pl      Upraszcza adresy URL stron produktów Amazon do formatu /dp/ASIN.
// @description:pt-BR   Simplifica as URLs das páginas de produtos da Amazon para o formato /dp/ASIN.
// @description:sv      Förenklar URL:er för Amazon-produktsidor till /dp/ASIN-format.
// @description:tr      Amazon ürün sayfası URL'lerini /dp/ASIN biçimine sadeleştirir.
// @author              miki33
// @license             MIT
// @icon                https://www.google.com/s2/favicons?sz=64&domain=amazon.com
// @match               https://www.amazon.com.au/*
// @match               https://www.amazon.com.be/*
// @match               https://www.amazon.com.br/*
// @match               https://www.amazon.ca/*
// @match               https://www.amazon.eg/*
// @match               https://www.amazon.fr/*
// @match               https://www.amazon.de/*
// @match               https://www.amazon.in/*
// @match               https://www.amazon.ie/*
// @match               https://www.amazon.it/*
// @match               https://www.amazon.co.jp/*
// @match               https://www.amazon.com.mx/*
// @match               https://www.amazon.nl/*
// @match               https://www.amazon.pl/*
// @match               https://www.amazon.sg/*
// @match               https://www.amazon.sa/*
// @match               https://www.amazon.es/*
// @match               https://www.amazon.se/*
// @match               https://www.amazon.com.tr/*
// @match               https://www.amazon.ae/*
// @match               https://www.amazon.co.uk/*
// @match               https://www.amazon.com/*
// @match               https://www.amazon.co.za/*
// @run-at              document-start
// @grant               none
// ==/UserScript==

(() => {
    'use strict';

    // Extract 10-character ASIN from Amazon product URL path
    const ASIN_RE = /\/(?:dp|gp\/product)\/([A-Z0-9]{10})(?:[/?]|$)/i;

    // Check and normalize URL to /dp/ASIN format
    const simplifyUrl = () => {
        const match = ASIN_RE.exec(location.pathname);
        if (!match) return;

        const asin = match[1];
        const normalizedPath = `/dp/${asin}`;

        // Rewrite URL if path is unnormalized or contains query parameters
        if (location.pathname !== normalizedPath || location.search !== '') {
            history.replaceState(history.state, '', normalizedPath + location.hash);
        }
    };

    // Run on page load and periodically check for dynamic parameter changes
    simplifyUrl();
    setInterval(simplifyUrl, 250);
})();