Remove URL Tracking Parameters

移除網址中的跟踪參數

Stan na 15-09-2024. Zobacz najnowsza wersja.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name         Remove URL Tracking Parameters
// @namespace    http://tampermonkey.net/
// @version      1.3
// @description  移除網址中的跟踪參數
// @author abc0922001
// @match        *://*/*
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    // 使用 Set 來存儲跟踪參數
    const trackingParams = new Set([
        'utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',
        'fbclid', 'gclid', 'yclid', 'mc_cid', 'mc_eid', 'dclid','from'
    ]);

    function removeTrackingParams(url) {
        const urlObj = new URL(url);
        let params = urlObj.searchParams;
        let removed = false;

        for (let param of trackingParams) {
            if (params.has(param)) {
                params.delete(param);
                removed = true;
            }
        }

        return removed ? urlObj.toString() : url;
    }

    function cleanURL() {
        try {
            const currentUrl = window.location.href;
            const cleanedUrl = removeTrackingParams(currentUrl);

            if (cleanedUrl !== currentUrl) {
                window.history.replaceState({}, document.title, cleanedUrl);
            }
        } catch (error) {
            console.error('Error processing URL:', error);
        }
    }

    // 初始清理
    cleanURL();

    // 監聽 URL 變化
    window.addEventListener('popstate', cleanURL);

    // 對於單頁應用,可以考慮額外監聽自定義事件
    // 例如:window.addEventListener('routechange', cleanURL);
})();