Created
August 13, 2022 11:22
-
-
Save rzfury/70df586230badd2159dd54e8882edcd4 to your computer and use it in GitHub Desktop.
Prevent facebook for passing fbclid params when clicking external links. Also works with right-click(new tab, new window, etc), and Enter Key.
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 NO FACEBOOK's FBCLID | |
// @namespace http://tampermonkey.net/ | |
// @version 1.0 | |
// @description Prevent facebook for passing fbclid params when clicking external links | |
// @author RZFury | |
// @match https://web.facebook.com/* | |
// @icon https://www.google.com/s2/favicons?domain=facebook.com | |
// @grant none | |
// ==/UserScript== | |
window.onload = function() { | |
document.addEventListener('click', verifyFBCLID(onClickExternalLink)); | |
document.addEventListener('contextmenu', verifyFBCLID(onContextMenuExternalLink, false)); | |
document.addEventListener('keydown', verifyFBCLID(onKeyEnterExternalLink)); | |
} | |
function verifyFBCLID(callback, stopPropagation = true) { | |
return (e) => { | |
if(e.type == 'keydown') { | |
if(e.key != 'Enter') { | |
return; | |
} | |
} | |
let targetLink = e.target; | |
while(targetLink != null) { | |
if(targetLink.tagName == "A") break; | |
targetLink = targetLink.parentElement; | |
} | |
if(targetLink == null) return; | |
if(targetLink.href.includes("fbclid")) { | |
if(stopPropagation) { | |
e.preventDefault(); | |
e.stopPropagation(); | |
} | |
if(callback) { | |
callback(e, targetLink); | |
} | |
} | |
} | |
} | |
function onClickExternalLink(e, targetLink) { | |
const url = new URL(targetLink.href); | |
url.searchParams.delete('fbclid'); | |
const a = document.createElement('a'); | |
a.href = url.toString(); | |
a.target = '_blank'; | |
a.click(); | |
a.remove(); | |
} | |
function onContextMenuExternalLink(e, targetLink) { | |
let url; | |
if(targetLink.href.includes('l.php')) { | |
url = new URL(targetLink.href).searchParams.get('u'); | |
} | |
url = new URL(url); | |
url.searchParams.delete('fbclid'); | |
targetLink.href = url.toString(); | |
} | |
function onKeyEnterExternalLink(e, targetLink) { | |
let url = targetLink.href; | |
if(targetLink.href.includes('l.php')) { | |
url = new URL(targetLink.href).searchParams.get('u'); | |
} | |
url = new URL(url); | |
url.searchParams.delete('fbclid'); | |
const a = document.createElement('a'); | |
a.href = url.toString(); | |
a.target = '_blank'; | |
a.click(); | |
a.remove(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment