CC: Split the profile history link into 2 links

Before there was a single link which pointed to the post history in all
forums. As this sometimes takes a long time to load, this commit splits
it into 2 links:

* A link to the post history in the current forum (loads faster)
* A link to the post history in all forums (loads slower)

Change-Id: Ifa3ed2340c5af2e87d75118eb6a66aba4f395e3c
diff --git a/src/_locales/ca/messages.json b/src/_locales/ca/messages.json
index 66a6349..b39a47a 100644
--- a/src/_locales/ca/messages.json
+++ b/src/_locales/ca/messages.json
@@ -85,7 +85,15 @@
   },
   "inject_previousposts": {
     "message": "Historial de publicacions",
-    "description": "Link shown in a user profile (in TW and the Community Console) which points to a search showing the user's posts and messages"
+    "description": "Link shown in a user profile (in TW) which points to a search showing the user's posts and messages"
+  },
+  "inject_previousposts_forum": {
+    "message": "Historial de publicacions (en aquest fòrum)",
+    "description": "Link shown in a user profile (in the Community Console) which points to a search showing the user's posts and messages in the current forum"
+  },
+  "inject_previousposts_all": {
+    "message": "Historial de publicacions (en tots els fòrums)",
+    "description": "Link shown in a user profile (in the Community Console) which points to a search showing the user's posts and messages in all forums"
   },
   "inject_profileindicator_loading": {
     "message": "Carregant...",
diff --git a/src/_locales/en/messages.json b/src/_locales/en/messages.json
index 329c373..3d558b4 100644
--- a/src/_locales/en/messages.json
+++ b/src/_locales/en/messages.json
@@ -85,7 +85,15 @@
   },
   "inject_previousposts": {
     "message": "Previous posts",
-    "description": "Link shown in a user profile (in TW and the Community Console) which points to a search showing the user's posts and messages"
+    "description": "Link shown in a user profile (in TW) which points to a search showing the user's posts and messages"
+  },
+  "inject_previousposts_forum": {
+    "message": "Previous posts (in this forum)",
+    "description": "Link shown in a user profile (in the Community Console) which points to a search showing the user's posts and messages in the current forum"
+  },
+  "inject_previousposts_all": {
+    "message": "Previous posts (in all forums)",
+    "description": "Link shown in a user profile (in the Community Console) which points to a search showing the user's posts and messages in all forums"
   },
   "inject_profileindicator_loading": {
     "message": "Loading...",
diff --git a/src/_locales/es/messages.json b/src/_locales/es/messages.json
index 891f0a5..6eedd25 100644
--- a/src/_locales/es/messages.json
+++ b/src/_locales/es/messages.json
@@ -85,7 +85,15 @@
   },
   "inject_previousposts": {
     "message": "Historial de publicaciones",
-    "description": "Link shown in a user profile (in TW and the Community Console) which points to a search showing the user's posts and messages"
+    "description": "Link shown in a user profile (in TW) which points to a search showing the user's posts and messages"
+  },
+  "inject_previousposts_forum": {
+    "message": "Historial de publicaciones (en este foro)",
+    "description": "Link shown in a user profile (in the Community Console) which points to a search showing the user's posts and messages in the current forum"
+  },
+  "inject_previousposts_all": {
+    "message": "Historial de publicaciones (en todos los foros)",
+    "description": "Link shown in a user profile (in the Community Console) which points to a search showing the user's posts and messages in all forums"
   },
   "inject_profileindicator_loading": {
     "message": "Cargando...",
diff --git a/src/common/content_scripts_injections.js b/src/common/content_scripts.js
similarity index 73%
rename from src/common/content_scripts_injections.js
rename to src/common/content_scripts.js
index 2067075..7186037 100644
--- a/src/common/content_scripts_injections.js
+++ b/src/common/content_scripts.js
@@ -14,3 +14,9 @@
   script.src = scriptName;
   document.head.appendChild(script);
 }
+
+function escapeUsername(username) {
+  var quoteRegex = /"/g;
+  var commentRegex = /<!---->/g;
+  return username.replace(quoteRegex, '\\"').replace(commentRegex, '');
+}
diff --git a/src/content_scripts/console_inject.js b/src/content_scripts/console_inject.js
index eda9960..14556b8 100644
--- a/src/content_scripts/console_inject.js
+++ b/src/content_scripts/console_inject.js
@@ -14,6 +14,20 @@
   };
 }
 
+function addProfileHistoryLink(node, type, query) {
+  var urlpart = encodeURIComponent('query=' + query);
+  var container = document.createElement('div');
+  container.style.margin = '3px 0';
+
+  var link = document.createElement('a');
+  link.setAttribute(
+      'href', 'https://support.google.com/s/community/search/' + urlpart);
+  link.innerText = chrome.i18n.getMessage('inject_previousposts_' + type);
+
+  container.appendChild(link);
+  node.querySelector('.main-card-content').appendChild(container);
+}
+
 function mutationCallback(mutationList, observer) {
   mutationList.forEach((mutation) => {
     if (mutation.type == 'childList') {
@@ -34,18 +48,16 @@
               node.parentNode.tagName == 'EC-USER') {
             var nameElement = node.querySelector('.name span');
             if (nameElement !== null) {
-              var name = nameElement.innerHTML;
-              var query = encodeURIComponent(
-                  '(creator:"' + name + '" | replier:"' + name + '") -forum:0');
-              var urlpart = encodeURIComponent('query=' + query);
-              var link = document.createElement('a');
-              link.setAttribute(
-                  'href',
-                  'https://support.google.com/s/community/search/' + urlpart);
-              link.innerText = chrome.i18n.getMessage('inject_previousposts');
-              node.querySelector('.main-card-content')
-                  .appendChild(document.createElement('br'));
-              node.querySelector('.main-card-content').appendChild(link);
+              var forumId =
+                  location.href.split('/forum/')[1].split('/')[0] || '0';
+
+              var name = escapeUsername(nameElement.innerHTML);
+              var query1 = encodeURIComponent(
+                  '(creator:"' + name + '" | replier:"' + name + '") forum:'+forumId);
+              var query2 = encodeURIComponent(
+                  '(creator:"' + name + '" | replier:"' + name + '") forum:any');
+              addProfileHistoryLink(node, 'forum', query1);
+              addProfileHistoryLink(node, 'all', query2);
             }
           }
         }
diff --git a/templates/manifest.gjson b/templates/manifest.gjson
index e1f6cdb..83cc8ba 100644
--- a/templates/manifest.gjson
+++ b/templates/manifest.gjson
@@ -13,7 +13,7 @@
   "content_scripts": [
     {
       "matches": ["https://support.google.com/s/community*"],
-      "js": ["common/content_scripts_injections.js", "content_scripts/console_inject.js"]
+      "js": ["common/content_scripts.js", "content_scripts/console_inject.js"]
     },
     {
       "matches": ["https://support.google.com/s/community*"],
@@ -26,7 +26,7 @@
     },
     {
       "matches": ["https://support.google.com/*/thread/*"],
-      "js": ["common/content_scripts_injections.js", "content_scripts/thread_inject.js"],
+      "js": ["common/content_scripts.js", "content_scripts/thread_inject.js"],
       "run_at": "document_end"
     },
     {