Skip to content

Instantly share code, notes, and snippets.

@hushin
Last active October 16, 2024 14:13
Show Gist options
  • Save hushin/9d55485d2bb4b4abcfec433ec157f551 to your computer and use it in GitHub Desktop.
Save hushin/9d55485d2bb4b4abcfec433ec157f551 to your computer and use it in GitHub Desktop.
// ==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());
}
})();
@hushin
Copy link
Author

hushin commented Sep 9, 2024

iframe とかでマッチするせいなのかたまに履歴が変になるページあるので余裕あったら見直す

@hushin
Copy link
Author

hushin commented Sep 11, 2024

修正済み

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment