Skip to content
Advertisement

Can’t figure why this code don’t edit the ‘href’ attributes in FB pages

I wonder why this code don’t edit the facebook href attributes.

I’m pretty sure it should works.

I get error in console Error: Promised response from onMessage listener went out of scope

The code:

// ==UserScript==
// @name         facebook anti tracking URL
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  remove FB tracking
// @author       MévatlavéKraspek
// @match        https://www.facebook.com/*
// @grant        none
// ==/UserScript==


(function() {
    'use strict';
    for (let a of document.querySelectorAll('a')) {
        try {
            var old_url = a.getAttribute('href');
            if (old_url.match(/l.facebook/)) {
                var myRegexp = /.*l.facebook.com/l.php?u=(.*)%3Ffbclid.*/;
                var match = myRegexp.exec(old_url);
                var n = decodeURIComponent(match[1]);
                a.setAttribute('href', n);
            }
        } catch(e) {
            true;
        }
    }

})();

Answer

I think you have one semi-colon that is causing a problem.

(function() {
    'use strict';
    for (let a of document.querySelectorAll('a')) {
        try {
            var old_url = a.getAttribute('href');
            if (old_url.match(/l.facebook/)) {
                var myRegexp = /.*l.facebook.com/l.php?u=(.*)%3Ffbclid.*/;
                var match = myRegexp.exec(old_url);
                var n = decodeURIComponent(match[1]);
                a.setAttribute('href', n);
            }
        } catch(e) {
            true;
        };  // <---- remove this semi-colon
    }

})();

I ran the following at facebook.com (in the dev console) and it worked:

 for (let a of document.querySelectorAll('a')) {
            try {
                var old_url = a.getAttribute('href');
                console.log(old_url);
            } catch(e) {
                true;
            }
        }

Since this code runs, it probably means that the problem is related to your regex.

Advertisement