Last active
July 20, 2021 12:52
-
-
Save coraxster/e5275429ab0971539626c1b4f1897537 to your computer and use it in GitHub Desktop.
Remove Facebook Workplace 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 Workplace external link tracking | |
| // @description https://gist.github.com/coraxster/e5275429ab0971539626c1b4f1897537/edit | |
| // @include https://*.workplace.com* | |
| // @include http://*.workplace.com* | |
| // @version 0.0.1 | |
| // @grant unsafeWindow | |
| // @run-at document-start | |
| // ==/UserScript== | |
| (function() { | |
| var redirUrl = new URL('https://l.workplace.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"); | |
| for (let k of url.searchParams.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.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); | |
| }; | |
| document.oncopy = function(event) { | |
| var el = findLinkUpwards(event.target); | |
| if (!el) return; | |
| fixLink(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"); | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment