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'; |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 3 | |
avm99963 | 104bad3 | 2021-02-05 22:00:44 +0100 | [diff] [blame] | 4 | var authuser = (new URL(location.href)).searchParams.get('authuser') || '0'; |
| 5 | |
Adrià Vilanova Martínez | 56d4d18 | 2021-06-09 14:56:15 +0200 | [diff] [blame] | 6 | function getSearchUrl(query) { |
| 7 | var urlpart = encodeURIComponent('query=' + encodeURIComponent(query)); |
| 8 | var authuserpart = |
| 9 | (authuser == '0' ? '' : '?authuser=' + encodeURIComponent(authuser)); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 10 | return 'https://support.google.com/s/community/search/' + urlpart + |
Adrià Vilanova Martínez | 56d4d18 | 2021-06-09 14:56:15 +0200 | [diff] [blame] | 11 | authuserpart; |
| 12 | } |
| 13 | |
| 14 | function injectPreviousPostsLinksUnifiedProfile() { |
| 15 | var nameElement = |
| 16 | document.querySelector('.scTailwindUser_profileUsercardname'); |
| 17 | if (nameElement === null) { |
| 18 | console.error('[previousposts] Can\'t find username.'); |
| 19 | return; |
| 20 | } |
| 21 | |
| 22 | var name = escapeUsername(nameElement.textContent); |
| 23 | var filter = '(creator:"' + name + '" | replier:"' + name + '") forum:any'; |
| 24 | var url = getSearchUrl(filter); |
| 25 | |
| 26 | var links = document.createElement('div'); |
| 27 | links.classList.add('TWPT-user-profile__user-links'); |
| 28 | |
| 29 | var a = document.createElement('a'); |
| 30 | a.classList.add('TWPT-user-profile__user-link', 'TWPT-user-link'); |
| 31 | a.href = url; |
Adrià Vilanova Martínez | 3083a43 | 2021-06-15 04:24:22 +0200 | [diff] [blame] | 32 | a.target = '_parent'; |
Adrià Vilanova Martínez | 56d4d18 | 2021-06-09 14:56:15 +0200 | [diff] [blame] | 33 | a.setAttribute( |
| 34 | 'data-stats-id', 'user-posts-link--tw-power-tools-by-avm99963'); |
| 35 | |
| 36 | var badge = document.createElement('span'); |
| 37 | badge.classList.add('TWPT-badge'); |
| 38 | badge.setAttribute( |
| 39 | 'title', chrome.i18n.getMessage('inject_extension_badge_helper', [ |
| 40 | chrome.i18n.getMessage('appName') |
| 41 | ])); |
| 42 | |
| 43 | var badgeImg = document.createElement('img'); |
| 44 | badgeImg.src = |
| 45 | 'https://fonts.gstatic.com/s/i/materialicons/repeat/v6/24px.svg'; |
| 46 | |
| 47 | badge.appendChild(badgeImg); |
| 48 | a.appendChild(badge); |
| 49 | |
| 50 | var span = document.createElement('span'); |
| 51 | span.textContent = chrome.i18n.getMessage('inject_previousposts'); |
| 52 | |
| 53 | a.appendChild(span); |
| 54 | links.appendChild(a); |
| 55 | |
| 56 | var userDetailsNode = |
| 57 | document.querySelector('.scTailwindUser_profileUsercarddetails'); |
| 58 | if (userDetailsNode === null) { |
| 59 | console.error('[previousposts] Can\'t get user card details div.'); |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | userDetailsNode.parentNode.insertBefore(links, userDetailsNode.nextSibling); |
| 64 | } |
| 65 | |
| 66 | function injectPreviousPostsLinksOldProfile() { |
| 67 | if (document.querySelector('.user-profile__user-links') === null) { |
avm99963 | b69eb3d | 2020-08-20 02:03:44 +0200 | [diff] [blame] | 68 | var nameElement = document.querySelector('.user-profile__user-name'); |
avm99963 | d075725 | 2019-03-30 20:13:00 +0100 | [diff] [blame] | 69 | if (nameElement !== null) { |
avm99963 | 104bad3 | 2021-02-05 22:00:44 +0100 | [diff] [blame] | 70 | var profileLink = document.querySelector('.community-console'); |
| 71 | if (profileLink === null) { |
| 72 | console.error( |
| 73 | '[previousposts] ' + |
| 74 | 'The user is not a PE so we can\'t show the previous posts link.'); |
| 75 | return; |
| 76 | } |
| 77 | |
| 78 | var profileUrl = profileLink.href || ''; |
| 79 | var profileUrlSplit = profileUrl.split('/forum/'); |
| 80 | if (profileUrlSplit.length < 2) { |
| 81 | console.error('[previousposts] Can\'t get forum id.'); |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | var forumId = profileUrlSplit[1].split('/')[0]; |
avm99963 | 14116b0 | 2020-11-02 14:04:14 +0100 | [diff] [blame] | 86 | var name = escapeUsername(nameElement.textContent); |
avm99963 | 104bad3 | 2021-02-05 22:00:44 +0100 | [diff] [blame] | 87 | var filter = |
| 88 | '(creator:"' + name + '" | replier:"' + name + '") forum:' + forumId; |
Adrià Vilanova Martínez | 56d4d18 | 2021-06-09 14:56:15 +0200 | [diff] [blame] | 89 | var url = getSearchUrl(filter); |
avm99963 | 0bd07f6 | 2020-09-01 20:18:06 +0200 | [diff] [blame] | 90 | |
| 91 | var links = document.createElement('div'); |
| 92 | links.classList.add('user-profile__user-links'); |
| 93 | |
| 94 | var linkTitle = document.createElement('div'); |
| 95 | linkTitle.classList.add('user-profile__user-link-title'); |
| 96 | linkTitle.textContent = chrome.i18n.getMessage('inject_links'); |
| 97 | |
| 98 | links.appendChild(linkTitle); |
| 99 | |
| 100 | var ul = document.createElement('ul'); |
| 101 | |
| 102 | var li = document.createElement('li'); |
| 103 | li.classList.add('user-profile__user-link'); |
| 104 | |
| 105 | var a = document.createElement('a'); |
avm99963 | 0616775 | 2020-09-08 00:50:36 +0200 | [diff] [blame] | 106 | a.classList.add('user-profile__user-link', 'TWPT-user-link'); |
avm99963 | 0bd07f6 | 2020-09-01 20:18:06 +0200 | [diff] [blame] | 107 | a.href = url; |
| 108 | a.setAttribute( |
| 109 | 'data-stats-id', 'user-posts-link--tw-power-tools-by-avm99963'); |
avm99963 | 0bd07f6 | 2020-09-01 20:18:06 +0200 | [diff] [blame] | 110 | |
avm99963 | 0616775 | 2020-09-08 00:50:36 +0200 | [diff] [blame] | 111 | var badge = document.createElement('span'); |
| 112 | badge.classList.add('TWPT-badge'); |
| 113 | badge.setAttribute( |
| 114 | 'title', chrome.i18n.getMessage('inject_extension_badge_helper', [ |
| 115 | chrome.i18n.getMessage('appName') |
| 116 | ])); |
| 117 | |
| 118 | var badgeImg = document.createElement('img'); |
| 119 | badgeImg.src = |
| 120 | 'https://fonts.gstatic.com/s/i/materialicons/repeat/v6/24px.svg'; |
| 121 | |
| 122 | badge.appendChild(badgeImg); |
| 123 | a.appendChild(badge); |
| 124 | |
| 125 | var span = document.createElement('span'); |
| 126 | span.textContent = chrome.i18n.getMessage('inject_previousposts'); |
| 127 | |
| 128 | a.appendChild(span); |
avm99963 | 0bd07f6 | 2020-09-01 20:18:06 +0200 | [diff] [blame] | 129 | li.appendChild(a); |
| 130 | ul.appendChild(li); |
| 131 | links.appendChild(ul); |
| 132 | |
avm99963 | b69eb3d | 2020-08-20 02:03:44 +0200 | [diff] [blame] | 133 | document.querySelector('.user-profile__user-details-container') |
avm99963 | 0bd07f6 | 2020-09-01 20:18:06 +0200 | [diff] [blame] | 134 | .appendChild(links); |
avm99963 | 104bad3 | 2021-02-05 22:00:44 +0100 | [diff] [blame] | 135 | } else { |
| 136 | console.error('[previousposts] Can\'t find username.'); |
avm99963 | d075725 | 2019-03-30 20:13:00 +0100 | [diff] [blame] | 137 | } |
| 138 | } |
Adrià Vilanova Martínez | 56d4d18 | 2021-06-09 14:56:15 +0200 | [diff] [blame] | 139 | } |
| 140 | |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 141 | getOptions('history').then(options => { |
| 142 | if (options?.history) { |
Adrià Vilanova Martínez | 56d4d18 | 2021-06-09 14:56:15 +0200 | [diff] [blame] | 143 | if (document.getElementById('unified-user-profile') !== null) |
| 144 | injectPreviousPostsLinksUnifiedProfile(); |
| 145 | else |
| 146 | injectPreviousPostsLinksOldProfile(); |
| 147 | } |
avm99963 | d075725 | 2019-03-30 20:13:00 +0100 | [diff] [blame] | 148 | }); |