Debian Codegraph @

src/js/firstparties/facebook.js

raw
1 /* globals findInAllFrames:false, observeMutations:false */
2 // Adapted from https://github.com/mgziminsky/FacebookTrackingRemoval
3 // this should only run on facebook.com, messenger.com, and
4 // facebookcorewwwi.onion
5 let fb_wrapped_link = `a[href*='${document.domain.split(".").slice(-2).join(".")}/l.php?']:not([aria-label])`;
6
7 function stopPropagation(e) {
8 e.stopImmediatePropagation();
9 }
10
11 // remove all attributes from a link
12 function cleanAttrs(elem) {
13 for (let attr of elem.attributes) {
14 // with some exceptions
15 if (attr.name != 'href' && attr.name != 'class' && !attr.name.startsWith('aria-')) {
16 elem.removeAttribute(attr.name);
17 }
18 }
19 }
20
21 /**
22 * Cleans link elements:
23 *
24 * - removes link redirection/"unwraps" the href
25 * - removes fbclid parameter
26 * - removes non-essential attributes
27 * - stops mouse events
28 *
29 * Could be called multiple times on the same element.
30 *
31 * @param {Element} el the link element
32 */
33 function cleanLink(el) {
34 let cleaned = false,
35 url = new URL(el.href);
36
37 // remove link redirection
38 if (url.searchParams.has('u')) {
39 let u_param = url.searchParams.get('u');
40 if (window.isURL(u_param)) {
41 // now remove fbclid
42 let uParamUrl = new URL(u_param);
43 uParamUrl.searchParams.delete('fbclid');
44 el.href = uParamUrl.toString();
45 cleaned = true;
46 }
47
48 // just remove fbclid
49 } else if (url.searchParams.has('fbclid')) {
50 url.searchParams.delete('fbclid');
51 el.href = url.toString();
52 cleaned = true;
53 }
54
55 if (!cleaned) {
56 return;
57 }
58
59 cleanAttrs(el);
60
61 el.rel = "noreferrer noopener";
62 el.target = "_blank";
63
64 el.addEventListener("click", stopPropagation, true);
65 el.addEventListener("mousedown", stopPropagation, true);
66 el.addEventListener("mouseup", stopPropagation, true);
67 el.addEventListener("mouseover", stopPropagation, true);
68 }
69
70 // TODO race condition; fix waiting on https://crbug.com/478183
71 chrome.runtime.sendMessage({
72 type: "checkEnabled"
73 }, function (enabled) {
74 if (!enabled) {
75 return;
76 }
77
78 // unwrap wrapped links in the original page
79 findInAllFrames(fb_wrapped_link).forEach((link) => {
80 cleanLink(link);
81 });
82
83 // execute redirect unwrapping each time new content is added to the page
84 observeMutations(fb_wrapped_link, cleanLink);
85 });