Skip to content

Instantly share code, notes, and snippets.

@adrianiainlam
Last active October 6, 2023 14:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adrianiainlam/37e4502357cb3601cb1592c0dc735293 to your computer and use it in GitHub Desktop.
Save adrianiainlam/37e4502357cb3601cb1592c0dc735293 to your computer and use it in GitHub Desktop.
// ==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
});
@Korb
Copy link

Korb commented Feb 24, 2023

Script not recognized as compatible with Tampermonkey 4.18.1 (January 17, 2023). Mozilla Firefox 111.0b5 (64-bit).

@adrianiainlam
Copy link
Author

@Korb Thanks for reporting the issue, I messed up the file extension. It is fixed now.

@Korb
Copy link

Korb commented Feb 24, 2023

Sumptuously! This is the only such script that successfully removes trackers from the links of the Augmented Steam browser add-on. Thank you!

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