Adrià Vilanova Martínez | e777047 | 2021-10-17 00:02:37 +0200 | [diff] [blame] | 1 | import {createImmuneLink} from '../../common/commonUtils.js'; |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 2 | import {escapeUsername, getAuthUser} from '../../common/communityConsoleUtils.js'; |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 3 | import {isOptionEnabled} from '../../common/optionsUtils.js'; |
| 4 | |
| 5 | import {createExtBadge, getNParent} from './utils/common.js'; |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 6 | |
| 7 | var authuser = getAuthUser(); |
| 8 | |
| 9 | function 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ínez | e777047 | 2021-10-17 00:02:37 +0200 | [diff] [blame] | 16 | var link = createImmuneLink(); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 17 | 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 | |
| 27 | export 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 | |
avm99963 | 2485a3e | 2021-09-08 22:18:38 +0200 | [diff] [blame] | 50 | let badge, badgeTooltip; |
| 51 | [badge, badgeTooltip] = createExtBadge(); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 52 | 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); |
avm99963 | 2485a3e | 2021-09-08 22:18:38 +0200 | [diff] [blame] | 63 | new MDCTooltip(badgeTooltip); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 64 | } |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 65 | |
| 66 | export function injectPreviousPostsLinksIfEnabled(nameElement) { |
| 67 | isOptionEnabled('history').then(isEnabled => { |
| 68 | if (isEnabled) injectPreviousPostsLinks(nameElement); |
| 69 | }); |
| 70 | } |