blob: 44c7ebb5cd59b9953b81f6e070ffb928afc9dbbd [file] [log] [blame]
Adrià Vilanova Martíneze7770472021-10-17 00:02:37 +02001import {createImmuneLink} from '../../common/commonUtils.js';
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +02002import {escapeUsername, getAuthUser} from '../../common/communityConsoleUtils.js';
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +02003import {isOptionEnabled} from '../../common/optionsUtils.js';
4
5import {createExtBadge, getNParent} from './utils/common.js';
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +02006
7var authuser = getAuthUser();
8
9function addProfileHistoryLink(node, type, query) {
10 var urlpart = encodeURIComponent('query=' + query);
11 var authuserpart =
12 (authuser == '0' ? '' : '?authuser=' + encodeURIComponent(authuser));
13 var container = document.createElement('div');
14 container.style.margin = '3px 0';
15
Adrià Vilanova Martíneze7770472021-10-17 00:02:37 +020016 var link = createImmuneLink();
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020017 link.setAttribute(
18 'href',
19 'https://support.google.com/s/community/search/' + urlpart +
20 authuserpart);
21 link.innerText = chrome.i18n.getMessage('inject_previousposts_' + type);
22
23 container.appendChild(link);
24 node.appendChild(container);
25}
26
27export function injectPreviousPostsLinks(nameElement) {
28 var mainCardContent = getNParent(nameElement, 3);
29 if (mainCardContent === null) {
30 console.error(
31 '[previousposts] Couldn\'t find |.main-card-content| element.');
32 return;
33 }
34
35 var forumId = location.href.split('/forum/')[1].split('/')[0] || '0';
36
37 var nameTag =
38 (nameElement.tagName == 'EC-DISPLAY-NAME-EDITOR' ?
39 nameElement.querySelector('.top-section > span') ?? nameElement :
40 nameElement);
41 var name = escapeUsername(nameTag.textContent);
42 var query1 = encodeURIComponent(
43 '(creator:"' + name + '" | replier:"' + name + '") forum:' + forumId);
44 var query2 = encodeURIComponent(
45 '(creator:"' + name + '" | replier:"' + name + '") forum:any');
46
47 var container = document.createElement('div');
48 container.classList.add('TWPT-previous-posts');
49
avm999632485a3e2021-09-08 22:18:38 +020050 let badge, badgeTooltip;
51 [badge, badgeTooltip] = createExtBadge();
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020052 container.appendChild(badge);
53
54 var linkContainer = document.createElement('div');
55 linkContainer.classList.add('TWPT-previous-posts--links');
56
57 addProfileHistoryLink(linkContainer, 'forum', query1);
58 addProfileHistoryLink(linkContainer, 'all', query2);
59
60 container.appendChild(linkContainer);
61
62 mainCardContent.appendChild(container);
avm999632485a3e2021-09-08 22:18:38 +020063 new MDCTooltip(badgeTooltip);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020064}
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020065
66export function injectPreviousPostsLinksIfEnabled(nameElement) {
67 isOptionEnabled('history').then(isEnabled => {
68 if (isEnabled) injectPreviousPostsLinks(nameElement);
69 });
70}