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