Remove URL Tracking Parameters

移除網址中的跟踪參數

当前为 2024-06-16 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Remove URL Tracking Parameters
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0
  5. // @description 移除網址中的跟踪參數
  6. // @author abc0922001
  7. // @match *://*/*
  8. // @grant none
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. // 定義要移除的跟踪參數
  16. const trackingParams = [
  17. 'utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',
  18. 'fbclid', 'gclid', 'yclid', 'mc_cid', 'mc_eid', 'dclid'
  19. ];
  20.  
  21. function removeTrackingParams(url) {
  22. const urlObj = new URL(url);
  23. let params = urlObj.searchParams;
  24. let removed = false;
  25.  
  26. trackingParams.forEach(param => {
  27. if (params.has(param)) {
  28. params.delete(param);
  29. removed = true;
  30. }
  31. });
  32.  
  33. return removed ? urlObj.toString() : null;
  34. }
  35.  
  36. function cleanURL() {
  37. const cleanedUrl = removeTrackingParams(window.location.href);
  38. if (cleanedUrl) {
  39. window.history.replaceState({}, document.title, cleanedUrl);
  40. }
  41. }
  42.  
  43. cleanURL();
  44.  
  45. // 監聽 URL 變化(例如單頁應用程序中的路由變化)
  46. const observer = new MutationObserver(cleanURL);
  47. observer.observe(document, { subtree: true, childList: true });
  48. })();