Workaround: link previous posts links to the CC in TW

Custom filters have stopped working in TW, so this change makes the
"previous posts" links in TW point to the CC to work around this
problem. This includes the following links:

- "Previous posts" links in user profiles in TW
- Profile indicator link (in TW threads)

Previously, the "previous posts" links pointed to TW.

Bug: #29
Change-Id: I3a77ae28542aed5227d3cf1fe6ae234f14bad592
diff --git a/src/content_scripts/profile_inject.js b/src/content_scripts/profile_inject.js
index ca16337..c7b04b1 100644
--- a/src/content_scripts/profile_inject.js
+++ b/src/content_scripts/profile_inject.js
@@ -1,12 +1,34 @@
+var authuser = (new URL(location.href)).searchParams.get('authuser') || '0';
+
 chrome.storage.sync.get(null, function(items) {
   if (items.history &&
       document.querySelector('.user-profile__user-links') === null) {
     var nameElement = document.querySelector('.user-profile__user-name');
     if (nameElement !== null) {
+      var profileLink = document.querySelector('.community-console');
+      if (profileLink === null) {
+        console.error(
+            '[previousposts] ' +
+            'The user is not a PE so we can\'t show the previous posts link.');
+        return;
+      }
+
+      var profileUrl = profileLink.href || '';
+      var profileUrlSplit = profileUrl.split('/forum/');
+      if (profileUrlSplit.length < 2) {
+        console.error('[previousposts] Can\'t get forum id.');
+        return;
+      }
+
+      var forumId = profileUrlSplit[1].split('/')[0];
       var name = escapeUsername(nameElement.textContent);
-      var filter = 'creator:"' + name + '" | replier:"' + name + '"';
-      var url = document.location.pathname.split('/profile')[0] +
-          '/threads?thread_filter=' + encodeURIComponent(filter);
+      var filter =
+          '(creator:"' + name + '" | replier:"' + name + '") forum:' + forumId;
+      var urlpart = encodeURIComponent('query=' + encodeURIComponent(filter));
+      var authuserpart =
+          (authuser == '0' ? '' : '?authuser=' + encodeURIComponent(authuser));
+      var url = 'https://support.google.com/s/community/search/' + urlpart +
+          authuserpart;
 
       var links = document.createElement('div');
       links.classList.add('user-profile__user-links');
@@ -50,10 +72,10 @@
       ul.appendChild(li);
       links.appendChild(ul);
 
-      console.log(links);
-
       document.querySelector('.user-profile__user-details-container')
           .appendChild(links);
+    } else {
+      console.error('[previousposts] Can\'t find username.');
     }
   }
 });
diff --git a/src/injections/profileindicator_inject.js b/src/injections/profileindicator_inject.js
index 823003f..4f18f85 100644
--- a/src/injections/profileindicator_inject.js
+++ b/src/injections/profileindicator_inject.js
@@ -204,18 +204,25 @@
 
   var forumId = forumUrlSplit[1].split('/')[0];
 
+  /*
+   * TODO(avm99963): If the TW filters ever work again, set isCCLink to isCC.
+   * Otherwise, issue #29 should be resolved:
+   * https://github.com/avm99963/infinitegforums/issues/29
+   */
+  var isCCLink = true;
+
   var query = '(replier:"' + escapedUsername + '" | creator:"' +
       escapedUsername + '") ' + FILTER_ALL_LANGUAGES;
   var encodedQuery =
-      encodeURIComponent(query + (isCC ? ' forum:' + forumId : ''));
+      encodeURIComponent(query + (isCCLink ? ' forum:' + forumId : ''));
   var authuserPart =
       (authuser == '0' ?
            '' :
-           (isCC ? '?' : '&') + 'authuser=' + encodeURIComponent(authuser));
+           (isCCLink ? '?' : '&') + 'authuser=' + encodeURIComponent(authuser));
   var searchURL =
-      (isCC ? 'https://support.google.com/s/community/search/' +
+      (isCCLink ? 'https://support.google.com/s/community/search/' +
                encodeURIComponent('query=' + encodedQuery) + authuserPart :
-              document.location.pathname.split('/thread')[0] +
+                  document.location.pathname.split('/thread')[0] +
                '/threads?thread_filter=' + encodedQuery + authuserPart);
 
   if (options.numPosts) {