blob: 6b5c7f7391a3d14070d2fcf06bce0323193ed5de [file] [log] [blame]
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +02001import {injectScript, injectStyles, injectStylesheet} from '../../common/contentScriptsUtils.js';
Adrià Vilanova Martínez4d599252023-02-04 20:54:54 +01002import {getOptions} from '../../common/optionsUtils.js';
Adrià Vilanova Martínez825888a2023-07-23 01:53:48 +02003import XHRProxyKillSwitchHandler from '../../xhrInterceptor/killSwitchHandler.js';
Adrià Vilanova Martínez1f652522021-10-14 00:23:23 +02004import {injectPreviousPostsLinksUnifiedProfileIfEnabled} from '../utilsCommon/unifiedProfiles.js';
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +02005
Adrià Vilanova Martínez27c69962021-07-17 23:32:51 +02006import AvatarsHandler from './avatars.js';
Adrià Vilanova Martínez462280f2021-08-07 22:59:02 +02007import {batchLock} from './batchLock.js';
Adrià Vilanova Martínez968e93c2024-03-11 23:18:16 +01008import {injectDarkThemeButton, isDarkThemeOn} from './darkTheme/darkTheme.js';
Adrià Vilanova Martínezf472d492024-03-05 21:26:53 +01009import ReportDialogColorThemeFix from './darkTheme/reportDialog.js';
Adrià Vilanova Martínez968e93c2024-03-11 23:18:16 +010010import {unifiedProfilesFix} from './darkTheme/unifiedProfiles.js';
Adrià Vilanova Martínezeebc0ac2022-01-05 14:45:53 +010011// #!if ['chromium', 'chromium_mv3'].includes(browser_target)
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020012import {applyDragAndDropFixIfEnabled} from './dragAndDropFix.js';
Adrià Vilanova Martínezeebc0ac2022-01-05 14:45:53 +010013// #!endif
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +010014import {default as FlattenThreads, kMatchingSelectors as kFlattenThreadMatchingSelectors} from './flattenThreads/flattenThreads.js';
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010015import {kRepliesSectionSelector} from './threadToolbar/constants.js';
16import ThreadToolbar from './threadToolbar/threadToolbar.js';
Adrià Vilanova Martínez2788d122022-10-10 22:06:25 +020017import Workflows from './workflows/workflows.js';
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020018
Adrià Vilanova Martínez18d03c42024-04-21 16:43:01 +020019var mutationObserver, options, avatars, workflows,
Adrià Vilanova Martínezf472d492024-03-05 21:26:53 +010020 threadToolbar, flattenThreads, reportDialogColorThemeFix;
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020021
22const watchedNodesSelectors = [
23 // App container (used to set up the intersection observer and inject the dark
24 // mode button)
25 'ec-app',
26
Adrià Vilanova Martíneza7ae3db2022-01-28 11:57:27 +010027 // Scrollable content (used for the intersection observer)
28 '.scrollable-content',
29
30 // Load more bar and buttons
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020031 '.load-more-bar',
Adrià Vilanova Martíneza7ae3db2022-01-28 11:57:27 +010032 '.scTailwindThreadMorebuttonbutton',
Adrià Vilanova Martínez88854972022-08-28 11:57:12 +020033 '.scTailwindThreadMessagegapbutton',
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020034
Adrià Vilanova Martínez7e8796c2022-01-23 21:46:46 +010035 // User profile card inside ec-unified-user
36 'ec-unified-user .scTailwindUser_profileUsercardmain',
37
Adrià Vilanova Martínez531cd072021-12-05 20:15:43 +010038 // Username span/editor inside ec-unified-user (user profile view)
Adrià Vilanova Martínez1f652522021-10-14 00:23:23 +020039 'ec-unified-user .scTailwindUser_profileUsercarddetails',
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020040
41 // Rich text editor
42 'ec-movable-dialog',
43 'ec-rich-text-editor',
44
45 // Read/unread bulk action in the list of thread, for the batch lock feature
46 'ec-bulk-actions material-button[debugid="mark-read-button"]',
47 'ec-bulk-actions material-button[debugid="mark-unread-button"]',
48
Adrià Vilanova Martínez854cb912022-02-02 16:18:23 +010049 // Thread list items (used to inject the avatars and extra info)
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020050 'li',
51
Adrià Vilanova Martínez854cb912022-02-02 16:18:23 +010052 // Thread list item toolbelt (used for the extra info feature)
53 'ec-thread-summary .main .toolbelt',
54
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020055 // Thread list (used for the autorefresh feature)
56 'ec-thread-list',
57
Adrià Vilanova Martínezf472d492024-03-05 21:26:53 +010058 // Unified profile iframe and report dialog iframe
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020059 'iframe',
Adrià Vilanova Martínez74a25202022-01-23 23:36:58 +010060
Adrià Vilanova Martínez46ecb982024-01-30 22:02:17 +010061 // Canned response tags (for the "import CR" popup for the workflows feature)
Adrià Vilanova Martínez6e4a68d2022-01-24 21:44:32 +010062 'ec-canned-response-row .tags',
Adrià Vilanova Martínez6e4a68d2022-01-24 21:44:32 +010063
Adrià Vilanova Martínez09b3bdb2022-02-05 00:15:05 +010064 // Question state chips container (for the extra info feature)
Adrià Vilanova Martínez19f6a652023-11-15 19:38:50 +010065 'sc-tailwind-thread-question-question-card sc-tailwind-thread-question-state-chips',
Adrià Vilanova Martínez6e4a68d2022-01-24 21:44:32 +010066
67 // Replies (for the extra info feature)
Adrià Vilanova Martínez19f6a652023-11-15 19:38:50 +010068 'sc-tailwind-thread-message-message-list sc-tailwind-thread-message-message-card',
Adrià Vilanova Martínez4f56d562022-01-26 00:23:27 +010069
Adrià Vilanova Martínez80f1c732023-11-16 00:52:56 +010070 // Comments (for the extra info feature)
71 'sc-tailwind-thread-message-message-list sc-tailwind-thread-message-comment-card',
72
Adrià Vilanova Martínez4f56d562022-01-26 00:23:27 +010073 // User activity chart (for the per-forum stats feature)
74 'ec-unified-user .scTailwindUser_profileUserprofilesection ' +
75 'sc-tailwind-shared-activity-chart',
Adrià Vilanova Martínez5a8055b2022-09-29 13:05:19 +020076
77 // Thread page main content
78 'ec-thread > .page > .material-content > div[role="list"]',
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010079
80 // Thread page reply section (for the thread page toolbar)
81 kRepliesSectionSelector,
Adrià Vilanova Martínez412b7582022-12-30 01:35:30 +010082
83 // Reply payload (for the flatten threads UI)
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +010084 ...kFlattenThreadMatchingSelectors,
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020085];
86
87function handleCandidateNode(node) {
88 if (typeof node.classList !== 'undefined') {
89 if (('tagName' in node) && node.tagName == 'EC-APP') {
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020090 // Inject the dark mode button
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020091 // TODO(avm99963): make this feature dynamic.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020092 if (options.ccdarktheme && options.ccdarktheme_mode == 'switch') {
93 var rightControl = node.querySelector('header .right-control');
94 if (rightControl !== null)
Adrià Vilanova Martínez968e93c2024-03-11 23:18:16 +010095 injectDarkThemeButton(
96 rightControl, options.ccdarktheme_switch_status);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020097 }
98 }
99
Adrià Vilanova Martínez7e8796c2022-01-23 21:46:46 +0100100 // Show additional details in the profile view.
101 if (node.matches('ec-unified-user .scTailwindUser_profileUsercardmain')) {
Adrià Vilanova Martínez0d92a0c2023-11-06 01:37:20 +0100102 window.TWPTExtraInfo.injectAbuseChipsAtProfileIfEnabled(node);
Adrià Vilanova Martínez7e8796c2022-01-23 21:46:46 +0100103 }
104
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200105 // Show the "previous posts" links if the option is currently enabled.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200106 // Here we're selecting the 'ec-user > div' element (unique child)
Adrià Vilanova Martínez1f652522021-10-14 00:23:23 +0200107 if (node.matches(
108 'ec-unified-user .scTailwindUser_profileUsercarddetails')) {
109 injectPreviousPostsLinksUnifiedProfileIfEnabled(
110 /* isCommunityConsole = */ true);
111 }
112
Adrià Vilanova Martínezeebc0ac2022-01-05 14:45:53 +0100113 // #!if ['chromium', 'chromium_mv3'].includes(browser_target)
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200114 // Fix the drag&drop issue with the rich text editor if the option is
115 // currently enabled.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200116 //
117 // We target both tags because in different contexts different
118 // elements containing the text editor get added to the DOM structure.
119 // Sometimes it's a EC-MOVABLE-DIALOG which already contains the
120 // EC-RICH-TEXT-EDITOR, and sometimes it's the EC-RICH-TEXT-EDITOR
121 // directly.
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200122 if (('tagName' in node) &&
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200123 (node.tagName == 'EC-MOVABLE-DIALOG' ||
124 node.tagName == 'EC-RICH-TEXT-EDITOR')) {
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200125 applyDragAndDropFixIfEnabled(node);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200126 }
Adrià Vilanova Martínezeebc0ac2022-01-05 14:45:53 +0100127 // #!endif
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200128
Adrià Vilanova Martínez2788d122022-10-10 22:06:25 +0200129 // Inject the worflows menu in the thread list if the option is currently
130 // enabled.
131 if (workflows.shouldAddThreadListBtn(node)) {
132 workflows.addThreadListBtnIfEnabled(node);
133 }
134
Adrià Vilanova Martínezb06c13d2022-06-20 17:30:14 +0000135 // Inject the batch lock button in the thread list if the option is
136 // currently enabled.
Adrià Vilanova Martínez4107b5e2022-10-09 23:11:11 +0200137 if (batchLock.shouldAddButton(node)) {
Adrià Vilanova Martínezb06c13d2022-06-20 17:30:14 +0000138 batchLock.addButtonIfEnabled(node);
139 }
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200140
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200141 // Inject avatar links to threads in the thread list. injectIfEnabled is
142 // responsible of determining whether it should run or not depending on its
143 // current setting.
Adrià Vilanova Martínez854cb912022-02-02 16:18:23 +0100144 //
145 // Also, inject extra info in the thread list.
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200146 if (('tagName' in node) && (node.tagName == 'LI') &&
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200147 node.querySelector('ec-thread-summary') !== null) {
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200148 avatars.injectIfEnabled(node);
Adrià Vilanova Martínez854cb912022-02-02 16:18:23 +0100149 window.TWPTExtraInfo.injectAtThreadListIfEnabled(node);
150 }
151
152 // Inject extra info in the toolbelt of an expanded thread list item.
153 if (node.matches('ec-thread-summary .main .toolbelt')) {
154 window.TWPTExtraInfo.injectAtExpandedThreadListIfEnabled(node);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200155 }
156
Adrià Vilanova Martínezf472d492024-03-05 21:26:53 +0100157 if (node.tagName == 'IFRAME') {
158 // Redirect unified profile iframe to dark version if applicable
159 if (isDarkThemeOn(options) && unifiedProfilesFix.checkIframe(node)) {
160 unifiedProfilesFix.fixIframe(node);
161 }
162
163 // Set report dialog iframe's theme to the appropriate theme
164 reportDialogColorThemeFix.fixThemeIfReportDialogIframeAndApplicable(node);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200165 }
Adrià Vilanova Martínez74a25202022-01-23 23:36:58 +0100166
Adrià Vilanova Martínez46ecb982024-01-30 22:02:17 +0100167 // Add the "import" button in the canned responses view for the workflows
168 // feature if applicable.
Adrià Vilanova Martínez74a25202022-01-23 23:36:58 +0100169 if (node.matches('ec-canned-response-row .tags')) {
Adrià Vilanova Martíneze0d65f22022-11-06 18:49:35 +0100170 window.TWPTWorkflowsImport.addButtonIfEnabled(node);
Adrià Vilanova Martínez74a25202022-01-23 23:36:58 +0100171 }
Adrià Vilanova Martínez6e4a68d2022-01-24 21:44:32 +0100172
173 // Show additional details in the thread view.
Adrià Vilanova Martínez19f6a652023-11-15 19:38:50 +0100174 if (node.matches(
175 'sc-tailwind-thread-question-question-card sc-tailwind-thread-question-state-chips')) {
Adrià Vilanova Martínez09b3bdb2022-02-05 00:15:05 +0100176 window.TWPTExtraInfo.injectAtQuestionIfEnabled(node);
Adrià Vilanova Martínez6e4a68d2022-01-24 21:44:32 +0100177 }
Adrià Vilanova Martínez19f6a652023-11-15 19:38:50 +0100178 if (node.matches(
179 'sc-tailwind-thread-message-message-list sc-tailwind-thread-message-message-card')) {
Adrià Vilanova Martínez80f1c732023-11-16 00:52:56 +0100180 window.TWPTExtraInfo.injectAtReplyIfEnabled(node);
181 }
182
183 if (node.matches(
184 'sc-tailwind-thread-message-message-list sc-tailwind-thread-message-comment-card')) {
185 window.TWPTExtraInfo.injectAtCommentIfEnabled(node);
Adrià Vilanova Martínez74a25202022-01-23 23:36:58 +0100186 }
Adrià Vilanova Martínez4f56d562022-01-26 00:23:27 +0100187
188 // Inject per-forum stats section in the user profile
189 if (node.matches(
190 'ec-unified-user .scTailwindUser_profileUserprofilesection ' +
191 'sc-tailwind-shared-activity-chart')) {
192 window.TWPTExtraInfo.injectPerForumStatsIfEnabled(node);
193 }
Adrià Vilanova Martínez5a8055b2022-09-29 13:05:19 +0200194
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +0100195 // Inject old thread page design warning if applicable
Adrià Vilanova Martínezd2344c42022-09-30 13:14:29 +0200196 if (node.matches(
197 'ec-thread > .page > .material-content > div[role="list"]')) {
198 window.TWPTThreadPageDesignWarning.injectWarningIfApplicable(node);
Adrià Vilanova Martínez5a8055b2022-09-29 13:05:19 +0200199 }
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +0100200
201 // Inject thread toolbar
202 if (threadToolbar.shouldInject(node)) {
203 threadToolbar.injectIfApplicable(node);
204 }
Adrià Vilanova Martínez412b7582022-12-30 01:35:30 +0100205
206 // Inject parent reply quote
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +0100207 if (flattenThreads.shouldInjectQuote(node)) {
208 flattenThreads.injectQuoteIfApplicable(node);
209 }
210
211 // Inject reply button in non-nested view
212 if (flattenThreads.shouldInjectReplyBtn(node)) {
213 flattenThreads.injectReplyBtnIfApplicable(node);
Adrià Vilanova Martínez412b7582022-12-30 01:35:30 +0100214 }
Adrià Vilanova Martínez4d599252023-02-04 20:54:54 +0100215
216 // Delete additional info in the edit message box
217 if (flattenThreads.isAdditionalInfoElement(node)) {
218 flattenThreads.deleteAdditionalInfoElementIfApplicable(node);
219 }
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200220 }
221}
222
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +0100223function handleRemovedNode(mutation, node) {
224 if (!('tagName' in node)) return;
225
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +0100226 // Readd reply button when the Community Console removes it
227 if (node.tagName == 'TWPT-FLATTEN-THREAD-REPLY-BUTTON') {
228 flattenThreads.injectReplyBtn(
229 mutation.target, JSON.parse(node.getAttribute('extraInfo')));
230 }
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200231}
232
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +0100233function mutationCallback(mutationList) {
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200234 mutationList.forEach((mutation) => {
235 if (mutation.type == 'childList') {
236 mutation.addedNodes.forEach(function(node) {
237 handleCandidateNode(node);
238 });
239
240 mutation.removedNodes.forEach(function(node) {
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +0100241 handleRemovedNode(mutation, node);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200242 });
243 }
244 });
245}
246
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200247var observerOptions = {
248 childList: true,
249 subtree: true,
250};
251
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200252getOptions(null).then(items => {
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200253 options = items;
254
Adrià Vilanova Martínez27c69962021-07-17 23:32:51 +0200255 // Initialize classes needed by the mutation observer
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200256 avatars = new AvatarsHandler();
Adrià Vilanova Martínez2788d122022-10-10 22:06:25 +0200257 workflows = new Workflows();
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +0100258 threadToolbar = new ThreadToolbar();
Adrià Vilanova Martínez412b7582022-12-30 01:35:30 +0100259 flattenThreads = new FlattenThreads();
Adrià Vilanova Martínezf472d492024-03-05 21:26:53 +0100260 reportDialogColorThemeFix = new ReportDialogColorThemeFix(options);
Adrià Vilanova Martínez27c69962021-07-17 23:32:51 +0200261
Adrià Vilanova Martínez58cc67c2024-04-21 16:43:01 +0200262 // extraInfo, threadPageDesignWarning and workflowsImport are
Adrià Vilanova Martíneze0d65f22022-11-06 18:49:35 +0100263 // initialized in start.js
avm99963d3f4ac02021-08-12 18:36:58 +0200264
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200265 // Before starting the mutation Observer, check whether we missed any
266 // mutations by manually checking whether some watched nodes already
267 // exist.
268 var cssSelectors = watchedNodesSelectors.join(',');
269 document.querySelectorAll(cssSelectors)
270 .forEach(node => handleCandidateNode(node));
271
272 mutationObserver = new MutationObserver(mutationCallback);
273 mutationObserver.observe(document.body, observerOptions);
274
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200275 // TODO(avm99963): The following features are not dynamic. Make them be.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200276 if (options.fixedtoolbar) {
277 injectStyles(
Adrià Vilanovae8ba09a2024-02-29 19:25:39 +0000278 'ec-bulk-actions{position: sticky; top: 0; background: var(--TWPT-primary-background, #fff); z-index: 96;}');
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200279 }
280
281 if (options.increasecontrast) {
282 injectStyles(
283 '.thread-summary.read:not(.checked){background: var(--TWPT-thread-read-background, #ecedee)!important;}');
284 }
285
286 if (options.stickysidebarheaders) {
287 injectStyles(
288 'material-drawer .main-header{background: var(--TWPT-drawer-background, #fff)!important; position: sticky; top: 0; z-index: 1;}');
289 }
290
291 if (options.enhancedannouncementsdot) {
292 injectStylesheet(
293 chrome.runtime.getURL('css/enhanced_announcements_dot.css'));
294 }
295
296 if (options.repositionexpandthread) {
Adrià Vilanova Martínez27c69962021-07-17 23:32:51 +0200297 injectStylesheet(chrome.runtime.getURL('css/reposition_expand_thread.css'));
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200298 }
299
Adrià Vilanova Martínez9d27c212021-12-05 13:54:10 +0100300 if (options.imagemaxheight) {
301 injectStylesheet(chrome.runtime.getURL('css/image_max_height.css'));
302 }
303
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200304 if (options.ccforcehidedrawer) {
305 var drawer = document.querySelector('material-drawer');
306 if (drawer !== null && drawer.classList.contains('mat-drawer-expanded')) {
307 document.querySelector('.material-drawer-button').click();
308 }
309 }
310
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200311 // Batch lock
312 injectScript(chrome.runtime.getURL('batchLockInject.bundle.js'));
313 injectStylesheet(chrome.runtime.getURL('css/batchlock_inject.css'));
314 // Thread list avatars
315 injectStylesheet(chrome.runtime.getURL('css/thread_list_avatars.css'));
Adrià Vilanova Martínez7e8796c2022-01-23 21:46:46 +0100316 // Extra info
317 injectStylesheet(chrome.runtime.getURL('css/extrainfo.css'));
avm9996337601bc2022-02-21 10:36:45 +0100318 injectStylesheet(chrome.runtime.getURL('css/extrainfo_perforumstats.css'));
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +0100319 // Workflows, Thread toolbar
320 injectScript(chrome.runtime.getURL('litComponentsInject.bundle.js'));
321 // Thread toolbar
322 injectStylesheet(chrome.runtime.getURL('css/thread_toolbar.css'));
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +0100323 // Flatten threads
324 injectStylesheet(chrome.runtime.getURL('css/flatten_threads.css'));
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200325});
Adrià Vilanova Martínez825888a2023-07-23 01:53:48 +0200326
327new XHRProxyKillSwitchHandler();