Remove Amazon Tracking Parameters (All Domains)

Aggressive approach to remove Amazon tracking parameters across all country domains

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

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

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name         Remove Amazon Tracking Parameters (All Domains)
// @namespace    http://tampermonkey.net/
// @version      2.0
// @description  Aggressive approach to remove Amazon tracking parameters across all country domains
// @include       *://*.amazon.*/*
// @grant        none
// @run-at       document-start
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    /**
     * Removes tracking from the current URL if it matches an Amazon product page
     * and rewrites history to a clean version of the URL.
     */
    function cleanAmazonUrl() {
        var currentURL = window.location.href;
        // Matches /dp/ASIN or /gp/product/ASIN with a 10-character code
        // The 'i' flag lets it match uppercase or lowercase letters/numbers
        var asinPattern = /\/(?:dp|gp\/product)\/([A-Z0-9]{10})(?:[/?]|$)/i;
        var match = asinPattern.exec(currentURL);
        if (match) {
            var asin = match[1];
            var newURL = window.location.protocol + '//' + window.location.host + '/dp/' + asin;
            if (newURL !== currentURL) {
                // Rewrite the URL in the address bar without triggering a page reload
                window.history.replaceState({}, document.title, newURL);
            }
        }
    }

    // --- Hook into history methods and popstate to catch all Amazon URL changes --- //

    var _pushState = history.pushState;
    var _replaceState = history.replaceState;

    // Override pushState
    history.pushState = function(state, title, url) {
        var ret = _pushState.apply(this, arguments);
        // After pushState is called, clean the URL
        setTimeout(cleanAmazonUrl, 10);
        return ret;
    };

    // Override replaceState
    history.replaceState = function(state, title, url) {
        var ret = _replaceState.apply(this, arguments);
        // After replaceState is called, clean the URL
        setTimeout(cleanAmazonUrl, 10);
        return ret;
    };

    // Also listen for popstate (back/forward browser buttons)
    window.addEventListener('popstate', function() {
        // Clean the URL after a brief delay
        setTimeout(cleanAmazonUrl, 10);
    });

    // Lastly, do an initial cleanup as early as possible (document-start)
    cleanAmazonUrl();
})();