blob: 0bb1a7d7bfe0cbe539c529a671ac338ff8de88f6 [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';
avm999632485a3e2021-09-08 22:18:38 +02003import {createPlainTooltip} from '../common/tooltip.js';
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +02004
Adrià Vilanova Martínez1f652522021-10-14 00:23:23 +02005import {getSearchUrl, injectPreviousPostsLinksUnifiedProfile} from './utilsCommon/unifiedProfiles.js';
avm99963104bad32021-02-05 22:00:44 +01006
Adrià Vilanova Martínez1f652522021-10-14 00:23:23 +02007// TODO(b/twpowertools/80): Remove this code.
Adrià Vilanova Martínez56d4d182021-06-09 14:56:15 +02008function injectPreviousPostsLinksOldProfile() {
9 if (document.querySelector('.user-profile__user-links') === null) {
avm99963b69eb3d2020-08-20 02:03:44 +020010 var nameElement = document.querySelector('.user-profile__user-name');
avm99963d0757252019-03-30 20:13:00 +010011 if (nameElement !== null) {
avm99963104bad32021-02-05 22:00:44 +010012 var profileLink = document.querySelector('.community-console');
13 if (profileLink === null) {
14 console.error(
15 '[previousposts] ' +
16 'The user is not a PE so we can\'t show the previous posts link.');
17 return;
18 }
19
20 var profileUrl = profileLink.href || '';
21 var profileUrlSplit = profileUrl.split('/forum/');
22 if (profileUrlSplit.length < 2) {
23 console.error('[previousposts] Can\'t get forum id.');
24 return;
25 }
26
27 var forumId = profileUrlSplit[1].split('/')[0];
avm9996314116b02020-11-02 14:04:14 +010028 var name = escapeUsername(nameElement.textContent);
avm99963104bad32021-02-05 22:00:44 +010029 var filter =
30 '(creator:"' + name + '" | replier:"' + name + '") forum:' + forumId;
Adrià Vilanova Martínez56d4d182021-06-09 14:56:15 +020031 var url = getSearchUrl(filter);
avm999630bd07f62020-09-01 20:18:06 +020032
33 var links = document.createElement('div');
34 links.classList.add('user-profile__user-links');
35
36 var linkTitle = document.createElement('div');
37 linkTitle.classList.add('user-profile__user-link-title');
38 linkTitle.textContent = chrome.i18n.getMessage('inject_links');
39
40 links.appendChild(linkTitle);
41
42 var ul = document.createElement('ul');
43
44 var li = document.createElement('li');
45 li.classList.add('user-profile__user-link');
46
47 var a = document.createElement('a');
avm9996306167752020-09-08 00:50:36 +020048 a.classList.add('user-profile__user-link', 'TWPT-user-link');
avm999630bd07f62020-09-01 20:18:06 +020049 a.href = url;
50 a.setAttribute(
51 'data-stats-id', 'user-posts-link--tw-power-tools-by-avm99963');
avm999630bd07f62020-09-01 20:18:06 +020052
avm9996306167752020-09-08 00:50:36 +020053 var badge = document.createElement('span');
54 badge.classList.add('TWPT-badge');
avm9996306167752020-09-08 00:50:36 +020055
56 var badgeImg = document.createElement('img');
57 badgeImg.src =
58 'https://fonts.gstatic.com/s/i/materialicons/repeat/v6/24px.svg';
59
60 badge.appendChild(badgeImg);
61 a.appendChild(badge);
62
63 var span = document.createElement('span');
64 span.textContent = chrome.i18n.getMessage('inject_previousposts');
65
66 a.appendChild(span);
avm999630bd07f62020-09-01 20:18:06 +020067 li.appendChild(a);
68 ul.appendChild(li);
69 links.appendChild(ul);
70
avm99963b69eb3d2020-08-20 02:03:44 +020071 document.querySelector('.user-profile__user-details-container')
avm999630bd07f62020-09-01 20:18:06 +020072 .appendChild(links);
avm999632485a3e2021-09-08 22:18:38 +020073
74 createPlainTooltip(
75 badge, chrome.i18n.getMessage('inject_extension_badge_helper', [
76 chrome.i18n.getMessage('appName')
77 ]));
avm99963104bad32021-02-05 22:00:44 +010078 } else {
79 console.error('[previousposts] Can\'t find username.');
avm99963d0757252019-03-30 20:13:00 +010080 }
81 }
Adrià Vilanova Martínez56d4d182021-06-09 14:56:15 +020082}
83
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020084getOptions('history').then(options => {
85 if (options?.history) {
Adrià Vilanova Martínez56d4d182021-06-09 14:56:15 +020086 if (document.getElementById('unified-user-profile') !== null)
Adrià Vilanova Martínez1f652522021-10-14 00:23:23 +020087 injectPreviousPostsLinksUnifiedProfile(/* isCommunityConsole = */ false);
Adrià Vilanova Martínez56d4d182021-06-09 14:56:15 +020088 else
89 injectPreviousPostsLinksOldProfile();
90 }
avm99963d0757252019-03-30 20:13:00 +010091});