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