Amazon Affiliate Tag Clean Links

Creates clean Amazon affiliate links

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Amazon Affiliate Tag Clean Links
// @namespace    http://tampermonkey.net/
// @version      2.0
// @description  Creates clean Amazon affiliate links
// @match        *://*.amazon.com/*
// @grant        none
// @license      MIT
// @run-at       document-start
// ==/UserScript==

(function() {
    'use strict';

    const AFFILIATE_TAG = 'pokemon-restocks-20';

    function cleanAmazonUrl() {
        const url = new URL(window.location.href);

        // Extract the essential parts
        const pathMatch = url.pathname.match(/\/dp\/([A-Z0-9]+)/i) ||
                         url.pathname.match(/\/gp\/product\/([A-Z0-9]+)/i);

        if (pathMatch) {
            const asin = pathMatch[1];

            // Check if we need to preserve certain parameters
            const th = url.searchParams.get('th'); // For variation selection
            const psc = url.searchParams.get('psc'); // For product selection context

            // Build clean URL
            let cleanUrl = `${url.origin}/dp/${asin}?tag=${AFFILIATE_TAG}`;

            // Add back essential parameters if they exist
            if (th) cleanUrl += `&th=${th}`;
            if (psc) cleanUrl += `&psc=${psc}`;

            // Only redirect if the current URL is different
            if (window.location.href !== cleanUrl) {
                window.location.replace(cleanUrl);
            }
        }
    }

    cleanAmazonUrl();
})();