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