Skip to content

Instantly share code, notes, and snippets.

@k-barton
Last active September 27, 2023 16:58
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save k-barton/6af2dfb466a1f2b69e37 to your computer and use it in GitHub Desktop.
Save k-barton/6af2dfb466a1f2b69e37 to your computer and use it in GitHub Desktop.
Greasemonkey user script: Remove Facebook's external link tracking
// ==UserScript==
// @name Remove Facebook's external link tracking
// @description Removes redirection and the "click identifier" from external links on FBs
// @namespace https://gist.github.com/k-barton
// @match https://*.facebook.com/*
// @version 0.3.7
// @grant unsafeWindow
// @run-at document-start
// ==/UserScript==
(function() {
var redirUrl = new URL('https://l.facebook.com/l.php');
var findLinkUpwards = function(el) {
var test;
while (el && (test = el.tagName.toUpperCase() !== "A"))
el = el.parentElement;
return test ? undefined : el;
}
var fixLink = function(el) {
var url = new URL(el.href);
if (url.host.endsWith(document.location.host)) return false;
var onclick = el.hasAttribute('onclick');
if (el.hasAttribute('onclick'))
el.removeAttribute('onclick');
if (el.hasAttribute('onmouseover'))
el.removeAttribute('onmouseover');
if (el.onclick) el.onclick = null;
if (el.onmouseover) el.onmouseover = null;
if (url.host === redirUrl.host &&
url.pathname === redirUrl.pathname &&
url.searchParams.has("u")) {
url = new URL(decodeURIComponent(url.searchParams.get("u")));
}
if (url.searchParams.has("fbclid"))
url.searchParams.delete("fbclid");
var rmKey = [];
if(url.search) { // && (Array.from(url.searchParams).length !== 0)) {
for (let [k, v] of url.searchParams)
if (k.startsWith("utm_")) rmKey.push(k);
for(let i = 0; i < rmKey.length; ++i) url.searchParams.delete(rmKey[i]);
}
//console.debug("Original link: \n" + el.href + "\nModified link: \n" + url.href);
el.href = url.href;
return true;
};
var linkClick = function(el) {
// alert(el.href); // DEBUG
window.open(el.href, '_blank');
// TODO: open link in background with CTRL/middle click
};
var markFixedElement = function(el) {
if(el.classList.contains("cleaned_link"))
return;
el.classList.add("cleaned_link");
};
document.onclick = function(event) {
var el = findLinkUpwards(event.target);
if (!el) return;
if (!fixLink(el)) return;
event.preventDefault();
markFixedElement(el); // XXX: this may obscure image links, comment this line out if needed
linkClick(el);
};
var sheet = (function() {
var style = document.createElement("style");
style.setAttribute("media", "screen");
style.appendChild(document.createTextNode("")); // WebKit hack
document.head.appendChild(style);
return style.sheet;
})();
sheet.insertRule(".cleaned_link { background-color: #FFEE2233 !important; }");
sheet.insertRule(".cleaned_link::after { content: '☺'; }");
//console.log("Loaded script: remove facebook tracking");
})();
@masterchop
Copy link

tahnks for this script, unfortunatelly its not working. facebook still redirecting to that l.facebook.com domain. I have been trying to bypass it for a while with no luck do you know why is so hard to get rid of?

@k-barton
Copy link
Author

@masterchop the script was meant for the old fb. I haven't updated it for the new interface yet.

@masterchop
Copy link

thanks for the reply, i would be waiting for an update i havent figure out how to do it on the new one.

@k-barton
Copy link
Author

@masterchop updated for the new Facebook.

@masterchop
Copy link

Thank you very much :)

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