Refactor content scripts/injections bridge

In order to use the existent bridge with more features other than the
profile indicator, this change refactors the bridge code so the content
script event listener can handle requests being done via different
prefixes.

Change-Id: I7c8e7e62068b7326a2eeec18d871a44114d6c259
diff --git a/src/common/cs_event_listener.js b/src/common/cs_event_listener.js
new file mode 100644
index 0000000..c32d9b8
--- /dev/null
+++ b/src/common/cs_event_listener.js
@@ -0,0 +1,38 @@
+// In order to pass i18n strings and settings values to the injected scripts,
+// which don't have access to the chrome.* APIs, we use event listeners.
+window.addEventListener('TWPT_sendRequest', evt => {
+  var request = evt.detail;
+  switch (request.data.action) {
+    case 'geti18nMessage':
+      var data = chrome.i18n.getMessage(
+          request.data.msg,
+          (Array.isArray(request.data.placeholders) ?
+               request.data.placeholders :
+               []));
+      break;
+
+    case 'getProfileIndicatorOptions':
+      var data = {
+        'indicatorDot': options.profileindicator,
+        'numPosts': options.profileindicatoralt
+      };
+      break;
+
+    case 'getNumPostMonths':
+      var data = options.profileindicatoralt_months;
+      break;
+
+    default:
+      var data = 'unknownAction';
+      console.warn('Unknown action ' + request.data.action + '.');
+      break;
+  }
+
+  var response = {
+    data,
+    requestId: request.id,
+    prefix: (request.prefix || 'TWPT'),
+  };
+
+  window.postMessage(response, '*');
+});
diff --git a/src/content_scripts/profileindicator_inject.js b/src/content_scripts/profileindicator_inject.js
index c726783..8cd1cfc 100644
--- a/src/content_scripts/profileindicator_inject.js
+++ b/src/content_scripts/profileindicator_inject.js
@@ -1,38 +1,5 @@
 chrome.storage.sync.get(null, function(options) {
   if (options.profileindicator || options.profileindicatoralt) {
-    // In order to pass i18n strings and settings values to the injected script,
-    // which doesn't have access to the chrome.* APIs, we use event listeners.
-    window.addEventListener('TWPT_sendRequest', evt => {
-      var request = evt.detail;
-      switch (request.data.action) {
-        case 'geti18nMessage':
-          var data = chrome.i18n.getMessage(
-              request.data.msg,
-              (Array.isArray(request.data.placeholders) ?
-                   request.data.placeholders :
-                   []));
-          break;
-
-        case 'getProfileIndicatorOptions':
-          var data = {
-            'indicatorDot': options.profileindicator,
-            'numPosts': options.profileindicatoralt
-          };
-          break;
-
-        case 'getNumPostMonths':
-          var data = options.profileindicatoralt_months;
-          break;
-
-        default:
-          var data = 'unknownAction';
-          break;
-      }
-      var response = {data, requestId: request.id, prefix: 'TWPT'};
-
-      window.postMessage(response, '*');
-    });
-
     injectScript(
         chrome.runtime.getURL('injections/profileindicator_inject.js'));
     injectStylesheet(
diff --git a/src/injections/profileindicator_inject.js b/src/injections/profileindicator_inject.js
index 1a0125f..fa0415b 100644
--- a/src/injections/profileindicator_inject.js
+++ b/src/injections/profileindicator_inject.js
@@ -90,13 +90,14 @@
 // https://stackoverflow.com/questions/33063774/communication-from-an-injected-script-to-the-content-script-with-a-response
 var contentScriptRequest = (function() {
   var requestId = 0;
+  var prefix = 'TWPT-profileindicator';
 
   function sendRequest(data) {
     var id = requestId++;
 
     return new Promise(function(resolve, reject) {
       var listener = function(evt) {
-        if (evt.source === window && evt.data && evt.data.prefix === 'TWPT' &&
+        if (evt.source === window && evt.data && evt.data.prefix === prefix &&
             evt.data.requestId == id) {
           // Deregister self
           window.removeEventListener('message', listener);
@@ -106,7 +107,7 @@
 
       window.addEventListener('message', listener);
 
-      var payload = {data, id};
+      var payload = {data, id, prefix};
 
       window.dispatchEvent(
           new CustomEvent('TWPT_sendRequest', {detail: payload}));
diff --git a/templates/manifest.gjson b/templates/manifest.gjson
index f6b8d5f..e1d3d0d 100644
--- a/templates/manifest.gjson
+++ b/templates/manifest.gjson
@@ -34,7 +34,7 @@
     {
       "matches": ["https://support.google.com/s/community*", "https://support.google.com/*/thread/*"],
       "exclude_matches": ["https://support.google.com/*/thread/new*"],
-      "js": ["common/content_scripts.js", "content_scripts/profileindicator_inject.js"]
+      "js": ["common/content_scripts.js", "common/cs_event_listener.js", "content_scripts/profileindicator_inject.js"]
     },
     {
       "matches": ["https://support.google.com/*/profile/*"],