Remove Amazon Tracking Parameters (All Domains)

Aggressive approach to remove Amazon tracking parameters across all country domains

  1. // ==UserScript==
  2. // @name Remove Amazon Tracking Parameters (All Domains)
  3. // @namespace http://tampermonkey.net/
  4. // @version 2.0
  5. // @description Aggressive approach to remove Amazon tracking parameters across all country domains
  6. // @include *://*.amazon.*/*
  7. // @grant none
  8. // @run-at document-start
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. /**
  16. * Removes tracking from the current URL if it matches an Amazon product page
  17. * and rewrites history to a clean version of the URL.
  18. */
  19. function cleanAmazonUrl() {
  20. var currentURL = window.location.href;
  21. // Matches /dp/ASIN or /gp/product/ASIN with a 10-character code
  22. // The 'i' flag lets it match uppercase or lowercase letters/numbers
  23. var asinPattern = /\/(?:dp|gp\/product)\/([A-Z0-9]{10})(?:[/?]|$)/i;
  24. var match = asinPattern.exec(currentURL);
  25. if (match) {
  26. var asin = match[1];
  27. var newURL = window.location.protocol + '//' + window.location.host + '/dp/' + asin;
  28. if (newURL !== currentURL) {
  29. // Rewrite the URL in the address bar without triggering a page reload
  30. window.history.replaceState({}, document.title, newURL);
  31. }
  32. }
  33. }
  34.  
  35. // --- Hook into history methods and popstate to catch all Amazon URL changes --- //
  36.  
  37. var _pushState = history.pushState;
  38. var _replaceState = history.replaceState;
  39.  
  40. // Override pushState
  41. history.pushState = function(state, title, url) {
  42. var ret = _pushState.apply(this, arguments);
  43. // After pushState is called, clean the URL
  44. setTimeout(cleanAmazonUrl, 10);
  45. return ret;
  46. };
  47.  
  48. // Override replaceState
  49. history.replaceState = function(state, title, url) {
  50. var ret = _replaceState.apply(this, arguments);
  51. // After replaceState is called, clean the URL
  52. setTimeout(cleanAmazonUrl, 10);
  53. return ret;
  54. };
  55.  
  56. // Also listen for popstate (back/forward browser buttons)
  57. window.addEventListener('popstate', function() {
  58. // Clean the URL after a brief delay
  59. setTimeout(cleanAmazonUrl, 10);
  60. });
  61.  
  62. // Lastly, do an initial cleanup as early as possible (document-start)
  63. cleanAmazonUrl();
  64. })();