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:
JavaScript
x
29
29
1
// ==UserScript==
2
// @name facebook anti tracking URL
3
// @namespace http://tampermonkey.net/
4
// @version 0.1
5
// @description remove FB tracking
6
// @author MévatlavéKraspek
7
// @match https://www.facebook.com/*
8
// @grant none
9
// ==/UserScript==
10
11
12
(function() {
13
'use strict';
14
for (let a of document.querySelectorAll('a')) {
15
try {
16
var old_url = a.getAttribute('href');
17
if (old_url.match(/l.facebook/)) {
18
var myRegexp = /.*l.facebook.com/l.php?u=(.*)%3Ffbclid.*/;
19
var match = myRegexp.exec(old_url);
20
var n = decodeURIComponent(match[1]);
21
a.setAttribute('href', n);
22
}
23
} catch(e) {
24
true;
25
}
26
}
27
28
})();
29
Advertisement
Answer
I think you have one semi-colon that is causing a problem.
JavaScript
1
18
18
1
(function() {
2
'use strict';
3
for (let a of document.querySelectorAll('a')) {
4
try {
5
var old_url = a.getAttribute('href');
6
if (old_url.match(/l.facebook/)) {
7
var myRegexp = /.*l.facebook.com/l.php?u=(.*)%3Ffbclid.*/;
8
var match = myRegexp.exec(old_url);
9
var n = decodeURIComponent(match[1]);
10
a.setAttribute('href', n);
11
}
12
} catch(e) {
13
true;
14
}; // <---- remove this semi-colon
15
}
16
17
})();
18
I ran the following at facebook.com (in the dev console) and it worked:
JavaScript
1
9
1
for (let a of document.querySelectorAll('a')) {
2
try {
3
var old_url = a.getAttribute('href');
4
console.log(old_url);
5
} catch(e) {
6
true;
7
}
8
}
9
Since this code runs, it probably means that the problem is related to your regex.