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/common/TWBasicUtils.js b/src/common/TWBasicUtils.js
new file mode 100644
index 0000000..b97fba1
--- /dev/null
+++ b/src/common/TWBasicUtils.js
@@ -0,0 +1,31 @@
+import rescape from '@stdlib/utils-escape-regexp-string';
+
+import {correctArrayKeys} from './protojs';
+
+export function parseView(viewVar) {
+  const escapedViewVar = rescape(viewVar);
+  const viewRegex = new RegExp(`var ${escapedViewVar} ?= ?'([^']+)';`);
+
+  const scripts = document.querySelectorAll('script');
+  let viewData = null;
+  for (let i = 0; i < scripts.length; ++i) {
+    const matches = scripts[i].textContent.match(viewRegex);
+    if (matches?.[1]) {
+      let rawJsonStringContents =
+          matches[1]
+              .replace(
+                  /\\x([0-9a-f]{2})/ig,
+                  (_, pair) => {
+                    return String.fromCharCode(parseInt(pair, 16));
+                  })
+              .replace(/\\'/g, `'`)
+              .replace(/"/g, `\\"`);
+      let rawJsonString = `"${rawJsonStringContents}"`;
+      let rawJson = JSON.parse(rawJsonString);
+      viewData = JSON.parse(rawJson);
+      break;
+    }
+  }
+  if (!viewData) throw new Error(`Could not find ${viewVar} view data.`);
+  return correctArrayKeys(viewData);
+}