From 84495c1664b29a89ca7633926dd9b19b4ef33741 Mon Sep 17 00:00:00 2001
From: David Byers <david.byers@liu.se>
Date: Wed, 3 Feb 2021 13:41:57 +0100
Subject: [PATCH] Refactored. Compose support in Firefox.

---
 firefox/content.js     |  3 +--
 firefox/mutation.js    | 22 +++++++++++++++++-----
 shared/links.js        | 34 ++++++++++++++++++++++++++--------
 thunderbird/compose.js | 11 +----------
 thunderbird/display.js |  3 +--
 5 files changed, 46 insertions(+), 27 deletions(-)

diff --git a/firefox/content.js b/firefox/content.js
index ecbb37e..b462aa3 100644
--- a/firefox/content.js
+++ b/firefox/content.js
@@ -31,6 +31,5 @@ function addLinkPopup(link) {
     link.addEventListener('mouseleave', scheduleHidePopup, {passive: true});
 }
 
-
-fixAllTheLinks();
+mutationHandler();
 enableMutationObserver();
diff --git a/firefox/mutation.js b/firefox/mutation.js
index 781085a..96e231a 100644
--- a/firefox/mutation.js
+++ b/firefox/mutation.js
@@ -25,30 +25,42 @@
 let mutationObserver = null;
 
 
+/**
+ * Handle mutation events. Attempts to detect a composition pane and
+ * remove nasty links from it.
+ */
+function mutationHandler(mutationsList, observer) {
+    let contentNode = (document.getElementById('divRplyFwdMsg')
+		       || document.getElementById('x_divRplyFwdMsg'))?.nextElementSibling;
+
+    while (contentNode) {
+	removeAllTheLinks(contentNode);
+	contentNode = contentNode.nextElementSibling;
+    }
+    fixAllTheLinks(document.body);
+}
+
+
 /**
  * Enable the mutation observer, creating it is necessary.
  */
 function enableMutationObserver() {
-    console.log('enter enableMutationObserver');
     if (!mutationObserver) {
-	mutationObserver = new MutationObserver(withoutMutationObserver(fixAllTheLinks));
+	mutationObserver = new MutationObserver(withoutMutationObserver(mutationHandler));
     }
     mutationObserver.observe(document.body, {
 	childList: true,
 	subtree: true
     });
-    console.log('exit enableMutationObserver');
 }
 
 /**
  * Disable the mutation observer if it is enabled
  */
 function disableMutationObserver() {
-    console.log('enter disableMutationObserver');
     if (mutationObserver) {
 	mutationObserver.disconnect();
     }
-    console.log('exit disableMutationObserver');
 }
 
 /**
diff --git a/shared/links.js b/shared/links.js
index b277fd4..41a0572 100644
--- a/shared/links.js
+++ b/shared/links.js
@@ -56,7 +56,6 @@ function untangleLink(link) {
 		return decodeURIComponent(url);
 	    }
 	    catch (e) {
-		console.log(e);
 		return url;
 	    }
 	});
@@ -101,17 +100,36 @@ function getTextNodes(elem) {
 
 /**
  * Fix all the links in the document.
+ * @param {Element} root - DOM element in which to fix links.
  */
-function fixAllTheLinks() {
-    for (const link of document.links) {
-	// Untangle link text
-	for (const node of getTextNodes(link)) {
-	    node.textContent = untangleLink(node.textContent);
+function fixAllTheLinks(root) {
+    for (const link of root.getElementsByTagName('a')) {
+	if (link.href) {
+	    // Untangle link text
+	    for (const node of getTextNodes(link)) {
+		node.textContent = untangleLink(node.textContent);
+	    }
+
+	    // Create popup event handlers
+	    if (isTangledLink(link.href)) {
+		addLinkPopup(link);
+	    }
 	}
+    }
+}
 
-	// Create popup event handlers
+
+/**
+ * Remove all safe links in an element
+ * @param {Element} root - DOM element in which to fix links.
+ */
+function removeAllTheLinks(root) {
+    for (const link of root.getElementsByTagName('a')) {
 	if (isTangledLink(link.href)) {
-	    addLinkPopup(link);
+	    link.href = untangleLink(link.href);
 	}
     }
+    for (const textNode of getTextNodes(root)) {
+	textNode.textContent = untangleLink(textNode.textContent);
+    }
 }
diff --git a/thunderbird/compose.js b/thunderbird/compose.js
index f9a8c15..bf7a1c9 100644
--- a/thunderbird/compose.js
+++ b/thunderbird/compose.js
@@ -22,13 +22,4 @@
 // Compose script
 
 
-for (const link of document.links) {
-    if (isTangledLink(link.href)) {
-	link.href = untangleLink(link.href);
-    }
-}
-
-for (const node of getTextNodes(document)) {
-    node.textContent = untangleLink(node.textContent);
-}
-
+removeAllTheLinks(document.body);
diff --git a/thunderbird/display.js b/thunderbird/display.js
index 813f6b8..292ba3b 100644
--- a/thunderbird/display.js
+++ b/thunderbird/display.js
@@ -31,5 +31,4 @@ function addLinkPopup(link) {
     link.addEventListener('mouseleave', scheduleHidePopup, {passive: true});
 }
 
-
-fixAllTheLinks();
+fixAllTheLinks(document.body);
-- 
GitLab