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