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/utilsCommon/unifiedProfiles.js b/src/contentScripts/utilsCommon/unifiedProfiles.js
new file mode 100644
index 0000000..d3995ea
--- /dev/null
+++ b/src/contentScripts/utilsCommon/unifiedProfiles.js
@@ -0,0 +1,77 @@
+import {escapeUsername} from '../../common/communityConsoleUtils.js';
+import {isOptionEnabled} from '../../common/optionsUtils.js';
+import {createExtBadge} from '../communityConsole/utils/common.js';
+
+var authuser = (new URL(location.href)).searchParams.get('authuser') || '0';
+
+export 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;
+}
+
+export function injectPreviousPostsLinksUnifiedProfile(isCommunityConsole) {
+  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');
+
+  let badge;
+  if (isCommunityConsole) {
+    badge = createExtBadge();
+  } else {
+    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);
+}
+
+export function injectPreviousPostsLinksUnifiedProfileIfEnabled(isCommunityConsole) {
+  isOptionEnabled('history').then(isEnabled => {
+    if (isEnabled) injectPreviousPostsLinksUnifiedProfile(isCommunityConsole);
+  });
+}