blob: 49422a5d44e60e66695e5ba9a15453fddb5638e6 [file] [log] [blame]
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +02001import {escapeUsername} from '../common/communityConsoleUtils.js';
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +02002import {getOptions} from '../common/optionsUtils.js';
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +02003
avm99963104bad32021-02-05 22:00:44 +01004var authuser = (new URL(location.href)).searchParams.get('authuser') || '0';
5
Adrià Vilanova Martínez56d4d182021-06-09 14:56:15 +02006function getSearchUrl(query) {
7 var urlpart = encodeURIComponent('query=' + encodeURIComponent(query));
8 var authuserpart =
9 (authuser == '0' ? '' : '?authuser=' + encodeURIComponent(authuser));
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020010 return 'https://support.google.com/s/community/search/' + urlpart +
Adrià Vilanova Martínez56d4d182021-06-09 14:56:15 +020011 authuserpart;
12}
13
14function injectPreviousPostsLinksUnifiedProfile() {
15 var nameElement =
16 document.querySelector('.scTailwindUser_profileUsercardname');
17 if (nameElement === null) {
18 console.error('[previousposts] Can\'t find username.');
19 return;
20 }
21
22 var name = escapeUsername(nameElement.textContent);
23 var filter = '(creator:"' + name + '" | replier:"' + name + '") forum:any';
24 var url = getSearchUrl(filter);
25
26 var links = document.createElement('div');
27 links.classList.add('TWPT-user-profile__user-links');
28
29 var a = document.createElement('a');
30 a.classList.add('TWPT-user-profile__user-link', 'TWPT-user-link');
31 a.href = url;
Adrià Vilanova Martínez3083a432021-06-15 04:24:22 +020032 a.target = '_parent';
Adrià Vilanova Martínez56d4d182021-06-09 14:56:15 +020033 a.setAttribute(
34 'data-stats-id', 'user-posts-link--tw-power-tools-by-avm99963');
35
36 var badge = document.createElement('span');
37 badge.classList.add('TWPT-badge');
38 badge.setAttribute(
39 'title', chrome.i18n.getMessage('inject_extension_badge_helper', [
40 chrome.i18n.getMessage('appName')
41 ]));
42
43 var badgeImg = document.createElement('img');
44 badgeImg.src =
45 'https://fonts.gstatic.com/s/i/materialicons/repeat/v6/24px.svg';
46
47 badge.appendChild(badgeImg);
48 a.appendChild(badge);
49
50 var span = document.createElement('span');
51 span.textContent = chrome.i18n.getMessage('inject_previousposts');
52
53 a.appendChild(span);
54 links.appendChild(a);
55
56 var userDetailsNode =
57 document.querySelector('.scTailwindUser_profileUsercarddetails');
58 if (userDetailsNode === null) {
59 console.error('[previousposts] Can\'t get user card details div.');
60 return;
61 }
62
63 userDetailsNode.parentNode.insertBefore(links, userDetailsNode.nextSibling);
64}
65
66function injectPreviousPostsLinksOldProfile() {
67 if (document.querySelector('.user-profile__user-links') === null) {
avm99963b69eb3d2020-08-20 02:03:44 +020068 var nameElement = document.querySelector('.user-profile__user-name');
avm99963d0757252019-03-30 20:13:00 +010069 if (nameElement !== null) {
avm99963104bad32021-02-05 22:00:44 +010070 var profileLink = document.querySelector('.community-console');
71 if (profileLink === null) {
72 console.error(
73 '[previousposts] ' +
74 'The user is not a PE so we can\'t show the previous posts link.');
75 return;
76 }
77
78 var profileUrl = profileLink.href || '';
79 var profileUrlSplit = profileUrl.split('/forum/');
80 if (profileUrlSplit.length < 2) {
81 console.error('[previousposts] Can\'t get forum id.');
82 return;
83 }
84
85 var forumId = profileUrlSplit[1].split('/')[0];
avm9996314116b02020-11-02 14:04:14 +010086 var name = escapeUsername(nameElement.textContent);
avm99963104bad32021-02-05 22:00:44 +010087 var filter =
88 '(creator:"' + name + '" | replier:"' + name + '") forum:' + forumId;
Adrià Vilanova Martínez56d4d182021-06-09 14:56:15 +020089 var url = getSearchUrl(filter);
avm999630bd07f62020-09-01 20:18:06 +020090
91 var links = document.createElement('div');
92 links.classList.add('user-profile__user-links');
93
94 var linkTitle = document.createElement('div');
95 linkTitle.classList.add('user-profile__user-link-title');
96 linkTitle.textContent = chrome.i18n.getMessage('inject_links');
97
98 links.appendChild(linkTitle);
99
100 var ul = document.createElement('ul');
101
102 var li = document.createElement('li');
103 li.classList.add('user-profile__user-link');
104
105 var a = document.createElement('a');
avm9996306167752020-09-08 00:50:36 +0200106 a.classList.add('user-profile__user-link', 'TWPT-user-link');
avm999630bd07f62020-09-01 20:18:06 +0200107 a.href = url;
108 a.setAttribute(
109 'data-stats-id', 'user-posts-link--tw-power-tools-by-avm99963');
avm999630bd07f62020-09-01 20:18:06 +0200110
avm9996306167752020-09-08 00:50:36 +0200111 var badge = document.createElement('span');
112 badge.classList.add('TWPT-badge');
113 badge.setAttribute(
114 'title', chrome.i18n.getMessage('inject_extension_badge_helper', [
115 chrome.i18n.getMessage('appName')
116 ]));
117
118 var badgeImg = document.createElement('img');
119 badgeImg.src =
120 'https://fonts.gstatic.com/s/i/materialicons/repeat/v6/24px.svg';
121
122 badge.appendChild(badgeImg);
123 a.appendChild(badge);
124
125 var span = document.createElement('span');
126 span.textContent = chrome.i18n.getMessage('inject_previousposts');
127
128 a.appendChild(span);
avm999630bd07f62020-09-01 20:18:06 +0200129 li.appendChild(a);
130 ul.appendChild(li);
131 links.appendChild(ul);
132
avm99963b69eb3d2020-08-20 02:03:44 +0200133 document.querySelector('.user-profile__user-details-container')
avm999630bd07f62020-09-01 20:18:06 +0200134 .appendChild(links);
avm99963104bad32021-02-05 22:00:44 +0100135 } else {
136 console.error('[previousposts] Can\'t find username.');
avm99963d0757252019-03-30 20:13:00 +0100137 }
138 }
Adrià Vilanova Martínez56d4d182021-06-09 14:56:15 +0200139}
140
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200141getOptions('history').then(options => {
142 if (options?.history) {
Adrià Vilanova Martínez56d4d182021-06-09 14:56:15 +0200143 if (document.getElementById('unified-user-profile') !== null)
144 injectPreviousPostsLinksUnifiedProfile();
145 else
146 injectPreviousPostsLinksOldProfile();
147 }
avm99963d0757252019-03-30 20:13:00 +0100148});