summaryrefslogtreecommitdiffstats
path: root/src/js/firstparties/facebook.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/js/firstparties/facebook.js')
-rw-r--r--src/js/firstparties/facebook.js56
1 files changed, 56 insertions, 0 deletions
diff --git a/src/js/firstparties/facebook.js b/src/js/firstparties/facebook.js
new file mode 100644
index 0000000..2c5d299
--- /dev/null
+++ b/src/js/firstparties/facebook.js
@@ -0,0 +1,56 @@
+/* globals findInAllFrames:false, observeMutations:false */
+// Adapted from https://github.com/mgziminsky/FacebookTrackingRemoval
+// this should only run on facebook.com, messenger.com, and
+// facebookcorewwwi.onion
+let fb_wrapped_link = `a[href*='${document.domain.split(".").slice(-2).join(".")}/l.php?']:not([aria-label])`;
+
+// remove all attributes from a link except for class and ARIA attributes
+function cleanAttrs(elem) {
+ for (let i = elem.attributes.length - 1; i >= 0; --i) {
+ const attr = elem.attributes[i];
+ if (attr.name !== 'class' && !attr.name.startsWith('aria-')) {
+ elem.removeAttribute(attr.name);
+ }
+ }
+}
+
+// Remove excessive attributes and event listeners from link a and replace
+// its destination with href
+function cleanLink(a) {
+ let href = new URL(a.href).searchParams.get('u');
+
+ // If we can't extract a good URL, abort without breaking the links
+ if (!window.isURL(href)) {
+ return;
+ }
+
+ let href_url = new URL(href);
+ href_url.searchParams.delete('fbclid');
+ href = href_url.toString();
+
+ cleanAttrs(a);
+ a.href = href;
+ a.rel = "noreferrer";
+ a.target = "_blank";
+ a.addEventListener("click", function (e) { e.stopImmediatePropagation(); }, true);
+ a.addEventListener("mousedown", function (e) { e.stopImmediatePropagation(); }, true);
+ a.addEventListener("mouseup", function (e) { e.stopImmediatePropagation(); }, true);
+ a.addEventListener("mouseover", function (e) { e.stopImmediatePropagation(); }, true);
+}
+
+// TODO race condition; fix waiting on https://crbug.com/478183
+chrome.runtime.sendMessage({
+ type: "checkEnabled"
+}, function (enabled) {
+ if (!enabled) {
+ return;
+ }
+
+ // unwrap wrapped links in the original page
+ findInAllFrames(fb_wrapped_link).forEach((link) => {
+ cleanLink(link);
+ });
+
+ // Execute redirect unwrapping each time new content is added to the page
+ observeMutations(fb_wrapped_link, cleanLink);
+});