blob: 38894d6f75f838f7b706ac99629cf7caedf718f0 [file] [log] [blame]
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +02001import {escapeUsername} from '../common/communityConsoleUtils.js';
2
avm99963104bad32021-02-05 22:00:44 +01003var authuser = (new URL(location.href)).searchParams.get('authuser') || '0';
4
Adrià Vilanova Martínez56d4d182021-06-09 14:56:15 +02005function getSearchUrl(query) {
6 var urlpart = encodeURIComponent('query=' + encodeURIComponent(query));
7 var authuserpart =
8 (authuser == '0' ? '' : '?authuser=' + encodeURIComponent(authuser));
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +02009 return 'https://support.google.com/s/community/search/' + urlpart +
Adrià Vilanova Martínez56d4d182021-06-09 14:56:15 +020010 authuserpart;
11}
12
13function injectPreviousPostsLinksUnifiedProfile() {
14 var nameElement =
15 document.querySelector('.scTailwindUser_profileUsercardname');
16 if (nameElement === null) {
17 console.error('[previousposts] Can\'t find username.');
18 return;
19 }
20
21 var name = escapeUsername(nameElement.textContent);
22 var filter = '(creator:"' + name + '" | replier:"' + name + '") forum:any';
23 var url = getSearchUrl(filter);
24
25 var links = document.createElement('div');
26 links.classList.add('TWPT-user-profile__user-links');
27
28 var a = document.createElement('a');
29 a.classList.add('TWPT-user-profile__user-link', 'TWPT-user-link');
30 a.href = url;
Adrià Vilanova Martínez3083a432021-06-15 04:24:22 +020031 a.target = '_parent';
Adrià Vilanova Martínez56d4d182021-06-09 14:56:15 +020032 a.setAttribute(
33 'data-stats-id', 'user-posts-link--tw-power-tools-by-avm99963');
34
35 var badge = document.createElement('span');
36 badge.classList.add('TWPT-badge');
37 badge.setAttribute(
38 'title', chrome.i18n.getMessage('inject_extension_badge_helper', [
39 chrome.i18n.getMessage('appName')
40 ]));
41
42 var badgeImg = document.createElement('img');
43 badgeImg.src =
44 'https://fonts.gstatic.com/s/i/materialicons/repeat/v6/24px.svg';
45
46 badge.appendChild(badgeImg);
47 a.appendChild(badge);
48
49 var span = document.createElement('span');
50 span.textContent = chrome.i18n.getMessage('inject_previousposts');
51
52 a.appendChild(span);
53 links.appendChild(a);
54
55 var userDetailsNode =
56 document.querySelector('.scTailwindUser_profileUsercarddetails');
57 if (userDetailsNode === null) {
58 console.error('[previousposts] Can\'t get user card details div.');
59 return;
60 }
61
62 userDetailsNode.parentNode.insertBefore(links, userDetailsNode.nextSibling);
63}
64
65function injectPreviousPostsLinksOldProfile() {
66 if (document.querySelector('.user-profile__user-links') === null) {
avm99963b69eb3d2020-08-20 02:03:44 +020067 var nameElement = document.querySelector('.user-profile__user-name');
avm99963d0757252019-03-30 20:13:00 +010068 if (nameElement !== null) {
avm99963104bad32021-02-05 22:00:44 +010069 var profileLink = document.querySelector('.community-console');
70 if (profileLink === null) {
71 console.error(
72 '[previousposts] ' +
73 'The user is not a PE so we can\'t show the previous posts link.');
74 return;
75 }
76
77 var profileUrl = profileLink.href || '';
78 var profileUrlSplit = profileUrl.split('/forum/');
79 if (profileUrlSplit.length < 2) {
80 console.error('[previousposts] Can\'t get forum id.');
81 return;
82 }
83
84 var forumId = profileUrlSplit[1].split('/')[0];
avm9996314116b02020-11-02 14:04:14 +010085 var name = escapeUsername(nameElement.textContent);
avm99963104bad32021-02-05 22:00:44 +010086 var filter =
87 '(creator:"' + name + '" | replier:"' + name + '") forum:' + forumId;
Adrià Vilanova Martínez56d4d182021-06-09 14:56:15 +020088 var url = getSearchUrl(filter);
avm999630bd07f62020-09-01 20:18:06 +020089
90 var links = document.createElement('div');
91 links.classList.add('user-profile__user-links');
92
93 var linkTitle = document.createElement('div');
94 linkTitle.classList.add('user-profile__user-link-title');
95 linkTitle.textContent = chrome.i18n.getMessage('inject_links');
96
97 links.appendChild(linkTitle);
98
99 var ul = document.createElement('ul');
100
101 var li = document.createElement('li');
102 li.classList.add('user-profile__user-link');
103
104 var a = document.createElement('a');
avm9996306167752020-09-08 00:50:36 +0200105 a.classList.add('user-profile__user-link', 'TWPT-user-link');
avm999630bd07f62020-09-01 20:18:06 +0200106 a.href = url;
107 a.setAttribute(
108 'data-stats-id', 'user-posts-link--tw-power-tools-by-avm99963');
avm999630bd07f62020-09-01 20:18:06 +0200109
avm9996306167752020-09-08 00:50:36 +0200110 var badge = document.createElement('span');
111 badge.classList.add('TWPT-badge');
112 badge.setAttribute(
113 'title', chrome.i18n.getMessage('inject_extension_badge_helper', [
114 chrome.i18n.getMessage('appName')
115 ]));
116
117 var badgeImg = document.createElement('img');
118 badgeImg.src =
119 'https://fonts.gstatic.com/s/i/materialicons/repeat/v6/24px.svg';
120
121 badge.appendChild(badgeImg);
122 a.appendChild(badge);
123
124 var span = document.createElement('span');
125 span.textContent = chrome.i18n.getMessage('inject_previousposts');
126
127 a.appendChild(span);
avm999630bd07f62020-09-01 20:18:06 +0200128 li.appendChild(a);
129 ul.appendChild(li);
130 links.appendChild(ul);
131
avm99963b69eb3d2020-08-20 02:03:44 +0200132 document.querySelector('.user-profile__user-details-container')
avm999630bd07f62020-09-01 20:18:06 +0200133 .appendChild(links);
avm99963104bad32021-02-05 22:00:44 +0100134 } else {
135 console.error('[previousposts] Can\'t find username.');
avm99963d0757252019-03-30 20:13:00 +0100136 }
137 }
Adrià Vilanova Martínez56d4d182021-06-09 14:56:15 +0200138}
139
140chrome.storage.sync.get(null, function(items) {
141 if (items.history) {
142 if (document.getElementById('unified-user-profile') !== null)
143 injectPreviousPostsLinksUnifiedProfile();
144 else
145 injectPreviousPostsLinksOldProfile();
146 }
avm99963d0757252019-03-30 20:13:00 +0100147});