Add "previous posts" support to unified profiles in CC

The Community Console used to embed profiles from TW via an iframe, so
the logic to add the "previous posts" link was only added to TW.

However, the CC now renders the profiles without an iframe, with the
exact same layout as TW. This change adds support to add the "previous
posts" link in the CC, which reuses existing code (which has been
abstracted).

Fixed: twpowertools:78
Change-Id: I511af1e8aab1292f34beb712f29d52df9409e352
diff --git a/src/contentScripts/profile.js b/src/contentScripts/profile.js
index 49422a5..7bd6916 100644
--- a/src/contentScripts/profile.js
+++ b/src/contentScripts/profile.js
@@ -1,68 +1,9 @@
 import {escapeUsername} from '../common/communityConsoleUtils.js';
 import {getOptions} from '../common/optionsUtils.js';
 
-var authuser = (new URL(location.href)).searchParams.get('authuser') || '0';
+import {getSearchUrl, injectPreviousPostsLinksUnifiedProfile} from './utilsCommon/unifiedProfiles.js';
 
-function getSearchUrl(query) {
-  var urlpart = encodeURIComponent('query=' + encodeURIComponent(query));
-  var authuserpart =
-      (authuser == '0' ? '' : '?authuser=' + encodeURIComponent(authuser));
-  return 'https://support.google.com/s/community/search/' + urlpart +
-      authuserpart;
-}
-
-function injectPreviousPostsLinksUnifiedProfile() {
-  var nameElement =
-      document.querySelector('.scTailwindUser_profileUsercardname');
-  if (nameElement === null) {
-    console.error('[previousposts] Can\'t find username.');
-    return;
-  }
-
-  var name = escapeUsername(nameElement.textContent);
-  var filter = '(creator:"' + name + '" | replier:"' + name + '") forum:any';
-  var url = getSearchUrl(filter);
-
-  var links = document.createElement('div');
-  links.classList.add('TWPT-user-profile__user-links');
-
-  var a = document.createElement('a');
-  a.classList.add('TWPT-user-profile__user-link', 'TWPT-user-link');
-  a.href = url;
-  a.target = '_parent';
-  a.setAttribute(
-      'data-stats-id', 'user-posts-link--tw-power-tools-by-avm99963');
-
-  var badge = document.createElement('span');
-  badge.classList.add('TWPT-badge');
-  badge.setAttribute(
-      'title', chrome.i18n.getMessage('inject_extension_badge_helper', [
-        chrome.i18n.getMessage('appName')
-      ]));
-
-  var badgeImg = document.createElement('img');
-  badgeImg.src =
-      'https://fonts.gstatic.com/s/i/materialicons/repeat/v6/24px.svg';
-
-  badge.appendChild(badgeImg);
-  a.appendChild(badge);
-
-  var span = document.createElement('span');
-  span.textContent = chrome.i18n.getMessage('inject_previousposts');
-
-  a.appendChild(span);
-  links.appendChild(a);
-
-  var userDetailsNode =
-      document.querySelector('.scTailwindUser_profileUsercarddetails');
-  if (userDetailsNode === null) {
-    console.error('[previousposts] Can\'t get user card details div.');
-    return;
-  }
-
-  userDetailsNode.parentNode.insertBefore(links, userDetailsNode.nextSibling);
-}
-
+// TODO(b/twpowertools/80): Remove this code.
 function injectPreviousPostsLinksOldProfile() {
   if (document.querySelector('.user-profile__user-links') === null) {
     var nameElement = document.querySelector('.user-profile__user-name');
@@ -141,7 +82,7 @@
 getOptions('history').then(options => {
   if (options?.history) {
     if (document.getElementById('unified-user-profile') !== null)
-      injectPreviousPostsLinksUnifiedProfile();
+      injectPreviousPostsLinksUnifiedProfile(/* isCommunityConsole = */ false);
     else
       injectPreviousPostsLinksOldProfile();
   }