From 75417f5e3d32645859d94cec82255dc130ec4a2e Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 18:55:34 +0200 Subject: Adding upstream version 2020.10.7. Signed-off-by: Daniel Baumann --- src/js/firstparties/facebook.js | 56 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/js/firstparties/facebook.js (limited to 'src/js/firstparties/facebook.js') 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); +}); -- cgit v1.2.3