avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 1 | var CCProfileRegex = |
| 2 | /^(?:https:\/\/support\.google\.com)?\/s\/community\/forum\/[0-9]*\/user\/(?:[0-9]+)$/; |
| 3 | var CCRegex = /^https:\/\/support\.google\.com\/s\/community/; |
| 4 | |
avm99963 | a2945b6 | 2020-11-27 00:32:02 +0100 | [diff] [blame] | 5 | const BASE_URL = 'https://support.google.com/s/community/api/'; |
| 6 | |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 7 | const OP_FIRST_POST = 0; |
| 8 | const OP_OTHER_POSTS_READ = 1; |
| 9 | const OP_OTHER_POSTS_UNREAD = 2; |
| 10 | |
| 11 | const OPClasses = { |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 12 | 0: 'first-post', |
| 13 | 1: 'other-posts-read', |
| 14 | 2: 'other-posts-unread', |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 15 | }; |
| 16 | |
| 17 | const OPi18n = { |
| 18 | 0: 'first_post', |
| 19 | 1: 'other_posts_read', |
| 20 | 2: 'other_posts_unread', |
| 21 | }; |
| 22 | |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 23 | const indicatorTypes = ['numPosts', 'indicatorDot']; |
| 24 | |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 25 | // Filter used as a workaround to speed up the ViewForum request. |
| 26 | const FILTER_ALL_LANGUAGES = |
| 27 | '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)'; |
| 28 | |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 29 | const numPostsForumArraysToSum = [3, 4]; |
| 30 | |
avm99963 | a2945b6 | 2020-11-27 00:32:02 +0100 | [diff] [blame] | 31 | var authuser = null; |
| 32 | |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 33 | function isElementInside(element, outerTag) { |
| 34 | while (element !== null && ('tagName' in element)) { |
| 35 | if (element.tagName == outerTag) return true; |
| 36 | element = element.parentNode; |
| 37 | } |
| 38 | |
| 39 | return false; |
| 40 | } |
| 41 | |
| 42 | function escapeUsername(username) { |
| 43 | var quoteRegex = /"/g; |
| 44 | var commentRegex = /<!---->/g; |
| 45 | return username.replace(quoteRegex, '\\"').replace(commentRegex, ''); |
| 46 | } |
| 47 | |
avm99963 | a2945b6 | 2020-11-27 00:32:02 +0100 | [diff] [blame] | 48 | function APIRequest(action, body) { |
| 49 | var authuserPart = |
| 50 | (authuser == '0' ? '' : '?authuser=' + encodeURIComponent(authuser)); |
| 51 | |
| 52 | return fetch(BASE_URL + action + authuserPart, { |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 53 | 'credentials': 'include', |
| 54 | 'headers': {'content-type': 'text/plain; charset=utf-8'}, |
avm99963 | a2945b6 | 2020-11-27 00:32:02 +0100 | [diff] [blame] | 55 | 'body': JSON.stringify(body), |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 56 | 'method': 'POST', |
| 57 | 'mode': 'cors', |
| 58 | }) |
| 59 | .then(res => res.json()); |
| 60 | } |
| 61 | |
avm99963 | a2945b6 | 2020-11-27 00:32:02 +0100 | [diff] [blame] | 62 | function getPosts(query, forumId) { |
| 63 | return APIRequest('ViewForum', { |
| 64 | '1': forumId, |
| 65 | '2': { |
| 66 | '1': { |
| 67 | '2': 5, |
| 68 | }, |
| 69 | '2': { |
| 70 | '1': 1, |
| 71 | '2': true, |
| 72 | }, |
| 73 | '12': query, |
| 74 | }, |
| 75 | }); |
| 76 | } |
| 77 | |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 78 | function getProfile(userId, forumId) { |
avm99963 | a2945b6 | 2020-11-27 00:32:02 +0100 | [diff] [blame] | 79 | return APIRequest('ViewUser', { |
| 80 | '1': userId, |
| 81 | '2': 0, |
| 82 | '3': forumId, |
avm99963 | 3a412b8 | 2020-12-01 23:22:31 +0100 | [diff] [blame] | 83 | '4': { |
| 84 | '20': true, |
| 85 | }, |
avm99963 | a2945b6 | 2020-11-27 00:32:02 +0100 | [diff] [blame] | 86 | }); |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 87 | } |
| 88 | |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 89 | // Source: |
| 90 | // 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] | 91 | var contentScriptRequest = (function() { |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 92 | var requestId = 0; |
avm99963 | 3e23888 | 2020-12-07 18:38:54 +0100 | [diff] [blame] | 93 | var prefix = 'TWPT-profileindicator'; |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 94 | |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 95 | function sendRequest(data) { |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 96 | var id = requestId++; |
| 97 | |
| 98 | return new Promise(function(resolve, reject) { |
| 99 | var listener = function(evt) { |
avm99963 | 3e23888 | 2020-12-07 18:38:54 +0100 | [diff] [blame] | 100 | if (evt.source === window && evt.data && evt.data.prefix === prefix && |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 101 | evt.data.requestId == id) { |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 102 | // Deregister self |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 103 | window.removeEventListener('message', listener); |
| 104 | resolve(evt.data.data); |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 105 | } |
| 106 | }; |
| 107 | |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 108 | window.addEventListener('message', listener); |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 109 | |
avm99963 | 3e23888 | 2020-12-07 18:38:54 +0100 | [diff] [blame] | 110 | var payload = {data, id, prefix}; |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 111 | |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 112 | window.dispatchEvent( |
| 113 | new CustomEvent('TWPT_sendRequest', {detail: payload})); |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 114 | }); |
| 115 | } |
| 116 | |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 117 | return {sendRequest: sendRequest}; |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 118 | })(); |
| 119 | |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 120 | // Create profile indicator dot with a loading state, or return the numPosts |
| 121 | // badge if it is already created. |
| 122 | function createIndicatorDot(sourceNode, searchURL, options) { |
| 123 | if (options.numPosts) return document.querySelector('.num-posts-indicator'); |
avm99963 | a1b23b6 | 2020-09-01 14:32:34 +0200 | [diff] [blame] | 124 | var dotContainer = document.createElement('div'); |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 125 | dotContainer.classList.add('profile-indicator', 'profile-indicator--loading'); |
| 126 | contentScriptRequest |
| 127 | .sendRequest({ |
| 128 | 'action': 'geti18nMessage', |
| 129 | 'msg': 'inject_profileindicator_loading' |
| 130 | }) |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 131 | .then(string => dotContainer.setAttribute('title', string)); |
| 132 | |
| 133 | var dotLink = document.createElement('a'); |
| 134 | dotLink.href = searchURL; |
| 135 | dotLink.innerText = '●'; |
| 136 | |
| 137 | dotContainer.appendChild(dotLink); |
| 138 | sourceNode.parentNode.appendChild(dotContainer); |
| 139 | |
| 140 | return dotContainer; |
| 141 | } |
| 142 | |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 143 | // Create badge indicating the number of posts with a loading state |
| 144 | function createNumPostsBadge(sourceNode, searchURL) { |
| 145 | var link = document.createElement('a'); |
| 146 | link.href = searchURL; |
| 147 | |
| 148 | var numPostsContainer = document.createElement('div'); |
| 149 | numPostsContainer.classList.add( |
| 150 | 'num-posts-indicator', 'num-posts-indicator--loading'); |
| 151 | contentScriptRequest |
| 152 | .sendRequest({ |
| 153 | 'action': 'geti18nMessage', |
| 154 | 'msg': 'inject_profileindicator_loading' |
| 155 | }) |
| 156 | .then(string => numPostsContainer.setAttribute('title', string)); |
| 157 | |
| 158 | var numPostsSpan = document.createElement('span'); |
| 159 | numPostsSpan.classList.add('num-posts-indicator--num'); |
| 160 | |
| 161 | numPostsContainer.appendChild(numPostsSpan); |
| 162 | link.appendChild(numPostsContainer); |
| 163 | sourceNode.parentNode.appendChild(link); |
| 164 | return numPostsContainer; |
| 165 | } |
| 166 | |
avm99963 | 13658b9 | 2020-12-02 00:02:53 +0100 | [diff] [blame] | 167 | // Set the badge text |
| 168 | function setNumPostsBadge(badge, text) { |
| 169 | badge.classList.remove('num-posts-indicator--loading'); |
| 170 | badge.querySelector('span').classList.remove( |
| 171 | 'num-posts-indicator--num--loading'); |
| 172 | badge.querySelector('span').textContent = text; |
| 173 | } |
| 174 | |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 175 | // Get options and then handle all the indicators |
| 176 | function getOptionsAndHandleIndicators(sourceNode, isCC) { |
| 177 | contentScriptRequest.sendRequest({'action': 'getProfileIndicatorOptions'}) |
| 178 | .then(options => handleIndicators(sourceNode, isCC, options)); |
| 179 | } |
| 180 | |
| 181 | // Handle the profile indicator dot |
| 182 | function handleIndicators(sourceNode, isCC, options) { |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 183 | var escapedUsername = escapeUsername( |
avm99963 | 9f586f4 | 2021-02-05 12:37:51 +0100 | [diff] [blame] | 184 | (isCC ? sourceNode.querySelector('.name-text').innerHTML : |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 185 | sourceNode.querySelector('span').innerHTML)); |
| 186 | |
| 187 | if (isCC) { |
| 188 | var threadLink = document.location.href; |
| 189 | } else { |
| 190 | var CCLink = document.getElementById('onebar-community-console'); |
| 191 | if (CCLink === null) { |
| 192 | console.error( |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 193 | '[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] | 194 | return; |
| 195 | } |
| 196 | var threadLink = CCLink.href; |
| 197 | } |
| 198 | |
| 199 | var forumUrlSplit = threadLink.split('/forum/'); |
| 200 | if (forumUrlSplit.length < 2) { |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 201 | console.error('[opindicator] Can\'t get forum id.'); |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 202 | return; |
| 203 | } |
| 204 | |
| 205 | var forumId = forumUrlSplit[1].split('/')[0]; |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 206 | |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 207 | var query = '(replier:"' + escapedUsername + '" | creator:"' + |
| 208 | escapedUsername + '") ' + FILTER_ALL_LANGUAGES; |
| 209 | var encodedQuery = |
| 210 | encodeURIComponent(query + (isCC ? ' forum:' + forumId : '')); |
avm99963 | a2945b6 | 2020-11-27 00:32:02 +0100 | [diff] [blame] | 211 | var authuserPart = |
| 212 | (authuser == '0' ? |
| 213 | '' : |
| 214 | (isCC ? '?' : '&') + 'authuser=' + encodeURIComponent(authuser)); |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 215 | var searchURL = |
| 216 | (isCC ? 'https://support.google.com/s/community/search/' + |
avm99963 | a2945b6 | 2020-11-27 00:32:02 +0100 | [diff] [blame] | 217 | encodeURIComponent('query=' + encodedQuery) + authuserPart : |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 218 | document.location.pathname.split('/thread')[0] + |
avm99963 | a2945b6 | 2020-11-27 00:32:02 +0100 | [diff] [blame] | 219 | '/threads?thread_filter=' + encodedQuery + authuserPart); |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 220 | |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 221 | if (options.numPosts) { |
| 222 | var profileURL = new URL(sourceNode.href); |
| 223 | var userId = |
| 224 | profileURL.pathname.split(isCC ? 'user/' : 'profile/')[1].split('/')[0]; |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 225 | |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 226 | var numPostsContainer = createNumPostsBadge(sourceNode, searchURL); |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 227 | |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 228 | getProfile(userId, forumId) |
| 229 | .then(res => { |
| 230 | if (!('1' in res) || !('2' in res[1])) { |
| 231 | throw new Error('Unexpected profile response.'); |
| 232 | return; |
| 233 | } |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 234 | |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 235 | contentScriptRequest.sendRequest({'action': 'getNumPostMonths'}) |
| 236 | .then(months => { |
| 237 | if (!options.indicatorDot) |
| 238 | contentScriptRequest |
| 239 | .sendRequest({ |
| 240 | 'action': 'geti18nMessage', |
| 241 | 'msg': 'inject_profileindicatoralt_numposts', |
| 242 | 'placeholders': [months] |
| 243 | }) |
| 244 | .then( |
| 245 | string => |
| 246 | numPostsContainer.setAttribute('title', string)); |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 247 | |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 248 | var numPosts = 0; |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 249 | |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 250 | for (const index of numPostsForumArraysToSum) { |
| 251 | if (!(index in res[1][2])) { |
| 252 | throw new Error('Unexpected profile response.'); |
| 253 | return; |
| 254 | } |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 255 | |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 256 | var i = 0; |
| 257 | for (const month of res[1][2][index].reverse()) { |
| 258 | if (i == months) break; |
| 259 | numPosts += month[3] || 0; |
| 260 | ++i; |
| 261 | } |
| 262 | } |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 263 | |
avm99963 | 13658b9 | 2020-12-02 00:02:53 +0100 | [diff] [blame] | 264 | setNumPostsBadge(numPostsContainer, numPosts); |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 265 | }) |
| 266 | .catch( |
| 267 | err => console.error('[opindicator] Unexpected error.', err)); |
| 268 | }) |
avm99963 | 13658b9 | 2020-12-02 00:02:53 +0100 | [diff] [blame] | 269 | .catch(err => { |
| 270 | console.error( |
| 271 | '[opindicator] Unexpected error. Couldn\'t load profile.', err); |
| 272 | setNumPostsBadge(numPostsContainer, '?'); |
| 273 | }); |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | if (options.indicatorDot) { |
| 277 | var dotContainer = createIndicatorDot(sourceNode, searchURL, options); |
| 278 | |
| 279 | // Query threads in order to see what state the indicator should be in |
| 280 | getPosts(query, forumId) |
| 281 | .then(res => { |
avm99963 | 9f586f4 | 2021-02-05 12:37:51 +0100 | [diff] [blame] | 282 | // Throw an error when the replies array is not present in the reply. |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 283 | if (!('1' in res) || !('2' in res['1'])) { |
avm99963 | 9f586f4 | 2021-02-05 12:37:51 +0100 | [diff] [blame] | 284 | // Throw a different error when the numThreads field exists and is |
| 285 | // equal to 0. This reply can be received, but is enexpected, |
| 286 | // because we know that the user has replied in at least 1 thread |
| 287 | // (the current one). |
| 288 | if (('1' in res) && ('4' in res['1']) && res['1']['4'] == 0) |
| 289 | throw new Error( |
| 290 | 'Thread list is empty ' + |
| 291 | '(but the OP has participated in this thread, ' + |
| 292 | 'so it shouldn\'t be empty).'); |
| 293 | |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 294 | throw new Error('Unexpected thread list response.'); |
| 295 | return; |
| 296 | } |
| 297 | |
| 298 | // Current thread ID |
| 299 | var threadUrlSplit = threadLink.split('/thread/'); |
| 300 | if (threadUrlSplit.length < 2) |
| 301 | throw new Error('Can\'t get thread id.'); |
| 302 | |
| 303 | var currId = threadUrlSplit[1].split('/')[0]; |
| 304 | |
| 305 | var OPStatus = OP_FIRST_POST; |
| 306 | |
| 307 | for (const thread of res['1']['2']) { |
| 308 | var id = thread['2']['1']['1'] || undefined; |
| 309 | if (id === undefined || id == currId) continue; |
| 310 | |
| 311 | var isRead = thread['6'] || false; |
| 312 | if (isRead) |
| 313 | OPStatus = Math.max(OP_OTHER_POSTS_READ, OPStatus); |
| 314 | else |
| 315 | OPStatus = Math.max(OP_OTHER_POSTS_UNREAD, OPStatus); |
| 316 | } |
| 317 | |
| 318 | var dotContainerPrefix = |
| 319 | (options.numPosts ? 'num-posts-indicator' : 'profile-indicator'); |
| 320 | |
| 321 | if (!options.numPosts) |
| 322 | dotContainer.classList.remove(dotContainerPrefix + '--loading'); |
| 323 | dotContainer.classList.add( |
| 324 | dotContainerPrefix + '--' + OPClasses[OPStatus]); |
| 325 | contentScriptRequest |
| 326 | .sendRequest({ |
| 327 | 'action': 'geti18nMessage', |
| 328 | 'msg': 'inject_profileindicator_' + OPi18n[OPStatus] |
| 329 | }) |
| 330 | .then(string => dotContainer.setAttribute('title', string)); |
| 331 | }) |
| 332 | .catch( |
| 333 | err => console.error( |
| 334 | '[opindicator] Unexpected error. Couldn\'t load recent posts.', |
| 335 | err)); |
| 336 | } |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | if (CCRegex.test(location.href)) { |
| 340 | // We are in the Community Console |
avm99963 | a2945b6 | 2020-11-27 00:32:02 +0100 | [diff] [blame] | 341 | var startup = |
| 342 | JSON.parse(document.querySelector('html').getAttribute('data-startup')); |
| 343 | |
| 344 | authuser = startup[2][1] || '0'; |
| 345 | |
avm99963 | 8981288 | 2021-02-02 20:51:25 +0100 | [diff] [blame] | 346 | // When the OP's username is found, call getOptionsAndHandleIndicators |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 347 | function mutationCallback(mutationList, observer) { |
| 348 | mutationList.forEach((mutation) => { |
| 349 | if (mutation.type == 'childList') { |
| 350 | mutation.addedNodes.forEach(function(node) { |
| 351 | if (node.tagName == 'A' && ('href' in node) && |
| 352 | CCProfileRegex.test(node.href) && |
avm99963 | 9f586f4 | 2021-02-05 12:37:51 +0100 | [diff] [blame] | 353 | node.matches( |
| 354 | 'ec-question ec-message-header .name-section ec-user-link a')) { |
avm99963 | 8981288 | 2021-02-02 20:51:25 +0100 | [diff] [blame] | 355 | console.info('Handling profile indicator via mutation callback.'); |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 356 | getOptionsAndHandleIndicators(node, true); |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 357 | } |
| 358 | }); |
| 359 | } |
| 360 | }); |
| 361 | }; |
| 362 | |
| 363 | var observerOptions = { |
| 364 | childList: true, |
| 365 | subtree: true, |
| 366 | } |
| 367 | |
avm99963 | 8981288 | 2021-02-02 20:51:25 +0100 | [diff] [blame] | 368 | // Before starting the mutation Observer, check if the OP's username link is |
| 369 | // already part of the page |
| 370 | var node = document.querySelector( |
| 371 | 'ec-question ec-message-header .name-section ec-user-link a'); |
| 372 | if (node !== null) { |
| 373 | console.info('Handling profile indicator via first check.'); |
| 374 | getOptionsAndHandleIndicators(node, true); |
| 375 | } |
| 376 | |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 377 | mutationObserver = new MutationObserver(mutationCallback); |
avm99963 | e4cac40 | 2020-12-03 16:10:58 +0100 | [diff] [blame] | 378 | mutationObserver.observe(document.body, observerOptions); |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 379 | } else { |
| 380 | // We are in TW |
avm99963 | a2945b6 | 2020-11-27 00:32:02 +0100 | [diff] [blame] | 381 | authuser = (new URL(location.href)).searchParams.get('authuser') || '0'; |
| 382 | |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 383 | var node = |
| 384 | document.querySelector('.thread-question a.user-info-display-name'); |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 385 | if (node !== null) |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 386 | getOptionsAndHandleIndicators(node, false); |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 387 | else |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 388 | console.error('[opindicator] Couldn\'t find username.'); |
avm99963 | e51444e | 2020-08-31 14:50:06 +0200 | [diff] [blame] | 389 | } |