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