Skip to content

Instantly share code, notes, and snippets.

@AndySuen
Last active December 22, 2023 03:28
Show Gist options
  • Save AndySuen/09c480b2f90818b4fe0334e804e1dcc2 to your computer and use it in GitHub Desktop.
Save AndySuen/09c480b2f90818b4fe0334e804e1dcc2 to your computer and use it in GitHub Desktop.
Remove utm parameters and medium hash tracking code from url / 去除网页链接中恼人的跟踪参数,网页内容可复制等
// ==UserScript==
// @name Remove utm
// @version 0.2.3
// @description Remove utm parameters and medium hash tracking code from url, 去除网页链接中恼人的跟踪参数
// @updateURL https://gist.github.com/AndySuen/09c480b2f90818b4fe0334e804e1dcc2/raw/remove_utm.user.js
// @homepage https://gist.github.com/AndySuen/09c480b2f90818b4fe0334e804e1dcc2
// @author Andysuen
// @match *
// @run-at document-idle
// @grant none
// ==/UserScript==
(function(){
// remove utm_, facebook fb_, taobao spm, medium hash and etc tracking code from url
// 去除网页 URL 中类 utm_ 的跟踪参数
function removeUtm() {
let search, params, i, flag;
search = location.search.replace(/^\?/, '').split('&');
params = [];
if (!search) return;
i = search.length;
while(i--){
if(!search[i].match(/^utm_|^fb_action|^fb_source|^action_object_map|^action_type_map|^action_ref_map|^hmsr|^spm/)){
params.push(search[i]);
} else {
flag = true;
}
}
if (flag) {
let newSearch = params.length ? '?' + params.join('&') : '';
history.pushState('', '', location.pathname + newSearch + location.hash);
}
setTimeout(function() {
// remove medium hash tracking code
if (/^#\.[a-zA-Z0-9]{8,11}$/.test(location.hash)) {
history.pushState('', '', location.pathname + location.search);
}
}, 1500);
}
function removeAllEventListener(type) {
window.addEventListener(type, e=> {e.stopPropagation()}, true);
}
function domainSpecifiedAction() {
let domain = window.location.host;
if(domain.match(/\byouku|taobao|tmall\.com$/)){
// prevent link href url overwrite by taobao spm tracking code
// 阻止网页内链接在点击时动态添加(类淘宝)spm 的跟踪代码
setTimeout(function() {
['mousedown'].forEach(e => removeAllEventListener(e))
}, 1500);
}
if(domain.match(/\bmedium\.com$/)){
// remove annoying modal dialog
// 去除 medium 恼人的对话框
setTimeout(function() {
var btn = document.querySelector('.button--close');
if (btn) {btn.click();}
}, 500);
}
// if(domain.match(/\.(com|cn|net)$/)){
// // enable copy on body etc.
// // 使网页内容可复制
// setTimeout(function() {
// ['copy'].forEach(e => removeAllEventListener(e))
// }, 1000);
// }
}
removeUtm();
domainSpecifiedAction();
})();
@Korb
Copy link

Korb commented Feb 24, 2023

The script does not work with links pasted by the Augmented Steam browser extension. Links like https://steamdb.info/app/.../?utm_source=Steam&utm_medium=Steam&utm_campaign=SteamDB%20Extension and https://pcgamingwiki.com/api/appid.php?appid=...&utm_source=Steam&utm_medium=Steam&utm_campaign=SteamDB%20Extension remain unchanged. Mozilla Firefox 111.0b5 (64-bit), Tampermonkey 4.18.1 (January 17, 2023).

@AndySuen
Copy link
Author

AndySuen commented Mar 2, 2023

The script does not work with links pasted by the Augmented Steam browser extension...

It seems that the JavaScript code on the original webpage had a bug, which caused the execution to interrupt.

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