Amazon URL Simplifier

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

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 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);
})();