blob: 8e6344cdfb370a817cd203576380615a5e7f4ed4 [file] [log] [blame]
avm99963e6166ed2021-08-26 10:45:17 +02001import {CCApi} from '../common/api.js';
Adrià Vilanova Martíneze7770472021-10-17 00:02:37 +02002import {createImmuneLink} from '../common/commonUtils.js';
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +02003import {escapeUsername} from '../common/communityConsoleUtils.js';
avm999632485a3e2021-09-08 22:18:38 +02004import {createPlainTooltip} from '../common/tooltip.js';
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +02005
avm99963e51444e2020-08-31 14:50:06 +02006var CCProfileRegex =
Adrià Vilanova Martínezcb28fd92022-01-28 12:41:52 +01007 /^(?:https:\/\/support\.google\.com)?\/s\/community(?:\/forum\/[0-9]*)?\/user\/(?:[0-9]+)(?:\?.*)?$/;
avm99963e51444e2020-08-31 14:50:06 +02008var CCRegex = /^https:\/\/support\.google\.com\/s\/community/;
9
10const OP_FIRST_POST = 0;
11const OP_OTHER_POSTS_READ = 1;
12const OP_OTHER_POSTS_UNREAD = 2;
13
14const OPClasses = {
avm99963ad65e752020-09-01 00:13:59 +020015 0: 'first-post',
16 1: 'other-posts-read',
17 2: 'other-posts-unread',
avm99963e51444e2020-08-31 14:50:06 +020018};
19
20const OPi18n = {
21 0: 'first_post',
22 1: 'other_posts_read',
23 2: 'other_posts_unread',
24};
25
Adrià Vilanova Martínezad6bedf2022-01-28 10:41:10 +010026const UI_COMMUNITY_CONSOLE = 0;
27const UI_TW_LEGACY = 1;
28const UI_TW_INTEROP = 2;
Adrià Vilanova Martínezcb28fd92022-01-28 12:41:52 +010029const UI_COMMUNITY_CONSOLE_INTEROP = 3;
Adrià Vilanova Martínezc8f66302023-07-06 21:38:50 +020030const UI_TW_INTEROP_V2 = 4;
31const UI_COMMUNITY_CONSOLE_INTEROP_V2 = 5;
Adrià Vilanova Martínezad6bedf2022-01-28 10:41:10 +010032
avm99963e51444e2020-08-31 14:50:06 +020033// Filter used as a workaround to speed up the ViewForum request.
34const 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
avm99963ad65e752020-09-01 00:13:59 +020037const numPostsForumArraysToSum = [3, 4];
38
Adrià Vilanova Martínezc8f66302023-07-06 21:38:50 +020039const 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
61const 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
avm99963a2945b62020-11-27 00:32:02 +010083var authuser = null;
84
Adrià Vilanova Martínezcb28fd92022-01-28 12:41:52 +010085function isCommunityConsole(ui) {
Adrià Vilanova Martínezc8f66302023-07-06 21:38:50 +020086 return ui === UI_COMMUNITY_CONSOLE || ui === UI_COMMUNITY_CONSOLE_INTEROP ||
87 ui === UI_COMMUNITY_CONSOLE_INTEROP_V2;
Adrià Vilanova Martínezcb28fd92022-01-28 12:41:52 +010088}
89
Adrià Vilanova Martínezc8f66302023-07-06 21:38:50 +020090function isInteropV1(ui) {
Adrià Vilanova Martínezcb28fd92022-01-28 12:41:52 +010091 return ui === UI_TW_INTEROP || ui === UI_COMMUNITY_CONSOLE_INTEROP;
92}
93
Adrià Vilanova Martínezc8f66302023-07-06 21:38:50 +020094function isInteropV2(ui) {
95 return ui === UI_TW_INTEROP_V2 || ui === UI_COMMUNITY_CONSOLE_INTEROP_V2;
96}
avm99963e51444e2020-08-31 14:50:06 +020097
avm99963a2945b62020-11-27 00:32:02 +010098function getPosts(query, forumId) {
avm99963e6166ed2021-08-26 10:45:17 +020099 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 },
avm99963a2945b62020-11-27 00:32:02 +0100112 },
avm99963e6166ed2021-08-26 10:45:17 +0200113 /* authenticated = */ true, authuser);
avm99963a2945b62020-11-27 00:32:02 +0100114}
115
avm99963ad65e752020-09-01 00:13:59 +0200116function getProfile(userId, forumId) {
avm99963e6166ed2021-08-26 10:45:17 +0200117 return CCApi(
118 'ViewUser', {
119 '1': userId,
120 '2': 0,
121 '3': forumId,
122 '4': {
123 '20': true,
124 },
125 },
126 /* authenticated = */ true, authuser);
avm99963ad65e752020-09-01 00:13:59 +0200127}
128
avm99963e51444e2020-08-31 14:50:06 +0200129// Source:
130// https://stackoverflow.com/questions/33063774/communication-from-an-injected-script-to-the-content-script-with-a-response
avm99963ad65e752020-09-01 00:13:59 +0200131var contentScriptRequest = (function() {
avm99963e51444e2020-08-31 14:50:06 +0200132 var requestId = 0;
avm999633e238882020-12-07 18:38:54 +0100133 var prefix = 'TWPT-profileindicator';
avm99963e51444e2020-08-31 14:50:06 +0200134
avm99963ad65e752020-09-01 00:13:59 +0200135 function sendRequest(data) {
avm99963e51444e2020-08-31 14:50:06 +0200136 var id = requestId++;
137
Adrià Vilanova Martínez78ff4062023-07-06 20:03:29 +0200138 return new Promise(function(resolve) {
avm99963e51444e2020-08-31 14:50:06 +0200139 var listener = function(evt) {
avm999633e238882020-12-07 18:38:54 +0100140 if (evt.source === window && evt.data && evt.data.prefix === prefix &&
avm99963ad65e752020-09-01 00:13:59 +0200141 evt.data.requestId == id) {
avm99963e51444e2020-08-31 14:50:06 +0200142 // Deregister self
avm99963ad65e752020-09-01 00:13:59 +0200143 window.removeEventListener('message', listener);
144 resolve(evt.data.data);
avm99963e51444e2020-08-31 14:50:06 +0200145 }
146 };
147
avm99963ad65e752020-09-01 00:13:59 +0200148 window.addEventListener('message', listener);
avm99963e51444e2020-08-31 14:50:06 +0200149
avm999633e238882020-12-07 18:38:54 +0100150 var payload = {data, id, prefix};
avm99963e51444e2020-08-31 14:50:06 +0200151
avm99963ad65e752020-09-01 00:13:59 +0200152 window.dispatchEvent(
153 new CustomEvent('TWPT_sendRequest', {detail: payload}));
avm99963e51444e2020-08-31 14:50:06 +0200154 });
155 }
156
avm99963ad65e752020-09-01 00:13:59 +0200157 return {sendRequest: sendRequest};
avm99963e51444e2020-08-31 14:50:06 +0200158})();
159
Adrià Vilanova Martínezc8f66302023-07-06 21:38:50 +0200160// Inject the indicator dot/badge to the appropriate position.
161function 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
avm99963ad65e752020-09-01 00:13:59 +0200172// Create profile indicator dot with a loading state, or return the numPosts
173// badge if it is already created.
Adrià Vilanova Martínezad6bedf2022-01-28 10:41:10 +0100174function createIndicatorDot(sourceNode, searchURL, options, ui) {
avm99963ad65e752020-09-01 00:13:59 +0200175 if (options.numPosts) return document.querySelector('.num-posts-indicator');
avm99963a1b23b62020-09-01 14:32:34 +0200176 var dotContainer = document.createElement('div');
avm99963ad65e752020-09-01 00:13:59 +0200177 dotContainer.classList.add('profile-indicator', 'profile-indicator--loading');
avm99963e51444e2020-08-31 14:50:06 +0200178
Adrià Vilanova Martínezcb28fd92022-01-28 12:41:52 +0100179 var dotLink = (isCommunityConsole(ui)) ? createImmuneLink() :
180 document.createElement('a');
Adrià Vilanova Martínezc8f66302023-07-06 21:38:50 +0200181 dotLink.classList.add(
182 'profile-indicator-link', 'profile-indicator-link--dot');
avm99963e51444e2020-08-31 14:50:06 +0200183 dotLink.href = searchURL;
184 dotLink.innerText = '●';
185
186 dotContainer.appendChild(dotLink);
Adrià Vilanova Martínezc8f66302023-07-06 21:38:50 +0200187 injectIndicator(sourceNode, dotContainer, ui);
avm99963e51444e2020-08-31 14:50:06 +0200188
avm999632485a3e2021-09-08 22:18:38 +0200189 contentScriptRequest
190 .sendRequest({
191 'action': 'geti18nMessage',
192 'msg': 'inject_profileindicator_loading'
193 })
194 .then(string => createPlainTooltip(dotContainer, string));
195
avm99963e51444e2020-08-31 14:50:06 +0200196 return dotContainer;
197}
198
avm99963ad65e752020-09-01 00:13:59 +0200199// Create badge indicating the number of posts with a loading state
Adrià Vilanova Martínezad6bedf2022-01-28 10:41:10 +0100200function createNumPostsBadge(sourceNode, searchURL, ui) {
Adrià Vilanova Martínezcb28fd92022-01-28 12:41:52 +0100201 var link = (isCommunityConsole(ui)) ? createImmuneLink() :
202 document.createElement('a');
Adrià Vilanova Martínezc8f66302023-07-06 21:38:50 +0200203 link.classList.add(
204 'profile-indicator-link', 'profile-indicator-link--num-posts');
avm99963ad65e752020-09-01 00:13:59 +0200205 link.href = searchURL;
206
207 var numPostsContainer = document.createElement('div');
208 numPostsContainer.classList.add(
209 'num-posts-indicator', 'num-posts-indicator--loading');
avm99963ad65e752020-09-01 00:13:59 +0200210
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ínezc8f66302023-07-06 21:38:50 +0200216 injectIndicator(sourceNode, link, ui);
avm999632485a3e2021-09-08 22:18:38 +0200217
218 contentScriptRequest
219 .sendRequest({
220 'action': 'geti18nMessage',
221 'msg': 'inject_profileindicator_loading'
222 })
223 .then(string => createPlainTooltip(numPostsContainer, string));
224
avm99963ad65e752020-09-01 00:13:59 +0200225 return numPostsContainer;
226}
227
avm9996313658b92020-12-02 00:02:53 +0100228// Set the badge text
229function 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
avm99963ad65e752020-09-01 00:13:59 +0200236// Get options and then handle all the indicators
Adrià Vilanova Martínezad6bedf2022-01-28 10:41:10 +0100237function getOptionsAndHandleIndicators(sourceNode, ui) {
avm99963ad65e752020-09-01 00:13:59 +0200238 contentScriptRequest.sendRequest({'action': 'getProfileIndicatorOptions'})
Adrià Vilanova Martínezad6bedf2022-01-28 10:41:10 +0100239 .then(options => handleIndicators(sourceNode, ui, options));
avm99963ad65e752020-09-01 00:13:59 +0200240}
241
242// Handle the profile indicator dot
Adrià Vilanova Martínezad6bedf2022-01-28 10:41:10 +0100243function 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ínezc8f66302023-07-06 21:38:50 +0200248 if (isInteropV1(ui)) nameEl = sourceNode;
249 if (isInteropV2(ui))
250 nameEl =
251 sourceNode.querySelector('.scTailwindThreadPost_headerUserinfoname');
Adrià Vilanova Martínezad6bedf2022-01-28 10:41:10 +0100252 var escapedUsername = escapeUsername(nameEl.textContent);
avm99963e51444e2020-08-31 14:50:06 +0200253
Adrià Vilanova Martínez78ff4062023-07-06 20:03:29 +0200254 var threadLink;
Adrià Vilanova Martínezcb28fd92022-01-28 12:41:52 +0100255 if (isCommunityConsole(ui)) {
Adrià Vilanova Martínez78ff4062023-07-06 20:03:29 +0200256 threadLink = document.location.href;
avm99963e51444e2020-08-31 14:50:06 +0200257 } else {
258 var CCLink = document.getElementById('onebar-community-console');
259 if (CCLink === null) {
260 console.error(
avm99963ad65e752020-09-01 00:13:59 +0200261 '[opindicator] The user is not a PE so the dot indicator cannot be shown in TW.');
avm99963e51444e2020-08-31 14:50:06 +0200262 return;
263 }
Adrià Vilanova Martínez78ff4062023-07-06 20:03:29 +0200264 threadLink = CCLink.href;
avm99963e51444e2020-08-31 14:50:06 +0200265 }
266
267 var forumUrlSplit = threadLink.split('/forum/');
268 if (forumUrlSplit.length < 2) {
avm99963ad65e752020-09-01 00:13:59 +0200269 console.error('[opindicator] Can\'t get forum id.');
avm99963e51444e2020-08-31 14:50:06 +0200270 return;
271 }
272
273 var forumId = forumUrlSplit[1].split('/')[0];
avm99963ad65e752020-09-01 00:13:59 +0200274
avm99963e51444e2020-08-31 14:50:06 +0200275 var query = '(replier:"' + escapedUsername + '" | creator:"' +
Adrià Vilanova Martínez2b2d69c2022-01-28 15:03:03 +0100276 escapedUsername + '") ' + FILTER_ALL_LANGUAGES + ' forum:' + forumId;
277 var encodedQuery = encodeURIComponent(query);
avm99963a2945b62020-11-27 00:32:02 +0100278 var authuserPart =
Adrià Vilanova Martínezad6bedf2022-01-28 10:41:10 +0100279 (authuser == '0' ? '' : '?authuser=' + encodeURIComponent(authuser));
280 var searchURL = 'https://support.google.com/s/community/search/' +
281 encodeURIComponent('query=' + encodedQuery) + authuserPart;
avm99963e51444e2020-08-31 14:50:06 +0200282
avm99963ad65e752020-09-01 00:13:59 +0200283 if (options.numPosts) {
284 var profileURL = new URL(sourceNode.href);
Adrià Vilanova Martínezcb28fd92022-01-28 12:41:52 +0100285 var userId = profileURL.pathname
286 .split(isCommunityConsole(ui) ? 'user/' : 'profile/')[1]
287 .split('?')[0]
288 .split('/')[0];
avm99963e51444e2020-08-31 14:50:06 +0200289
Adrià Vilanova Martínezad6bedf2022-01-28 10:41:10 +0100290 var numPostsContainer = createNumPostsBadge(sourceNode, searchURL, ui);
avm99963e51444e2020-08-31 14:50:06 +0200291
avm99963ad65e752020-09-01 00:13:59 +0200292 getProfile(userId, forumId)
293 .then(res => {
294 if (!('1' in res) || !('2' in res[1])) {
295 throw new Error('Unexpected profile response.');
avm99963ad65e752020-09-01 00:13:59 +0200296 }
avm99963e51444e2020-08-31 14:50:06 +0200297
avm99963ad65e752020-09-01 00:13:59 +0200298 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 =>
avm999632485a3e2021-09-08 22:18:38 +0200309 createPlainTooltip(numPostsContainer, string));
avm99963e51444e2020-08-31 14:50:06 +0200310
avm99963ad65e752020-09-01 00:13:59 +0200311 var numPosts = 0;
avm99963e51444e2020-08-31 14:50:06 +0200312
avm99963ad65e752020-09-01 00:13:59 +0200313 for (const index of numPostsForumArraysToSum) {
314 if (!(index in res[1][2])) {
315 throw new Error('Unexpected profile response.');
avm99963ad65e752020-09-01 00:13:59 +0200316 }
avm99963e51444e2020-08-31 14:50:06 +0200317
avm99963ad65e752020-09-01 00:13:59 +0200318 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 }
avm99963e51444e2020-08-31 14:50:06 +0200325
avm9996313658b92020-12-02 00:02:53 +0100326 setNumPostsBadge(numPostsContainer, numPosts);
avm99963ad65e752020-09-01 00:13:59 +0200327 })
328 .catch(
329 err => console.error('[opindicator] Unexpected error.', err));
330 })
avm9996313658b92020-12-02 00:02:53 +0100331 .catch(err => {
332 console.error(
333 '[opindicator] Unexpected error. Couldn\'t load profile.', err);
334 setNumPostsBadge(numPostsContainer, '?');
335 });
avm99963ad65e752020-09-01 00:13:59 +0200336 }
337
338 if (options.indicatorDot) {
Adrià Vilanova Martínezad6bedf2022-01-28 10:41:10 +0100339 var dotContainer = createIndicatorDot(sourceNode, searchURL, options, ui);
avm99963ad65e752020-09-01 00:13:59 +0200340
341 // Query threads in order to see what state the indicator should be in
342 getPosts(query, forumId)
343 .then(res => {
avm999639f586f42021-02-05 12:37:51 +0100344 // Throw an error when the replies array is not present in the reply.
avm99963ad65e752020-09-01 00:13:59 +0200345 if (!('1' in res) || !('2' in res['1'])) {
avm999639f586f42021-02-05 12:37:51 +0100346 // 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
avm99963ad65e752020-09-01 00:13:59 +0200356 throw new Error('Unexpected thread list response.');
avm99963ad65e752020-09-01 00:13:59 +0200357 }
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ínez2b2d69c2022-01-28 15:03:03 +0100364 var currId = threadUrlSplit[1].split('?')[0].split('/')[0];
avm99963ad65e752020-09-01 00:13:59 +0200365
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 })
avm999632485a3e2021-09-08 22:18:38 +0200391 .then(string => createPlainTooltip(dotContainer, string));
avm99963ad65e752020-09-01 00:13:59 +0200392 })
393 .catch(
394 err => console.error(
395 '[opindicator] Unexpected error. Couldn\'t load recent posts.',
396 err));
397 }
avm99963e51444e2020-08-31 14:50:06 +0200398}
399
400if (CCRegex.test(location.href)) {
401 // We are in the Community Console
avm99963a2945b62020-11-27 00:32:02 +0100402 var startup =
403 JSON.parse(document.querySelector('html').getAttribute('data-startup'));
404
405 authuser = startup[2][1] || '0';
406
avm9996389812882021-02-02 20:51:25 +0100407 // When the OP's username is found, call getOptionsAndHandleIndicators
Adrià Vilanova Martínezc8f66302023-07-06 21:38:50 +0200408 var mutationCallback =
409 function(mutationList) {
avm99963e51444e2020-08-31 14:50:06 +0200410 mutationList.forEach((mutation) => {
Adrià Vilanova Martínezc8f66302023-07-06 21:38:50 +0200411 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ínez5b987932023-11-16 02:18:44 +0100420 console.debug('Handling profile indicator via mutation callback.');
Adrià Vilanova Martínezc8f66302023-07-06 21:38:50 +0200421 getOptionsAndHandleIndicators(node, linkType.ui);
422 break;
avm99963e51444e2020-08-31 14:50:06 +0200423 }
Adrià Vilanova Martínezc8f66302023-07-06 21:38:50 +0200424 }
425 });
avm99963e51444e2020-08-31 14:50:06 +0200426 });
Adrià Vilanova Martínez78ff4062023-07-06 20:03:29 +0200427 }
avm99963e51444e2020-08-31 14:50:06 +0200428
429 var observerOptions = {
430 childList: true,
431 subtree: true,
432 }
433
avm9996389812882021-02-02 20:51:25 +0100434 // Before starting the mutation Observer, check if the OP's username link is
435 // already part of the page
Adrià Vilanova Martínezc8f66302023-07-06 21:38:50 +0200436 for (const linkType of CC_PROFILE_LINK_TYPES) {
437 let node = document.querySelector(linkType.nodeSelector);
Adrià Vilanova Martínezcb28fd92022-01-28 12:41:52 +0100438 if (node !== null) {
Adrià Vilanova Martínez5b987932023-11-16 02:18:44 +0100439 console.debug('Handling profile indicator via first check.');
Adrià Vilanova Martínezc8f66302023-07-06 21:38:50 +0200440 getOptionsAndHandleIndicators(node, linkType.ui);
441 break;
Adrià Vilanova Martínezcb28fd92022-01-28 12:41:52 +0100442 }
avm9996389812882021-02-02 20:51:25 +0100443 }
444
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200445 var mutationObserver = new MutationObserver(mutationCallback);
avm99963e4cac402020-12-03 16:10:58 +0100446 mutationObserver.observe(document.body, observerOptions);
avm99963e51444e2020-08-31 14:50:06 +0200447} else {
448 // We are in TW
avm99963a2945b62020-11-27 00:32:02 +0100449 authuser = (new URL(location.href)).searchParams.get('authuser') || '0';
450
Adrià Vilanova Martínezc8f66302023-07-06 21:38:50 +0200451 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ínezad6bedf2022-01-28 10:41:10 +0100459 }
Adrià Vilanova Martínezc8f66302023-07-06 21:38:50 +0200460
461 if (!foundProfileLink)
462 console.error('[opindicator] Couldn\'t find username.');
avm99963e51444e2020-08-31 14:50:06 +0200463}