Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 1 | import {escapeUsername} from '../common/communityConsoleUtils.js'; |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 2 | import {getOptions} from '../common/optionsUtils.js'; |
avm99963 | 2485a3e | 2021-09-08 22:18:38 +0200 | [diff] [blame] | 3 | import {createPlainTooltip} from '../common/tooltip.js'; |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 4 | |
Adrià Vilanova Martínez | 1f65252 | 2021-10-14 00:23:23 +0200 | [diff] [blame] | 5 | import {getSearchUrl, injectPreviousPostsLinksUnifiedProfile} from './utilsCommon/unifiedProfiles.js'; |
avm99963 | 104bad3 | 2021-02-05 22:00:44 +0100 | [diff] [blame] | 6 | |
Adrià Vilanova Martínez | 1f65252 | 2021-10-14 00:23:23 +0200 | [diff] [blame] | 7 | // TODO(b/twpowertools/80): Remove this code. |
Adrià Vilanova Martínez | 56d4d18 | 2021-06-09 14:56:15 +0200 | [diff] [blame] | 8 | function injectPreviousPostsLinksOldProfile() { |
| 9 | if (document.querySelector('.user-profile__user-links') === null) { |
avm99963 | b69eb3d | 2020-08-20 02:03:44 +0200 | [diff] [blame] | 10 | var nameElement = document.querySelector('.user-profile__user-name'); |
avm99963 | d075725 | 2019-03-30 20:13:00 +0100 | [diff] [blame] | 11 | if (nameElement !== null) { |
avm99963 | 104bad3 | 2021-02-05 22:00:44 +0100 | [diff] [blame] | 12 | var profileLink = document.querySelector('.community-console'); |
| 13 | if (profileLink === null) { |
| 14 | console.error( |
| 15 | '[previousposts] ' + |
| 16 | 'The user is not a PE so we can\'t show the previous posts link.'); |
| 17 | return; |
| 18 | } |
| 19 | |
| 20 | var profileUrl = profileLink.href || ''; |
| 21 | var profileUrlSplit = profileUrl.split('/forum/'); |
| 22 | if (profileUrlSplit.length < 2) { |
| 23 | console.error('[previousposts] Can\'t get forum id.'); |
| 24 | return; |
| 25 | } |
| 26 | |
| 27 | var forumId = profileUrlSplit[1].split('/')[0]; |
avm99963 | 14116b0 | 2020-11-02 14:04:14 +0100 | [diff] [blame] | 28 | var name = escapeUsername(nameElement.textContent); |
avm99963 | 104bad3 | 2021-02-05 22:00:44 +0100 | [diff] [blame] | 29 | var filter = |
| 30 | '(creator:"' + name + '" | replier:"' + name + '") forum:' + forumId; |
Adrià Vilanova Martínez | 56d4d18 | 2021-06-09 14:56:15 +0200 | [diff] [blame] | 31 | var url = getSearchUrl(filter); |
avm99963 | 0bd07f6 | 2020-09-01 20:18:06 +0200 | [diff] [blame] | 32 | |
| 33 | var links = document.createElement('div'); |
| 34 | links.classList.add('user-profile__user-links'); |
| 35 | |
| 36 | var linkTitle = document.createElement('div'); |
| 37 | linkTitle.classList.add('user-profile__user-link-title'); |
| 38 | linkTitle.textContent = chrome.i18n.getMessage('inject_links'); |
| 39 | |
| 40 | links.appendChild(linkTitle); |
| 41 | |
| 42 | var ul = document.createElement('ul'); |
| 43 | |
| 44 | var li = document.createElement('li'); |
| 45 | li.classList.add('user-profile__user-link'); |
| 46 | |
| 47 | var a = document.createElement('a'); |
avm99963 | 0616775 | 2020-09-08 00:50:36 +0200 | [diff] [blame] | 48 | a.classList.add('user-profile__user-link', 'TWPT-user-link'); |
avm99963 | 0bd07f6 | 2020-09-01 20:18:06 +0200 | [diff] [blame] | 49 | a.href = url; |
| 50 | a.setAttribute( |
| 51 | 'data-stats-id', 'user-posts-link--tw-power-tools-by-avm99963'); |
avm99963 | 0bd07f6 | 2020-09-01 20:18:06 +0200 | [diff] [blame] | 52 | |
avm99963 | 0616775 | 2020-09-08 00:50:36 +0200 | [diff] [blame] | 53 | var badge = document.createElement('span'); |
| 54 | badge.classList.add('TWPT-badge'); |
avm99963 | 0616775 | 2020-09-08 00:50:36 +0200 | [diff] [blame] | 55 | |
| 56 | var badgeImg = document.createElement('img'); |
| 57 | badgeImg.src = |
| 58 | 'https://fonts.gstatic.com/s/i/materialicons/repeat/v6/24px.svg'; |
| 59 | |
| 60 | badge.appendChild(badgeImg); |
| 61 | a.appendChild(badge); |
| 62 | |
| 63 | var span = document.createElement('span'); |
| 64 | span.textContent = chrome.i18n.getMessage('inject_previousposts'); |
| 65 | |
| 66 | a.appendChild(span); |
avm99963 | 0bd07f6 | 2020-09-01 20:18:06 +0200 | [diff] [blame] | 67 | li.appendChild(a); |
| 68 | ul.appendChild(li); |
| 69 | links.appendChild(ul); |
| 70 | |
avm99963 | b69eb3d | 2020-08-20 02:03:44 +0200 | [diff] [blame] | 71 | document.querySelector('.user-profile__user-details-container') |
avm99963 | 0bd07f6 | 2020-09-01 20:18:06 +0200 | [diff] [blame] | 72 | .appendChild(links); |
avm99963 | 2485a3e | 2021-09-08 22:18:38 +0200 | [diff] [blame] | 73 | |
| 74 | createPlainTooltip( |
| 75 | badge, chrome.i18n.getMessage('inject_extension_badge_helper', [ |
| 76 | chrome.i18n.getMessage('appName') |
| 77 | ])); |
avm99963 | 104bad3 | 2021-02-05 22:00:44 +0100 | [diff] [blame] | 78 | } else { |
| 79 | console.error('[previousposts] Can\'t find username.'); |
avm99963 | d075725 | 2019-03-30 20:13:00 +0100 | [diff] [blame] | 80 | } |
| 81 | } |
Adrià Vilanova Martínez | 56d4d18 | 2021-06-09 14:56:15 +0200 | [diff] [blame] | 82 | } |
| 83 | |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 84 | getOptions('history').then(options => { |
| 85 | if (options?.history) { |
Adrià Vilanova Martínez | 56d4d18 | 2021-06-09 14:56:15 +0200 | [diff] [blame] | 86 | if (document.getElementById('unified-user-profile') !== null) |
Adrià Vilanova Martínez | 1f65252 | 2021-10-14 00:23:23 +0200 | [diff] [blame] | 87 | injectPreviousPostsLinksUnifiedProfile(/* isCommunityConsole = */ false); |
Adrià Vilanova Martínez | 56d4d18 | 2021-06-09 14:56:15 +0200 | [diff] [blame] | 88 | else |
| 89 | injectPreviousPostsLinksOldProfile(); |
| 90 | } |
avm99963 | d075725 | 2019-03-30 20:13:00 +0100 | [diff] [blame] | 91 | }); |