Add workaround for the issue with links

Links added by the extension to the Community Console recently started
being handled by the Community Console to load the view dynamically
instead of reloading the whole page. However, this introduced a bug for
links pointing to Community Console search pages, which sometimes took a
long time to load (see linked bug).

This change works around this issue by making the Console Community not
handle these link clicks, so they continue to work like before when
clicked (the page is loaded from scratch to show the search results).

Bug: twpowertools:77
Change-Id: Iaec85ba58ea8006fe18939fbd72bd6ad70382c97
diff --git a/src/common/commonUtils.js b/src/common/commonUtils.js
index ee31037..814ce29 100644
--- a/src/common/commonUtils.js
+++ b/src/common/commonUtils.js
@@ -15,3 +15,12 @@
 export function isEmpty(obj) {
   return Object.keys(obj).length === 0;
 }
+
+// Create a link element which isn't handled by the Community Console when
+// clicked. This is done by cancelling the event propagation in the beginning of
+// the bubbling phase.
+export function createImmuneLink() {
+  var a = document.createElement('a');
+  a.addEventListener('click', e => e.stopPropagation(), false);
+  return a;
+}
diff --git a/src/contentScripts/communityConsole/profileHistoryLink.js b/src/contentScripts/communityConsole/profileHistoryLink.js
index 7f06b3e..44c7ebb 100644
--- a/src/contentScripts/communityConsole/profileHistoryLink.js
+++ b/src/contentScripts/communityConsole/profileHistoryLink.js
@@ -1,3 +1,4 @@
+import {createImmuneLink} from '../../common/commonUtils.js';
 import {escapeUsername, getAuthUser} from '../../common/communityConsoleUtils.js';
 import {isOptionEnabled} from '../../common/optionsUtils.js';
 
@@ -12,7 +13,7 @@
   var container = document.createElement('div');
   container.style.margin = '3px 0';
 
-  var link = document.createElement('a');
+  var link = createImmuneLink();
   link.setAttribute(
       'href',
       'https://support.google.com/s/community/search/' + urlpart +
diff --git a/src/contentScripts/utilsCommon/unifiedProfiles.js b/src/contentScripts/utilsCommon/unifiedProfiles.js
index 82bc9c3..e1ba59f 100644
--- a/src/contentScripts/utilsCommon/unifiedProfiles.js
+++ b/src/contentScripts/utilsCommon/unifiedProfiles.js
@@ -1,5 +1,6 @@
 import {MDCTooltip} from '@material/tooltip';
 
+import {createImmuneLink} from '../../common/commonUtils.js';
 import {escapeUsername} from '../../common/communityConsoleUtils.js';
 import {isOptionEnabled} from '../../common/optionsUtils.js';
 import {createPlainTooltip} from '../../common/tooltip.js';
@@ -30,7 +31,7 @@
   var links = document.createElement('div');
   links.classList.add('TWPT-user-profile__user-links');
 
-  var a = document.createElement('a');
+  var a = isCommunityConsole ? createImmuneLink() : document.createElement('a');
   a.classList.add('TWPT-user-profile__user-link', 'TWPT-user-link');
   a.href = url;
   a.target = '_parent';
diff --git a/src/injections/profileIndicator.js b/src/injections/profileIndicator.js
index 94ce943..408c736 100644
--- a/src/injections/profileIndicator.js
+++ b/src/injections/profileIndicator.js
@@ -1,4 +1,5 @@
 import {CCApi} from '../common/api.js';
+import {createImmuneLink} from '../common/commonUtils.js';
 import {escapeUsername} from '../common/communityConsoleUtils.js';
 import {createPlainTooltip} from '../common/tooltip.js';
 
@@ -105,12 +106,12 @@
 
 // Create profile indicator dot with a loading state, or return the numPosts
 // badge if it is already created.
-function createIndicatorDot(sourceNode, searchURL, options) {
+function createIndicatorDot(sourceNode, searchURL, options, isCC) {
   if (options.numPosts) return document.querySelector('.num-posts-indicator');
   var dotContainer = document.createElement('div');
   dotContainer.classList.add('profile-indicator', 'profile-indicator--loading');
 
-  var dotLink = document.createElement('a');
+  var dotLink = isCC ? createImmuneLink() : document.createElement('a');
   dotLink.href = searchURL;
   dotLink.innerText = '●';
 
@@ -128,8 +129,8 @@
 }
 
 // Create badge indicating the number of posts with a loading state
-function createNumPostsBadge(sourceNode, searchURL) {
-  var link = document.createElement('a');
+function createNumPostsBadge(sourceNode, searchURL, isCC) {
+  var link = isCC ? createImmuneLink() : document.createElement('a');
   link.href = searchURL;
 
   var numPostsContainer = document.createElement('div');
@@ -219,7 +220,7 @@
     var userId =
         profileURL.pathname.split(isCC ? 'user/' : 'profile/')[1].split('/')[0];
 
-    var numPostsContainer = createNumPostsBadge(sourceNode, searchURL);
+    var numPostsContainer = createNumPostsBadge(sourceNode, searchURL, isCC);
 
     getProfile(userId, forumId)
         .then(res => {
@@ -270,7 +271,7 @@
   }
 
   if (options.indicatorDot) {
-    var dotContainer = createIndicatorDot(sourceNode, searchURL, options);
+    var dotContainer = createIndicatorDot(sourceNode, searchURL, options, isCC);
 
     // Query threads in order to see what state the indicator should be in
     getPosts(query, forumId)