feat(cc-redirect): redirect URL hash and add redundant redirect method

This CL adds logic to redirect the URL hash to the Community Console,
so actions that are embedded in the URL like |#action=reply| will be
passed to the Community Console.

It also adds another method of redirecting to the Community Console
which will coexist with the old method.

Fixed: twpowertools:164
Change-Id: Ib3f770d7cbeec8f26cdd249e66f7f46ae94bb1c8
diff --git a/src/contentScripts/publicThread.js b/src/contentScripts/publicThread.js
index dd2088a..4d98bee 100644
--- a/src/contentScripts/publicThread.js
+++ b/src/contentScripts/publicThread.js
@@ -1,7 +1,6 @@
 import {injectStylesheet} from '../common/contentScriptsUtils.js';
 import {getOptions} from '../common/optionsUtils.js';
-
-var CCThreadWithoutMessage = /forum\/[0-9]*\/thread\/[0-9]*$/;
+import {redirectIfApplicable} from '../redirect/index.js';
 
 const kLoadMoreButtons = [
   {
@@ -23,15 +22,27 @@
 ];
 const kMsgidDelay = 3500;
 
+function main() {
+  const ok = redirectIfApplicable();
+  if (ok) return;
+
+  getOptions(null).then(options => {
+    setUpInfiniteScrollWithPotentialDelay(options);
+
+    if (options.imagemaxheight)
+      injectStylesheet(chrome.runtime.getURL('css/image_max_height.css'));
+  });
+}
+
 var intersectionObserver;
 
-function intersectionCallback(entries, observer) {
+function intersectionCallback(entries) {
   entries.forEach(entry => {
     if (entry.isIntersecting) {
       entry.target.click();
     }
   });
-};
+}
 
 var intersectionOptions = {
   threshold: 1.0,
@@ -65,22 +76,4 @@
   }
 }
 
-getOptions(null).then(options => {
-  var redirectLink = document.querySelector('.community-console');
-  if (options.redirect && redirectLink !== null) {
-    var redirectUrl = redirectLink.href;
-
-    var searchParams = new URLSearchParams(location.search);
-    if (searchParams.has('msgid') && searchParams.get('msgid') !== '' &&
-        CCThreadWithoutMessage.test(redirectUrl))
-      redirectUrl +=
-          '/message/' + encodeURIComponent(searchParams.get('msgid'));
-
-    window.location = redirectUrl;
-  } else {
-    setUpInfiniteScrollWithPotentialDelay(options);
-
-    if (options.imagemaxheight)
-      injectStylesheet(chrome.runtime.getURL('css/image_max_height.css'));
-  }
-});
+main();