-
-
Save nfekete/286fe631de230a316f3918805514eacb to your computer and use it in GitHub Desktop.
Greasemonkey user script: Remove Facebook's external link tracking
This file contains hidden or 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 Facebook's external link tracking (updated for the new FB) | |
// @description Removes redirection and "FB click identifier" from external FB links | |
// @namespace https://gist.github.com/k-barton | |
// @include https://*.facebook.com* | |
// @include http://*.facebook.com* | |
// @version 0.3.2 | |
// @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.path && | |
url.searchParams.has("u")) { | |
url = decodeURIComponent(url.searchParams.get("u")); | |
} | |
if (url.searchParams.has("fbclid")) | |
url.searchParams.delete("fbclid"); | |
var keys = Array.from(url.searchParams.keys()); | |
for (let k of keys) | |
if (k.startsWith("utm_")) | |
url.searchParams.delete(k); | |
//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'); | |
}; | |
var markFixedElement = function(el) { | |
if (el.textContent != "") | |
el.style.backgroundColor = "#FFEE22"; | |
var flagEl = document.createElement('div'); | |
flagEl.style.display = 'inline-block'; | |
flagEl.style.verticalAlign = 'top'; | |
flagEl.style.fontSize = "5 pt"; | |
flagEl.appendChild(document.createTextNode('☺')); | |
if (el.childElementCount === 0) { | |
el.appendChild(flagEl); | |
} else { | |
el.insertBefore(flagEl, el.firstChild); | |
} | |
} | |
document.onclick = function(event) { | |
var el = findLinkUpwards(event.target); | |
if (!el) return; | |
if (!fixLink(el)) return; | |
event.preventDefault(); | |
markFixedElement(el); | |
linkClick(el); | |
} | |
console.log("Loaded script: remove facebook tracking"); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment