Previous posts: add support for unified profiles

This change adds the code needed to add the previous posts links to
unified profiles.

Change-Id: I65f9e5bed4961e8af10e702fc8cb8b31cd24a17b
diff --git a/src/common/forum.css b/src/common/forum.css
index 7e862fd..90a64aa 100644
--- a/src/common/forum.css
+++ b/src/common/forum.css
@@ -23,6 +23,11 @@
   filter: invert(1);
 }
 
-.user-profile__user-link.TWPT-user-link > * {
+.TWPT-user-profile__user-links {
+  margin-top: 8px;
+}
+
+.user-profile__user-link.TWPT-user-link > *,
+    .TWPT-user-profile__user-link.TWPT-user-link > * {
   vertical-align: middle;
 }
diff --git a/src/content_scripts/profile_inject.js b/src/content_scripts/profile_inject.js
index c7b04b1..dd5e68f 100644
--- a/src/content_scripts/profile_inject.js
+++ b/src/content_scripts/profile_inject.js
@@ -1,8 +1,66 @@
 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) {
+function getSearchUrl(query) {
+  var urlpart = encodeURIComponent('query=' + encodeURIComponent(query));
+  var authuserpart =
+      (authuser == '0' ? '' : '?authuser=' + encodeURIComponent(authuser));
+  return url = '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.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);
+}
+
+function injectPreviousPostsLinksOldProfile() {
+  if (document.querySelector('.user-profile__user-links') === null) {
     var nameElement = document.querySelector('.user-profile__user-name');
     if (nameElement !== null) {
       var profileLink = document.querySelector('.community-console');
@@ -24,11 +82,7 @@
       var name = escapeUsername(nameElement.textContent);
       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 url = getSearchUrl(filter);
 
       var links = document.createElement('div');
       links.classList.add('user-profile__user-links');
@@ -78,4 +132,13 @@
       console.error('[previousposts] Can\'t find username.');
     }
   }
+}
+
+chrome.storage.sync.get(null, function(items) {
+  if (items.history) {
+    if (document.getElementById('unified-user-profile') !== null)
+      injectPreviousPostsLinksUnifiedProfile();
+    else
+      injectPreviousPostsLinksOldProfile();
+  }
 });
diff --git a/templates/manifest.gjson b/templates/manifest.gjson
index 266ec60..e643604 100644
--- a/templates/manifest.gjson
+++ b/templates/manifest.gjson
@@ -42,7 +42,7 @@
       "js": ["common/content_scripts.js", "common/cs_event_listener.js", "content_scripts/profileindicator_inject.js"]
     },
     {
-      "matches": ["https://support.google.com/*/profile/*"],
+      "matches": ["https://support.google.com/*/profile/*", "https://support.google.com/profile/*"],
       "js": ["common/content_scripts.js", "content_scripts/profile_inject.js"],
       "css": ["common/forum.css"]
     }