Last active
October 16, 2024 14:13
-
-
Save hushin/9d55485d2bb4b4abcfec433ec157f551 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Remove UTM and fbclid Parameters | |
// @namespace http://tampermonkey.net/ | |
// @version 1.3 | |
// @description Remove UTM and fbclid parameters from URL and navigate to clean URL | |
// @author hushin | |
// @match *://*/* | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
// Check if the script is running in the top-level window | |
if (window.top !== window.self) { | |
return; // Exit if not in the top-level window | |
} | |
// Get the current URL | |
let url = new URL(window.location.href); | |
// List of tracking parameters to remove | |
const trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid']; | |
// Remove each tracking parameter if it exists | |
trackingParams.forEach(param => url.searchParams.delete(param)); | |
// If the URL changed, navigate to the clean URL using pushState | |
// NOTE query param で value がないページが末尾 = がつくのでそれは除去する | |
if (url.toString().replace(/=$/, '') !== window.location.href) { | |
window.history.pushState({}, document.title, url.toString()); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
iframe とかでマッチするせいなのかたまに履歴が変になるページあるので余裕あったら見直す