avm99963 | e6166ed | 2021-08-26 10:45:17 +0200 | [diff] [blame] | 1 | import {CCApi} from '../common/api.js'; |
Adrià Vilanova Martínez | e777047 | 2021-10-17 00:02:37 +0200 | [diff] [blame] | 2 | import {createImmuneLink} from '../common/commonUtils.js'; |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 3 | import {escapeUsername} from '../common/communityConsoleUtils.js'; |
avm99963 | 2485a3e | 2021-09-08 22:18:38 +0200 | [diff] [blame] | 4 | import {createPlainTooltip} from '../common/tooltip.js'; |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 5 | |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 6 | var CCProfileRegex = |
Adrià Vilanova Martínez | cb28fd9 | 2022-01-28 12:41:52 +0100 | [diff] [blame] | 7 | /^(?:https:\/\/support\.google\.com)?\/s\/community(?:\/forum\/[0-9]*)?\/user\/(?:[0-9]+)(?:\?.*)?$/; |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 8 | var CCRegex = /^https:\/\/support\.google\.com\/s\/community/; |
| 9 | |
| 10 | const OP_FIRST_POST = 0; |
| 11 | const OP_OTHER_POSTS_READ = 1; |
| 12 | const OP_OTHER_POSTS_UNREAD = 2; |
| 13 | |
| 14 | const OPClasses = { |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 15 | 0: 'first-post', |
| 16 | 1: 'other-posts-read', |
| 17 | 2: 'other-posts-unread', |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 18 | }; |
| 19 | |
| 20 | const OPi18n = { |
| 21 | 0: 'first_post', |
| 22 | 1: 'other_posts_read', |
| 23 | 2: 'other_posts_unread', |
| 24 | }; |
| 25 | |
Adrià Vilanova Martínez | ad6bedf | 2022-01-28 10:41:10 +0100 | [diff] [blame] | 26 | const UI_COMMUNITY_CONSOLE = 0; |
| 27 | const UI_TW_LEGACY = 1; |
| 28 | const UI_TW_INTEROP = 2; |
Adrià Vilanova Martínez | cb28fd9 | 2022-01-28 12:41:52 +0100 | [diff] [blame] | 29 | const UI_COMMUNITY_CONSOLE_INTEROP = 3; |
Adrià Vilanova Martínez | c8f6630 | 2023-07-06 21:38:50 +0200 | [diff] [blame] | 30 | const UI_TW_INTEROP_V2 = 4; |
| 31 | const UI_COMMUNITY_CONSOLE_INTEROP_V2 = 5; |
Adrià Vilanova Martínez | ad6bedf | 2022-01-28 10:41:10 +0100 | [diff] [blame] | 32 | |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 33 | // Filter used as a workaround to speed up the ViewForum request. |
| 34 | const FILTER_ALL_LANGUAGES = |
| 35 | 'lang:(ar | bg | ca | "zh-hk" | "zh-cn" | "zh-tw" | hr | cs | da | nl | en | "en-au" | "en-gb" | et | fil | fi | fr | de | el | iw | hi | hu | id | it | ja | ko | lv | lt | ms | no | pl | "pt-br" | "pt-pt" | ro | ru | sr | sk | sl | es | "es-419" | sv | th | tr | uk | vi)'; |
| 36 | |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 37 | const numPostsForumArraysToSum = [3, 4]; |
| 38 | |
Adrià Vilanova Martínez | c8f6630 | 2023-07-06 21:38:50 +0200 | [diff] [blame] | 39 | const CC_PROFILE_LINK_TYPES = [ |
| 40 | { |
| 41 | // Legacy |
| 42 | ui: UI_COMMUNITY_CONSOLE, |
| 43 | nodeSelector: 'ec-question ec-message-header .name-section ec-user-link a', |
| 44 | }, |
| 45 | { |
| 46 | // Interop (legacy) |
| 47 | ui: UI_COMMUNITY_CONSOLE_INTEROP, |
| 48 | nodeSelector: 'sc-tailwind-thread-question-question-card ' + |
| 49 | 'sc-tailwind-thread-post_header-user-info ' + |
| 50 | '.scTailwindThreadPost_headerUserinfoname a', |
| 51 | }, |
| 52 | { |
| 53 | // Interop v2 |
| 54 | ui: UI_COMMUNITY_CONSOLE_INTEROP_V2, |
| 55 | nodeSelector: 'sc-tailwind-thread-question-question-card ' + |
| 56 | 'sc-tailwind-thread-post_header-user-info > ' + |
| 57 | '.scTailwindThreadPost_headerUserinforoot > a', |
| 58 | }, |
| 59 | ]; |
| 60 | |
| 61 | const TW_PROFILE_LINK_TYPES = [ |
| 62 | { |
| 63 | // Legacy |
| 64 | ui: UI_TW_LEGACY, |
| 65 | nodeSelector: '.thread-question a.user-info-display-name', |
| 66 | }, |
| 67 | { |
| 68 | // Interop (legacy) |
| 69 | ui: UI_TW_INTEROP, |
| 70 | nodeSelector: 'sc-tailwind-thread-question-question-card ' + |
| 71 | 'sc-tailwind-thread-post_header-user-info ' + |
| 72 | '.scTailwindThreadPost_headerUserinfoname a', |
| 73 | }, |
| 74 | { |
| 75 | // Interop v2 |
| 76 | ui: UI_TW_INTEROP_V2, |
| 77 | nodeSelector: 'sc-tailwind-thread-question-question-card ' + |
| 78 | 'sc-tailwind-thread-post_header-user-info > ' + |
| 79 | '.scTailwindThreadPost_headerUserinforoot > a', |
| 80 | }, |
| 81 | ]; |
| 82 | |
avm99963 | a2945b6 | 2020-11-27 00:32:02 +0100 | [diff] [blame] | 83 | var authuser = null; |
| 84 | |
Adrià Vilanova Martínez | cb28fd9 | 2022-01-28 12:41:52 +0100 | [diff] [blame] | 85 | function isCommunityConsole(ui) { |
Adrià Vilanova Martínez | c8f6630 | 2023-07-06 21:38:50 +0200 | [diff] [blame] | 86 | return ui === UI_COMMUNITY_CONSOLE || ui === UI_COMMUNITY_CONSOLE_INTEROP || |
| 87 | ui === UI_COMMUNITY_CONSOLE_INTEROP_V2; |
Adrià Vilanova Martínez | cb28fd9 | 2022-01-28 12:41:52 +0100 | [diff] [blame] | 88 | } |
| 89 | |
Adrià Vilanova Martínez | c8f6630 | 2023-07-06 21:38:50 +0200 | [diff] [blame] | 90 | function isInteropV1(ui) { |
Adrià Vilanova Martínez | cb28fd9 | 2022-01-28 12:41:52 +0100 | [diff] [blame] | 91 | return ui === UI_TW_INTEROP || ui === UI_COMMUNITY_CONSOLE_INTEROP; |
| 92 | } |
| 93 | |
Adrià Vilanova Martínez | c8f6630 | 2023-07-06 21:38:50 +0200 | [diff] [blame] | 94 | function isInteropV2(ui) { |
| 95 | return ui === UI_TW_INTEROP_V2 || ui === UI_COMMUNITY_CONSOLE_INTEROP_V2; |
| 96 | } |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 97 | |
avm99963 | a2945b6 | 2020-11-27 00:32:02 +0100 | [diff] [blame] | 98 | function getPosts(query, forumId) { |
avm99963 | e6166ed | 2021-08-26 10:45:17 +0200 | [diff] [blame] | 99 | return CCApi( |
| 100 | 'ViewForum', { |
| 101 | '1': forumId, |
| 102 | '2': { |
| 103 | '1': { |
| 104 | '2': 5, |
| 105 | }, |
| 106 | '2': { |
| 107 | '1': 1, |
| 108 | '2': true, |
| 109 | }, |
| 110 | '12': query, |
| 111 | }, |
avm99963 | a2945b6 | 2020-11-27 00:32:02 +0100 | [diff] [blame] | 112 | }, |
avm99963 | e6166ed | 2021-08-26 10:45:17 +0200 | [diff] [blame] | 113 | /* authenticated = */ true, authuser); |
avm99963 | a2945b6 | 2020-11-27 00:32:02 +0100 | [diff] [blame] | 114 | } |
| 115 | |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 116 | function getProfile(userId, forumId) { |
avm99963 | e6166ed | 2021-08-26 10:45:17 +0200 | [diff] [blame] | 117 | return CCApi( |
| 118 | 'ViewUser', { |
| 119 | '1': userId, |
| 120 | '2': 0, |
| 121 | '3': forumId, |
| 122 | '4': { |
| 123 | '20': true, |
| 124 | }, |
| 125 | }, |
| 126 | /* authenticated = */ true, authuser); |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 127 | } |
| 128 | |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 129 | // Source: |
| 130 | // https://stackoverflow.com/questions/33063774/communication-from-an-injected-script-to-the-content-script-with-a-response |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 131 | var contentScriptRequest = (function() { |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 132 | var requestId = 0; |
avm99963 | 3e23888 | 2020-12-07 18:38:54 +0100 | [diff] [blame] | 133 | var prefix = 'TWPT-profileindicator'; |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 134 | |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 135 | function sendRequest(data) { |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 136 | var id = requestId++; |
| 137 | |
Adrià Vilanova Martínez | 78ff406 | 2023-07-06 20:03:29 +0200 | [diff] [blame] | 138 | return new Promise(function(resolve) { |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 139 | var listener = function(evt) { |
avm99963 | 3e23888 | 2020-12-07 18:38:54 +0100 | [diff] [blame] | 140 | if (evt.source === window && evt.data && evt.data.prefix === prefix && |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 141 | evt.data.requestId == id) { |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 142 | // Deregister self |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 143 | window.removeEventListener('message', listener); |
| 144 | resolve(evt.data.data); |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 145 | } |
| 146 | }; |
| 147 | |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 148 | window.addEventListener('message', listener); |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 149 | |
avm99963 | 3e23888 | 2020-12-07 18:38:54 +0100 | [diff] [blame] | 150 | var payload = {data, id, prefix}; |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 151 | |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 152 | window.dispatchEvent( |
| 153 | new CustomEvent('TWPT_sendRequest', {detail: payload})); |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 154 | }); |
| 155 | } |
| 156 | |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 157 | return {sendRequest: sendRequest}; |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 158 | })(); |
| 159 | |
Adrià Vilanova Martínez | c8f6630 | 2023-07-06 21:38:50 +0200 | [diff] [blame] | 160 | // Inject the indicator dot/badge to the appropriate position. |
| 161 | function injectIndicator(sourceNode, indicatorEl, ui) { |
| 162 | if (!isInteropV2(ui)) { |
| 163 | sourceNode.parentNode.appendChild(indicatorEl); |
| 164 | return; |
| 165 | } |
| 166 | |
| 167 | let username = |
| 168 | sourceNode.querySelector('.scTailwindThreadPost_headerUserinfoname'); |
| 169 | username.append(indicatorEl); |
| 170 | } |
| 171 | |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 172 | // Create profile indicator dot with a loading state, or return the numPosts |
| 173 | // badge if it is already created. |
Adrià Vilanova Martínez | ad6bedf | 2022-01-28 10:41:10 +0100 | [diff] [blame] | 174 | function createIndicatorDot(sourceNode, searchURL, options, ui) { |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 175 | if (options.numPosts) return document.querySelector('.num-posts-indicator'); |
avm99963 | a1b23b6 | 2020-09-01 14:32:34 +0200 | [diff] [blame] | 176 | var dotContainer = document.createElement('div'); |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 177 | dotContainer.classList.add('profile-indicator', 'profile-indicator--loading'); |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 178 | |
Adrià Vilanova Martínez | cb28fd9 | 2022-01-28 12:41:52 +0100 | [diff] [blame] | 179 | var dotLink = (isCommunityConsole(ui)) ? createImmuneLink() : |
| 180 | document.createElement('a'); |
Adrià Vilanova Martínez | c8f6630 | 2023-07-06 21:38:50 +0200 | [diff] [blame] | 181 | dotLink.classList.add( |
| 182 | 'profile-indicator-link', 'profile-indicator-link--dot'); |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 183 | dotLink.href = searchURL; |
| 184 | dotLink.innerText = '●'; |
| 185 | |
| 186 | dotContainer.appendChild(dotLink); |
Adrià Vilanova Martínez | c8f6630 | 2023-07-06 21:38:50 +0200 | [diff] [blame] | 187 | injectIndicator(sourceNode, dotContainer, ui); |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 188 | |
avm99963 | 2485a3e | 2021-09-08 22:18:38 +0200 | [diff] [blame] | 189 | contentScriptRequest |
| 190 | .sendRequest({ |
| 191 | 'action': 'geti18nMessage', |
| 192 | 'msg': 'inject_profileindicator_loading' |
| 193 | }) |
| 194 | .then(string => createPlainTooltip(dotContainer, string)); |
| 195 | |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 196 | return dotContainer; |
| 197 | } |
| 198 | |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 199 | // Create badge indicating the number of posts with a loading state |
Adrià Vilanova Martínez | ad6bedf | 2022-01-28 10:41:10 +0100 | [diff] [blame] | 200 | function createNumPostsBadge(sourceNode, searchURL, ui) { |
Adrià Vilanova Martínez | cb28fd9 | 2022-01-28 12:41:52 +0100 | [diff] [blame] | 201 | var link = (isCommunityConsole(ui)) ? createImmuneLink() : |
| 202 | document.createElement('a'); |
Adrià Vilanova Martínez | c8f6630 | 2023-07-06 21:38:50 +0200 | [diff] [blame] | 203 | link.classList.add( |
| 204 | 'profile-indicator-link', 'profile-indicator-link--num-posts'); |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 205 | link.href = searchURL; |
| 206 | |
| 207 | var numPostsContainer = document.createElement('div'); |
| 208 | numPostsContainer.classList.add( |
| 209 | 'num-posts-indicator', 'num-posts-indicator--loading'); |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 210 | |
| 211 | var numPostsSpan = document.createElement('span'); |
| 212 | numPostsSpan.classList.add('num-posts-indicator--num'); |
| 213 | |
| 214 | numPostsContainer.appendChild(numPostsSpan); |
| 215 | link.appendChild(numPostsContainer); |
Adrià Vilanova Martínez | c8f6630 | 2023-07-06 21:38:50 +0200 | [diff] [blame] | 216 | injectIndicator(sourceNode, link, ui); |
avm99963 | 2485a3e | 2021-09-08 22:18:38 +0200 | [diff] [blame] | 217 | |
| 218 | contentScriptRequest |
| 219 | .sendRequest({ |
| 220 | 'action': 'geti18nMessage', |
| 221 | 'msg': 'inject_profileindicator_loading' |
| 222 | }) |
| 223 | .then(string => createPlainTooltip(numPostsContainer, string)); |
| 224 | |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 225 | return numPostsContainer; |
| 226 | } |
| 227 | |
avm99963 | 13658b9 | 2020-12-02 00:02:53 +0100 | [diff] [blame] | 228 | // Set the badge text |
| 229 | function setNumPostsBadge(badge, text) { |
| 230 | badge.classList.remove('num-posts-indicator--loading'); |
| 231 | badge.querySelector('span').classList.remove( |
| 232 | 'num-posts-indicator--num--loading'); |
| 233 | badge.querySelector('span').textContent = text; |
| 234 | } |
| 235 | |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 236 | // Get options and then handle all the indicators |
Adrià Vilanova Martínez | ad6bedf | 2022-01-28 10:41:10 +0100 | [diff] [blame] | 237 | function getOptionsAndHandleIndicators(sourceNode, ui) { |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 238 | contentScriptRequest.sendRequest({'action': 'getProfileIndicatorOptions'}) |
Adrià Vilanova Martínez | ad6bedf | 2022-01-28 10:41:10 +0100 | [diff] [blame] | 239 | .then(options => handleIndicators(sourceNode, ui, options)); |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | // Handle the profile indicator dot |
Adrià Vilanova Martínez | ad6bedf | 2022-01-28 10:41:10 +0100 | [diff] [blame] | 243 | function handleIndicators(sourceNode, ui, options) { |
| 244 | let nameEl; |
| 245 | if (ui === UI_COMMUNITY_CONSOLE) |
| 246 | nameEl = sourceNode.querySelector('.name-text'); |
| 247 | if (ui === UI_TW_LEGACY) nameEl = sourceNode.querySelector('span'); |
Adrià Vilanova Martínez | c8f6630 | 2023-07-06 21:38:50 +0200 | [diff] [blame] | 248 | if (isInteropV1(ui)) nameEl = sourceNode; |
| 249 | if (isInteropV2(ui)) |
| 250 | nameEl = |
| 251 | sourceNode.querySelector('.scTailwindThreadPost_headerUserinfoname'); |
Adrià Vilanova Martínez | ad6bedf | 2022-01-28 10:41:10 +0100 | [diff] [blame] | 252 | var escapedUsername = escapeUsername(nameEl.textContent); |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 253 | |
Adrià Vilanova Martínez | 78ff406 | 2023-07-06 20:03:29 +0200 | [diff] [blame] | 254 | var threadLink; |
Adrià Vilanova Martínez | cb28fd9 | 2022-01-28 12:41:52 +0100 | [diff] [blame] | 255 | if (isCommunityConsole(ui)) { |
Adrià Vilanova Martínez | 78ff406 | 2023-07-06 20:03:29 +0200 | [diff] [blame] | 256 | threadLink = document.location.href; |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 257 | } else { |
| 258 | var CCLink = document.getElementById('onebar-community-console'); |
| 259 | if (CCLink === null) { |
| 260 | console.error( |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 261 | '[opindicator] The user is not a PE so the dot indicator cannot be shown in TW.'); |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 262 | return; |
| 263 | } |
Adrià Vilanova Martínez | 78ff406 | 2023-07-06 20:03:29 +0200 | [diff] [blame] | 264 | threadLink = CCLink.href; |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | var forumUrlSplit = threadLink.split('/forum/'); |
| 268 | if (forumUrlSplit.length < 2) { |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 269 | console.error('[opindicator] Can\'t get forum id.'); |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 270 | return; |
| 271 | } |
| 272 | |
| 273 | var forumId = forumUrlSplit[1].split('/')[0]; |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 274 | |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 275 | var query = '(replier:"' + escapedUsername + '" | creator:"' + |
Adrià Vilanova Martínez | 2b2d69c | 2022-01-28 15:03:03 +0100 | [diff] [blame] | 276 | escapedUsername + '") ' + FILTER_ALL_LANGUAGES + ' forum:' + forumId; |
| 277 | var encodedQuery = encodeURIComponent(query); |
avm99963 | a2945b6 | 2020-11-27 00:32:02 +0100 | [diff] [blame] | 278 | var authuserPart = |
Adrià Vilanova Martínez | ad6bedf | 2022-01-28 10:41:10 +0100 | [diff] [blame] | 279 | (authuser == '0' ? '' : '?authuser=' + encodeURIComponent(authuser)); |
| 280 | var searchURL = 'https://support.google.com/s/community/search/' + |
| 281 | encodeURIComponent('query=' + encodedQuery) + authuserPart; |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 282 | |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 283 | if (options.numPosts) { |
| 284 | var profileURL = new URL(sourceNode.href); |
Adrià Vilanova Martínez | cb28fd9 | 2022-01-28 12:41:52 +0100 | [diff] [blame] | 285 | var userId = profileURL.pathname |
| 286 | .split(isCommunityConsole(ui) ? 'user/' : 'profile/')[1] |
| 287 | .split('?')[0] |
| 288 | .split('/')[0]; |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 289 | |
Adrià Vilanova Martínez | ad6bedf | 2022-01-28 10:41:10 +0100 | [diff] [blame] | 290 | var numPostsContainer = createNumPostsBadge(sourceNode, searchURL, ui); |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 291 | |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 292 | getProfile(userId, forumId) |
| 293 | .then(res => { |
| 294 | if (!('1' in res) || !('2' in res[1])) { |
| 295 | throw new Error('Unexpected profile response.'); |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 296 | } |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 297 | |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 298 | contentScriptRequest.sendRequest({'action': 'getNumPostMonths'}) |
| 299 | .then(months => { |
| 300 | if (!options.indicatorDot) |
| 301 | contentScriptRequest |
| 302 | .sendRequest({ |
| 303 | 'action': 'geti18nMessage', |
| 304 | 'msg': 'inject_profileindicatoralt_numposts', |
| 305 | 'placeholders': [months] |
| 306 | }) |
| 307 | .then( |
| 308 | string => |
avm99963 | 2485a3e | 2021-09-08 22:18:38 +0200 | [diff] [blame] | 309 | createPlainTooltip(numPostsContainer, string)); |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 310 | |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 311 | var numPosts = 0; |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 312 | |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 313 | for (const index of numPostsForumArraysToSum) { |
| 314 | if (!(index in res[1][2])) { |
| 315 | throw new Error('Unexpected profile response.'); |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 316 | } |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 317 | |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 318 | var i = 0; |
| 319 | for (const month of res[1][2][index].reverse()) { |
| 320 | if (i == months) break; |
| 321 | numPosts += month[3] || 0; |
| 322 | ++i; |
| 323 | } |
| 324 | } |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 325 | |
avm99963 | 13658b9 | 2020-12-02 00:02:53 +0100 | [diff] [blame] | 326 | setNumPostsBadge(numPostsContainer, numPosts); |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 327 | }) |
| 328 | .catch( |
| 329 | err => console.error('[opindicator] Unexpected error.', err)); |
| 330 | }) |
avm99963 | 13658b9 | 2020-12-02 00:02:53 +0100 | [diff] [blame] | 331 | .catch(err => { |
| 332 | console.error( |
| 333 | '[opindicator] Unexpected error. Couldn\'t load profile.', err); |
| 334 | setNumPostsBadge(numPostsContainer, '?'); |
| 335 | }); |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | if (options.indicatorDot) { |
Adrià Vilanova Martínez | ad6bedf | 2022-01-28 10:41:10 +0100 | [diff] [blame] | 339 | var dotContainer = createIndicatorDot(sourceNode, searchURL, options, ui); |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 340 | |
| 341 | // Query threads in order to see what state the indicator should be in |
| 342 | getPosts(query, forumId) |
| 343 | .then(res => { |
avm99963 | 9f586f4 | 2021-02-05 12:37:51 +0100 | [diff] [blame] | 344 | // Throw an error when the replies array is not present in the reply. |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 345 | if (!('1' in res) || !('2' in res['1'])) { |
avm99963 | 9f586f4 | 2021-02-05 12:37:51 +0100 | [diff] [blame] | 346 | // Throw a different error when the numThreads field exists and is |
| 347 | // equal to 0. This reply can be received, but is enexpected, |
| 348 | // because we know that the user has replied in at least 1 thread |
| 349 | // (the current one). |
| 350 | if (('1' in res) && ('4' in res['1']) && res['1']['4'] == 0) |
| 351 | throw new Error( |
| 352 | 'Thread list is empty ' + |
| 353 | '(but the OP has participated in this thread, ' + |
| 354 | 'so it shouldn\'t be empty).'); |
| 355 | |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 356 | throw new Error('Unexpected thread list response.'); |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | // Current thread ID |
| 360 | var threadUrlSplit = threadLink.split('/thread/'); |
| 361 | if (threadUrlSplit.length < 2) |
| 362 | throw new Error('Can\'t get thread id.'); |
| 363 | |
Adrià Vilanova Martínez | 2b2d69c | 2022-01-28 15:03:03 +0100 | [diff] [blame] | 364 | var currId = threadUrlSplit[1].split('?')[0].split('/')[0]; |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 365 | |
| 366 | var OPStatus = OP_FIRST_POST; |
| 367 | |
| 368 | for (const thread of res['1']['2']) { |
| 369 | var id = thread['2']['1']['1'] || undefined; |
| 370 | if (id === undefined || id == currId) continue; |
| 371 | |
| 372 | var isRead = thread['6'] || false; |
| 373 | if (isRead) |
| 374 | OPStatus = Math.max(OP_OTHER_POSTS_READ, OPStatus); |
| 375 | else |
| 376 | OPStatus = Math.max(OP_OTHER_POSTS_UNREAD, OPStatus); |
| 377 | } |
| 378 | |
| 379 | var dotContainerPrefix = |
| 380 | (options.numPosts ? 'num-posts-indicator' : 'profile-indicator'); |
| 381 | |
| 382 | if (!options.numPosts) |
| 383 | dotContainer.classList.remove(dotContainerPrefix + '--loading'); |
| 384 | dotContainer.classList.add( |
| 385 | dotContainerPrefix + '--' + OPClasses[OPStatus]); |
| 386 | contentScriptRequest |
| 387 | .sendRequest({ |
| 388 | 'action': 'geti18nMessage', |
| 389 | 'msg': 'inject_profileindicator_' + OPi18n[OPStatus] |
| 390 | }) |
avm99963 | 2485a3e | 2021-09-08 22:18:38 +0200 | [diff] [blame] | 391 | .then(string => createPlainTooltip(dotContainer, string)); |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 392 | }) |
| 393 | .catch( |
| 394 | err => console.error( |
| 395 | '[opindicator] Unexpected error. Couldn\'t load recent posts.', |
| 396 | err)); |
| 397 | } |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | if (CCRegex.test(location.href)) { |
| 401 | // We are in the Community Console |
avm99963 | a2945b6 | 2020-11-27 00:32:02 +0100 | [diff] [blame] | 402 | var startup = |
| 403 | JSON.parse(document.querySelector('html').getAttribute('data-startup')); |
| 404 | |
| 405 | authuser = startup[2][1] || '0'; |
| 406 | |
avm99963 | 8981288 | 2021-02-02 20:51:25 +0100 | [diff] [blame] | 407 | // When the OP's username is found, call getOptionsAndHandleIndicators |
Adrià Vilanova Martínez | c8f6630 | 2023-07-06 21:38:50 +0200 | [diff] [blame] | 408 | var mutationCallback = |
| 409 | function(mutationList) { |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 410 | mutationList.forEach((mutation) => { |
Adrià Vilanova Martínez | c8f6630 | 2023-07-06 21:38:50 +0200 | [diff] [blame] | 411 | if (mutation.type !== 'childList') return; |
| 412 | |
| 413 | mutation.addedNodes.forEach(function(node) { |
| 414 | let isProfileLink = node.tagName == 'A' && ('href' in node) && |
| 415 | CCProfileRegex.test(node.href); |
| 416 | if (!isProfileLink) return; |
| 417 | |
| 418 | for (const linkType of CC_PROFILE_LINK_TYPES) { |
| 419 | if (node.matches(linkType.nodeSelector)) { |
Adrià Vilanova Martínez | 5b98793 | 2023-11-16 02:18:44 +0100 | [diff] [blame] | 420 | console.debug('Handling profile indicator via mutation callback.'); |
Adrià Vilanova Martínez | c8f6630 | 2023-07-06 21:38:50 +0200 | [diff] [blame] | 421 | getOptionsAndHandleIndicators(node, linkType.ui); |
| 422 | break; |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 423 | } |
Adrià Vilanova Martínez | c8f6630 | 2023-07-06 21:38:50 +0200 | [diff] [blame] | 424 | } |
| 425 | }); |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 426 | }); |
Adrià Vilanova Martínez | 78ff406 | 2023-07-06 20:03:29 +0200 | [diff] [blame] | 427 | } |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 428 | |
| 429 | var observerOptions = { |
| 430 | childList: true, |
| 431 | subtree: true, |
| 432 | } |
| 433 | |
avm99963 | 8981288 | 2021-02-02 20:51:25 +0100 | [diff] [blame] | 434 | // Before starting the mutation Observer, check if the OP's username link is |
| 435 | // already part of the page |
Adrià Vilanova Martínez | c8f6630 | 2023-07-06 21:38:50 +0200 | [diff] [blame] | 436 | for (const linkType of CC_PROFILE_LINK_TYPES) { |
| 437 | let node = document.querySelector(linkType.nodeSelector); |
Adrià Vilanova Martínez | cb28fd9 | 2022-01-28 12:41:52 +0100 | [diff] [blame] | 438 | if (node !== null) { |
Adrià Vilanova Martínez | 5b98793 | 2023-11-16 02:18:44 +0100 | [diff] [blame] | 439 | console.debug('Handling profile indicator via first check.'); |
Adrià Vilanova Martínez | c8f6630 | 2023-07-06 21:38:50 +0200 | [diff] [blame] | 440 | getOptionsAndHandleIndicators(node, linkType.ui); |
| 441 | break; |
Adrià Vilanova Martínez | cb28fd9 | 2022-01-28 12:41:52 +0100 | [diff] [blame] | 442 | } |
avm99963 | 8981288 | 2021-02-02 20:51:25 +0100 | [diff] [blame] | 443 | } |
| 444 | |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 445 | var mutationObserver = new MutationObserver(mutationCallback); |
avm99963 | e4cac40 | 2020-12-03 16:10:58 +0100 | [diff] [blame] | 446 | mutationObserver.observe(document.body, observerOptions); |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 447 | } else { |
| 448 | // We are in TW |
avm99963 | a2945b6 | 2020-11-27 00:32:02 +0100 | [diff] [blame] | 449 | authuser = (new URL(location.href)).searchParams.get('authuser') || '0'; |
| 450 | |
Adrià Vilanova Martínez | c8f6630 | 2023-07-06 21:38:50 +0200 | [diff] [blame] | 451 | let foundProfileLink = false; |
| 452 | for (const linkType of TW_PROFILE_LINK_TYPES) { |
| 453 | let node = document.querySelector(linkType.nodeSelector); |
| 454 | if (node !== null) { |
| 455 | foundProfileLink = true; |
| 456 | getOptionsAndHandleIndicators(node, linkType.ui); |
| 457 | break; |
| 458 | } |
Adrià Vilanova Martínez | ad6bedf | 2022-01-28 10:41:10 +0100 | [diff] [blame] | 459 | } |
Adrià Vilanova Martínez | c8f6630 | 2023-07-06 21:38:50 +0200 | [diff] [blame] | 460 | |
| 461 | if (!foundProfileLink) |
| 462 | console.error('[opindicator] Couldn\'t find username.'); |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 463 | } |