-
-
Save gewill/95812a963239c8ef45634cdf93bf7b8b to your computer and use it in GitHub Desktop.
Remove utm parameters and medium hash tracking code from url / 去除网页链接中恼人的跟踪参数,网页内容可复制等
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 | |
// @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(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
修复match不可用的问题
// @match *://*/*