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