From c3c28d02373221eff25221bfcc791a3fe5477c67 Mon Sep 17 00:00:00 2001
From: David Byers <david.byers@liu.se>
Date: Sat, 20 Feb 2021 11:44:07 +0100
Subject: [PATCH] Add support for Google Chrome.

---
 .gitlab-ci.yml     | 26 ++++++++++----------------
 shared/links.js    | 13 +++++++++----
 shared/menu.js     |  9 +++++++++
 shared/mutation.js |  2 +-
 4 files changed, 29 insertions(+), 21 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 3304fbd..3a6e9c6 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -5,13 +5,18 @@ stages:
   - collect
   - deploy
 
+workflow:
+  rules:
+    - if: '$CI_COMMIT_TAG'
+      when: never
+    - if: '$CI_PIPELINE_SOURCE != "push"'
+      when: always
+    - when: never
 
 # Anchors
 
 .release_rules: &release_rules
   rules:
-    - if: '$CI_COMMIT_TAG'
-      when: never
     - if: '$CI_COMMIT_BRANCH != "master"'
       when: never
     - if: "$CI_COMMIT_MESSAGE =~ /#release/"
@@ -21,8 +26,6 @@ stages:
 # When changing, also make changes to copies in the build stage
 .beta_rules: &beta_rules
   rules:
-    - if: '$CI_COMMIT_TAG'
-      when: never
     - if: '$CI_COMMIT_BRANCH != "master"'
       when: never
     - if: "$CI_COMMIT_MESSAGE =~ /#release/"
@@ -31,8 +34,6 @@ stages:
 
 .dev_rules: &dev_rules
   rules:
-    - if: '$CI_COMMIT_TAG'
-      when: never
     - if: '$CI_COMMIT_BRANCH != "master"'
       when: always
     - when: never
@@ -103,8 +104,7 @@ build:edge:beta:
   extends: .build:edge
   variables:
     VARIANT: beta
-    - if: '$CI_COMMIT_TAG'
-      when: never
+  rules:
     - if: '$CI_COMMIT_BRANCH != "master"'
       when: never
     - if: "$CI_COMMIT_MESSAGE =~ /#release/"
@@ -151,8 +151,6 @@ build:firefox:beta:
     CHANNEL: unlisted
     VARIANT: beta
   rules:
-    - if: '$CI_COMMIT_TAG'
-      when: never
     - if: '$CI_COMMIT_BRANCH != "master"'
       when: never
     - if: "$CI_COMMIT_MESSAGE =~ /#release/"
@@ -164,9 +162,6 @@ build:firefox:beta:
         - firefox/**/*
       when: always
     - when: never
-  artifacts:
-    paths:
-      - safelinks-cleaner-thunderbird-$VARIANT.xpi
 
 build:firefox:release:
   extends: .build:firefox
@@ -196,8 +191,7 @@ build:thunderbird:beta:
   extends: .build:thunderbird
   variables:
     VARIANT: beta
-    - if: '$CI_COMMIT_TAG'
-      when: never
+  rules:
     - if: '$CI_COMMIT_BRANCH != "master"'
       when: never
     - if: "$CI_COMMIT_MESSAGE =~ /#release/"
@@ -208,7 +202,7 @@ build:thunderbird:beta:
         - shared/**/*
         - thunderbird/**/*
       when: always
-    - when: beta
+    - when: never
 
 build:thunderbird:release:
   extends: .build:thunderbird
diff --git a/shared/links.js b/shared/links.js
index 0a9fe5a..a3e6589 100644
--- a/shared/links.js
+++ b/shared/links.js
@@ -111,19 +111,24 @@ function addLinkPopup(link) {
 /**
  * Fix all the links in the document.
  * @param {Element} root - DOM element in which to fix links.
+ * @param {Element} is_owa - Set to true when fixing Outlook.
  */
-function fixAllTheLinks(root) {
+function fixAllTheLinks(root, is_owa) {
     console.log('enter fixAllTheLinks'); // DEBUG
     for (const link of root.getElementsByTagName('a')) {
-	console.log(link);	// DEBUG
 	if (link.href) {
+	    let textWasTangled = false;
+
 	    // Untangle link text
 	    for (const node of getTextNodes(link)) {
-		node.textContent = untangleLink(node.textContent);
+		if (isTangledLink(node.textContent)) {
+		    textWasTangled = true;
+		    node.textContent = untangleLink(node.textContent);
+		}
 	    }
 
 	    // Create popup event handlers
-	    if (isTangledLink(link.href)) {
+	    if (isTangledLink(link.href) && (!is_owa || textWasTangled)) {
 		addLinkPopup(link);
 	    }
 	}
diff --git a/shared/menu.js b/shared/menu.js
index 6dd91cd..8e5f4de 100644
--- a/shared/menu.js
+++ b/shared/menu.js
@@ -21,6 +21,15 @@
 
 // Context menu
 
+/*
+ * Minimal polyfill for Chrome
+ */
+if (typeof browser === "undefined") {
+    var browser = {
+	menus: chrome.contextMenus,
+	i18n: chrome.i18n,
+    }
+}
 
 browser.menus.create({
     id: "liu-safelinks-copy",
diff --git a/shared/mutation.js b/shared/mutation.js
index af84bc6..4080b6a 100644
--- a/shared/mutation.js
+++ b/shared/mutation.js
@@ -38,7 +38,7 @@ function mutationHandler(mutationsList, observer) {
 	removeAllTheLinks(contentNode);
 	contentNode = contentNode.nextElementSibling;
     }
-    fixAllTheLinks(document.body);
+    fixAllTheLinks(document.body, true);
     console.log('exit mutationHandler'); // DEBUG
 }
 
-- 
GitLab