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