Remove Facebook's external link tracking (updated for the new FB)

Removes redirection and the "click identifier" from external links on FB

目前为 2021-03-04 提交的版本。查看 最新版本

// ==UserScript==
// @name          Remove Facebook's external link tracking (updated for the new FB)
// @description   Removes redirection and the "click identifier" from external links on FB
// @namespace     https://gist.github.com/k-barton
// @include       https://*.facebook.com*
// @include       http://*.facebook.com*
// @version       0.3.3
// @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");
        for (let k of url.searchParams.keys())
            if (k.startsWith("utm_"))
                url.searchParams.delete(k);

         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
    };

    // TODO: improve the "cleaned link" style
    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); // XXX: this may obscure image links, comment this line out if needed
        linkClick(el);
    }

})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址