Conserve highlighted message when redirecting from TW to CC

This change adds special handling of the msgid GET parameter in TW, so
when a thread is opened in TW with the msgid parameter set, the CC is
opened with that message highlighted. By default, the URL doesn't have
the |/message/{id}| part.

Fixes: #35
Change-Id: I57c4d139c4efe38b92e831ea15e9cea8d5d1d529
diff --git a/src/content_scripts/thread_inject.js b/src/content_scripts/thread_inject.js
index 984774e..110b5c4 100644
--- a/src/content_scripts/thread_inject.js
+++ b/src/content_scripts/thread_inject.js
@@ -1,3 +1,5 @@
+var CCThreadWithoutMessage = /forum\/[0-9]*\/thread\/[0-9]*$/;
+
 var intersectionObserver;
 
 function intersectionCallback(entries, observer) {
@@ -15,7 +17,15 @@
 chrome.storage.sync.get(null, function(items) {
   var redirectLink = document.querySelector('.community-console');
   if (items.redirect && redirectLink !== null) {
-    window.location = redirectLink.href;
+    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 {
     var button =
         document.querySelector('.thread-all-replies__load-more-button');