blob: eccb4b61bb0fec97488bfab1c9a663a0718a1d84 [file] [log] [blame]
avm999632485a3e2021-09-08 22:18:38 +02001import {MDCTooltip} from '@material/tooltip';
2
Adrià Vilanova Martíneze7770472021-10-17 00:02:37 +02003import {createImmuneLink} from '../../common/commonUtils.js';
Adrià Vilanova Martínez1f652522021-10-14 00:23:23 +02004import {escapeUsername} from '../../common/communityConsoleUtils.js';
Adrià Vilanova Martínezb523be92024-05-25 19:14:19 +02005import {isOptionEnabled} from '../../common/options/optionsUtils.js';
avm999632485a3e2021-09-08 22:18:38 +02006import {createPlainTooltip} from '../../common/tooltip.js';
Adrià Vilanova Martínez1f652522021-10-14 00:23:23 +02007import {createExtBadge} from '../communityConsole/utils/common.js';
8
9var authuser = (new URL(location.href)).searchParams.get('authuser') || '0';
10
11export function getSearchUrl(query) {
12 var urlpart = encodeURIComponent('query=' + encodeURIComponent(query));
13 var authuserpart =
14 (authuser == '0' ? '' : '?authuser=' + encodeURIComponent(authuser));
15 return 'https://support.google.com/s/community/search/' + urlpart +
16 authuserpart;
17}
18
19export function injectPreviousPostsLinksUnifiedProfile(isCommunityConsole) {
20 var nameElement =
21 document.querySelector('.scTailwindUser_profileUsercardname');
22 if (nameElement === null) {
23 console.error('[previousposts] Can\'t find username.');
24 return;
25 }
26
27 var name = escapeUsername(nameElement.textContent);
28 var filter = '(creator:"' + name + '" | replier:"' + name + '") forum:any';
29 var url = getSearchUrl(filter);
30
31 var links = document.createElement('div');
32 links.classList.add('TWPT-user-profile__user-links');
33
Adrià Vilanova Martíneze7770472021-10-17 00:02:37 +020034 var a = isCommunityConsole ? createImmuneLink() : document.createElement('a');
Adrià Vilanova Martínez1f652522021-10-14 00:23:23 +020035 a.classList.add('TWPT-user-profile__user-link', 'TWPT-user-link');
36 a.href = url;
37 a.target = '_parent';
38 a.setAttribute(
39 'data-stats-id', 'user-posts-link--tw-power-tools-by-avm99963');
40
avm999632485a3e2021-09-08 22:18:38 +020041 let badge, badgeTooltip;
Adrià Vilanova Martínez1f652522021-10-14 00:23:23 +020042 if (isCommunityConsole) {
avm999632485a3e2021-09-08 22:18:38 +020043 [badge, badgeTooltip] = createExtBadge();
Adrià Vilanova Martínez1f652522021-10-14 00:23:23 +020044 } else {
45 badge = document.createElement('span');
46 badge.classList.add('TWPT-badge');
Adrià Vilanova Martínez1f652522021-10-14 00:23:23 +020047
48 var badgeImg = document.createElement('img');
49 badgeImg.src =
50 'https://fonts.gstatic.com/s/i/materialicons/repeat/v6/24px.svg';
51
52 badge.appendChild(badgeImg);
53 }
54
55 a.appendChild(badge);
56
57 var span = document.createElement('span');
58 span.textContent = chrome.i18n.getMessage('inject_previousposts');
59
60 a.appendChild(span);
61 links.appendChild(a);
62
63 var userDetailsNode =
64 document.querySelector('.scTailwindUser_profileUsercarddetails');
65 if (userDetailsNode === null) {
66 console.error('[previousposts] Can\'t get user card details div.');
67 return;
68 }
69
70 userDetailsNode.parentNode.insertBefore(links, userDetailsNode.nextSibling);
avm999632485a3e2021-09-08 22:18:38 +020071
72 if (isCommunityConsole)
73 new MDCTooltip(badgeTooltip);
74 else
75 createPlainTooltip(
76 badge, chrome.i18n.getMessage('inject_extension_badge_helper', [
77 chrome.i18n.getMessage('appName')
78 ]));
Adrià Vilanova Martínez1f652522021-10-14 00:23:23 +020079}
80
avm999632485a3e2021-09-08 22:18:38 +020081export function injectPreviousPostsLinksUnifiedProfileIfEnabled(
82 isCommunityConsole) {
Adrià Vilanova Martínez1f652522021-10-14 00:23:23 +020083 isOptionEnabled('history').then(isEnabled => {
84 if (isEnabled) injectPreviousPostsLinksUnifiedProfile(isCommunityConsole);
85 });
86}