blob: 309e41bfa8570fad511955d628b582e907df3654 [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ínez66836fd2024-05-04 22:43:06 +020049 // Thread list items (used to inject the avatars)
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020050 'li',
51
52 // Thread list (used for the autorefresh feature)
53 'ec-thread-list',
54
Adrià Vilanova Martínezf472d492024-03-05 21:26:53 +010055 // Unified profile iframe and report dialog iframe
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020056 'iframe',
Adrià Vilanova Martínez74a25202022-01-23 23:36:58 +010057
Adrià Vilanova Martínez46ecb982024-01-30 22:02:17 +010058 // Canned response tags (for the "import CR" popup for the workflows feature)
Adrià Vilanova Martínez6e4a68d2022-01-24 21:44:32 +010059 'ec-canned-response-row .tags',
Adrià Vilanova Martínez6e4a68d2022-01-24 21:44:32 +010060
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010061 // Thread page reply section (for the thread page toolbar)
62 kRepliesSectionSelector,
Adrià Vilanova Martínez412b7582022-12-30 01:35:30 +010063
64 // Reply payload (for the flatten threads UI)
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +010065 ...kFlattenThreadMatchingSelectors,
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020066];
67
68function handleCandidateNode(node) {
69 if (typeof node.classList !== 'undefined') {
70 if (('tagName' in node) && node.tagName == 'EC-APP') {
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020071 // Inject the dark mode button
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020072 // TODO(avm99963): make this feature dynamic.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020073 if (options.ccdarktheme && options.ccdarktheme_mode == 'switch') {
74 var rightControl = node.querySelector('header .right-control');
75 if (rightControl !== null)
Adrià Vilanova Martínez968e93c2024-03-11 23:18:16 +010076 injectDarkThemeButton(
77 rightControl, options.ccdarktheme_switch_status);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020078 }
79 }
80
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020081 // Show the "previous posts" links if the option is currently enabled.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020082 // Here we're selecting the 'ec-user > div' element (unique child)
Adrià Vilanova Martínez1f652522021-10-14 00:23:23 +020083 if (node.matches(
84 'ec-unified-user .scTailwindUser_profileUsercarddetails')) {
85 injectPreviousPostsLinksUnifiedProfileIfEnabled(
86 /* isCommunityConsole = */ true);
87 }
88
Adrià Vilanova Martínezeebc0ac2022-01-05 14:45:53 +010089 // #!if ['chromium', 'chromium_mv3'].includes(browser_target)
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020090 // Fix the drag&drop issue with the rich text editor if the option is
91 // currently enabled.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020092 //
93 // We target both tags because in different contexts different
94 // elements containing the text editor get added to the DOM structure.
95 // Sometimes it's a EC-MOVABLE-DIALOG which already contains the
96 // EC-RICH-TEXT-EDITOR, and sometimes it's the EC-RICH-TEXT-EDITOR
97 // directly.
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020098 if (('tagName' in node) &&
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020099 (node.tagName == 'EC-MOVABLE-DIALOG' ||
100 node.tagName == 'EC-RICH-TEXT-EDITOR')) {
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200101 applyDragAndDropFixIfEnabled(node);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200102 }
Adrià Vilanova Martínezeebc0ac2022-01-05 14:45:53 +0100103 // #!endif
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200104
Adrià Vilanova Martínez2788d122022-10-10 22:06:25 +0200105 // Inject the worflows menu in the thread list if the option is currently
106 // enabled.
107 if (workflows.shouldAddThreadListBtn(node)) {
108 workflows.addThreadListBtnIfEnabled(node);
109 }
110
Adrià Vilanova Martínezb06c13d2022-06-20 17:30:14 +0000111 // Inject the batch lock button in the thread list if the option is
112 // currently enabled.
Adrià Vilanova Martínez4107b5e2022-10-09 23:11:11 +0200113 if (batchLock.shouldAddButton(node)) {
Adrià Vilanova Martínezb06c13d2022-06-20 17:30:14 +0000114 batchLock.addButtonIfEnabled(node);
115 }
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200116
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200117 // Inject avatar links to threads in the thread list. injectIfEnabled is
118 // responsible of determining whether it should run or not depending on its
119 // current setting.
120 if (('tagName' in node) && (node.tagName == 'LI') &&
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200121 node.querySelector('ec-thread-summary') !== null) {
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200122 avatars.injectIfEnabled(node);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200123 }
124
Adrià Vilanova Martínezf472d492024-03-05 21:26:53 +0100125 if (node.tagName == 'IFRAME') {
126 // Redirect unified profile iframe to dark version if applicable
127 if (isDarkThemeOn(options) && unifiedProfilesFix.checkIframe(node)) {
128 unifiedProfilesFix.fixIframe(node);
129 }
130
131 // Set report dialog iframe's theme to the appropriate theme
132 reportDialogColorThemeFix.fixThemeIfReportDialogIframeAndApplicable(node);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200133 }
Adrià Vilanova Martínez74a25202022-01-23 23:36:58 +0100134
Adrià Vilanova Martínez46ecb982024-01-30 22:02:17 +0100135 // Add the "import" button in the canned responses view for the workflows
136 // feature if applicable.
Adrià Vilanova Martínez74a25202022-01-23 23:36:58 +0100137 if (node.matches('ec-canned-response-row .tags')) {
Adrià Vilanova Martíneze0d65f22022-11-06 18:49:35 +0100138 window.TWPTWorkflowsImport.addButtonIfEnabled(node);
Adrià Vilanova Martínez74a25202022-01-23 23:36:58 +0100139 }
Adrià Vilanova Martínez6e4a68d2022-01-24 21:44:32 +0100140
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +0100141 // Inject thread toolbar
142 if (threadToolbar.shouldInject(node)) {
143 threadToolbar.injectIfApplicable(node);
144 }
Adrià Vilanova Martínez412b7582022-12-30 01:35:30 +0100145
146 // Inject parent reply quote
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +0100147 if (flattenThreads.shouldInjectQuote(node)) {
148 flattenThreads.injectQuoteIfApplicable(node);
149 }
150
151 // Inject reply button in non-nested view
152 if (flattenThreads.shouldInjectReplyBtn(node)) {
153 flattenThreads.injectReplyBtnIfApplicable(node);
Adrià Vilanova Martínez412b7582022-12-30 01:35:30 +0100154 }
Adrià Vilanova Martínez4d599252023-02-04 20:54:54 +0100155
156 // Delete additional info in the edit message box
157 if (flattenThreads.isAdditionalInfoElement(node)) {
158 flattenThreads.deleteAdditionalInfoElementIfApplicable(node);
159 }
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200160 }
161}
162
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +0100163function handleRemovedNode(mutation, node) {
164 if (!('tagName' in node)) return;
165
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +0100166 // Readd reply button when the Community Console removes it
167 if (node.tagName == 'TWPT-FLATTEN-THREAD-REPLY-BUTTON') {
168 flattenThreads.injectReplyBtn(
169 mutation.target, JSON.parse(node.getAttribute('extraInfo')));
170 }
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200171}
172
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +0100173function mutationCallback(mutationList) {
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200174 mutationList.forEach((mutation) => {
175 if (mutation.type == 'childList') {
176 mutation.addedNodes.forEach(function(node) {
177 handleCandidateNode(node);
178 });
179
180 mutation.removedNodes.forEach(function(node) {
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +0100181 handleRemovedNode(mutation, node);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200182 });
183 }
184 });
185}
186
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200187var observerOptions = {
188 childList: true,
189 subtree: true,
190};
191
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200192getOptions(null).then(items => {
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200193 options = items;
194
Adrià Vilanova Martínez27c69962021-07-17 23:32:51 +0200195 // Initialize classes needed by the mutation observer
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200196 avatars = new AvatarsHandler();
Adrià Vilanova Martínez2788d122022-10-10 22:06:25 +0200197 workflows = new Workflows();
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +0100198 threadToolbar = new ThreadToolbar();
Adrià Vilanova Martínez412b7582022-12-30 01:35:30 +0100199 flattenThreads = new FlattenThreads();
Adrià Vilanova Martínezf472d492024-03-05 21:26:53 +0100200 reportDialogColorThemeFix = new ReportDialogColorThemeFix(options);
Adrià Vilanova Martínez27c69962021-07-17 23:32:51 +0200201
Adrià Vilanova Martínez879d44a2024-05-17 15:30:03 +0200202 // workflowsImport is initialized in start.js
avm99963d3f4ac02021-08-12 18:36:58 +0200203
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200204 // Before starting the mutation Observer, check whether we missed any
205 // mutations by manually checking whether some watched nodes already
206 // exist.
207 var cssSelectors = watchedNodesSelectors.join(',');
208 document.querySelectorAll(cssSelectors)
209 .forEach(node => handleCandidateNode(node));
210
211 mutationObserver = new MutationObserver(mutationCallback);
212 mutationObserver.observe(document.body, observerOptions);
213
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200214 // TODO(avm99963): The following features are not dynamic. Make them be.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200215 if (options.fixedtoolbar) {
216 injectStyles(
Adrià Vilanovae8ba09a2024-02-29 19:25:39 +0000217 '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 +0200218 }
219
220 if (options.increasecontrast) {
221 injectStyles(
222 '.thread-summary.read:not(.checked){background: var(--TWPT-thread-read-background, #ecedee)!important;}');
223 }
224
225 if (options.stickysidebarheaders) {
226 injectStyles(
227 'material-drawer .main-header{background: var(--TWPT-drawer-background, #fff)!important; position: sticky; top: 0; z-index: 1;}');
228 }
229
230 if (options.enhancedannouncementsdot) {
231 injectStylesheet(
232 chrome.runtime.getURL('css/enhanced_announcements_dot.css'));
233 }
234
235 if (options.repositionexpandthread) {
Adrià Vilanova Martínez27c69962021-07-17 23:32:51 +0200236 injectStylesheet(chrome.runtime.getURL('css/reposition_expand_thread.css'));
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200237 }
238
Adrià Vilanova Martínez9d27c212021-12-05 13:54:10 +0100239 if (options.imagemaxheight) {
240 injectStylesheet(chrome.runtime.getURL('css/image_max_height.css'));
241 }
242
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200243 if (options.ccforcehidedrawer) {
244 var drawer = document.querySelector('material-drawer');
245 if (drawer !== null && drawer.classList.contains('mat-drawer-expanded')) {
246 document.querySelector('.material-drawer-button').click();
247 }
248 }
249
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200250 // Batch lock
251 injectScript(chrome.runtime.getURL('batchLockInject.bundle.js'));
252 injectStylesheet(chrome.runtime.getURL('css/batchlock_inject.css'));
253 // Thread list avatars
254 injectStylesheet(chrome.runtime.getURL('css/thread_list_avatars.css'));
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +0100255 // Workflows, Thread toolbar
256 injectScript(chrome.runtime.getURL('litComponentsInject.bundle.js'));
257 // Thread toolbar
258 injectStylesheet(chrome.runtime.getURL('css/thread_toolbar.css'));
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +0100259 // Flatten threads
260 injectStylesheet(chrome.runtime.getURL('css/flatten_threads.css'));
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200261});
Adrià Vilanova Martínez825888a2023-07-23 01:53:48 +0200262
263new XHRProxyKillSwitchHandler();