Amazon Affiliate Tag Clean Links

Creates clean Amazon affiliate links

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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