Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// ==UserScript==
// @name Remove URL redirectors and tracking params
// @namespace https://github.com/adrianiainlam
// @description Remove URL redirectors and tracking params
// @include *
// @version 2.1
// @downloadURL
// @updateURL
// @grant none
// ==/UserScript==
function isUrlWindowLocation(url) {
let winLoc = new URL(window.location);
return (url.origin == winLoc.origin &&
url.pathname == winLoc.pathname);
}
function main() {
for (let elem of document.querySelectorAll('a')) {
sanitizeURL(elem);
elem.addEventListener("click", sanitizeURLEventHandler);
elem.addEventListener("auxclick", sanitizeURLEventHandler);
elem.addEventListener("mouseover", sanitizeURLEventHandler);
}
}
function sanitizeURLEventHandler(e) {
sanitizeURL(e.currentTarget);
}
function sanitizeURL(elem) {
try {
let url = new URL(elem.href);
if (elem.href[0] == '#')
return;
if (isUrlWindowLocation(url) && elem.indexOf("#") >= 0)
return;
if (elem.hasAttribute('data-lynx-mode'))
elem.removeAttribute('data-lynx-mode');
if (elem.hasAttribute('data-lynx-uri'))
elem.removeAttribute('data-lynx-uri');
if(elem.href.indexOf("https://l.facebook.com/l.php") == 0) {
elem.href = decodeURIComponent(elem.href.split('l.php?u=')[1].split('&')[0]);
url = new URL(elem.href);
}
//if (url.protocol == "javascript:")
// return;
if (url.protocol != "http:" && url.protocol != "https:")
return;
let params = url.searchParams;
if (params.has('fbclid'))
params.delete('fbclid');
if (params.has('gclid'))
params.delete('gclid');
if (params.has('utm_source'))
params.delete('utm_source');
if (params.has('utm_medium'))
params.delete('utm_medium');
if (params.has('utm_campaign'))
params.delete('utm_campaign');
if (params.has('utm_term'))
params.delete('utm_term');
if (params.has('utm_content'))
params.delete('utm_content');
let paramStr = params.toString();
if (paramStr != "")
paramStr = '?' + paramStr;
elem.href = url.origin + url.pathname + paramStr + url.hash;
}
catch (err) {
// most likely new URL() failed
// do nothing
}
}
var obs = new MutationObserver(main);
obs.observe(document.body, {
"childList": true,
"subtree": true
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment