blob: 1edb466265ee540a553816e3e2820cdca1fb39a8 [file] [log] [blame]
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +02001import {injectScript, injectStyles, injectStylesheet} from '../../common/contentScriptsUtils.js';
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +02002import {getOptions, isOptionEnabled} from '../../common/optionsUtils.js';
Adrià Vilanova Martínez1f652522021-10-14 00:23:23 +02003import {injectPreviousPostsLinksUnifiedProfileIfEnabled} from '../utilsCommon/unifiedProfiles.js';
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +02004
Adrià Vilanova Martínez27c69962021-07-17 23:32:51 +02005import AvatarsHandler from './avatars.js';
Adrià Vilanova Martínez462280f2021-08-07 22:59:02 +02006import {batchLock} from './batchLock.js';
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +02007import {injectDarkModeButton, isDarkThemeOn} from './darkMode.js';
Adrià Vilanova Martínezeebc0ac2022-01-05 14:45:53 +01008// #!if ['chromium', 'chromium_mv3'].includes(browser_target)
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +02009import {applyDragAndDropFixIfEnabled} from './dragAndDropFix.js';
Adrià Vilanova Martínezeebc0ac2022-01-05 14:45:53 +010010// #!endif
Adrià Vilanova Martíneza7ae3db2022-01-28 11:57:27 +010011import InfiniteScroll from './infiniteScroll.js';
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010012import {kRepliesSectionSelector} from './threadToolbar/constants.js';
13import ThreadToolbar from './threadToolbar/threadToolbar.js';
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020014import {unifiedProfilesFix} from './unifiedProfiles.js';
Adrià Vilanova Martínez2788d122022-10-10 22:06:25 +020015import Workflows from './workflows/workflows.js';
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020016
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010017var mutationObserver, options, avatars, infiniteScroll, workflows,
18 threadToolbar;
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020019
20const watchedNodesSelectors = [
21 // App container (used to set up the intersection observer and inject the dark
22 // mode button)
23 'ec-app',
24
Adrià Vilanova Martíneza7ae3db2022-01-28 11:57:27 +010025 // Scrollable content (used for the intersection observer)
26 '.scrollable-content',
27
28 // Load more bar and buttons
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020029 '.load-more-bar',
Adrià Vilanova Martíneza7ae3db2022-01-28 11:57:27 +010030 '.scTailwindThreadMorebuttonbutton',
Adrià Vilanova Martínez88854972022-08-28 11:57:12 +020031 '.scTailwindThreadMessagegapbutton',
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020032
Adrià Vilanova Martínez7e8796c2022-01-23 21:46:46 +010033 // User profile card inside ec-unified-user
34 'ec-unified-user .scTailwindUser_profileUsercardmain',
35
Adrià Vilanova Martínez531cd072021-12-05 20:15:43 +010036 // Username span/editor inside ec-unified-user (user profile view)
Adrià Vilanova Martínez1f652522021-10-14 00:23:23 +020037 'ec-unified-user .scTailwindUser_profileUsercarddetails',
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020038
39 // Rich text editor
40 'ec-movable-dialog',
41 'ec-rich-text-editor',
42
43 // Read/unread bulk action in the list of thread, for the batch lock feature
44 'ec-bulk-actions material-button[debugid="mark-read-button"]',
45 'ec-bulk-actions material-button[debugid="mark-unread-button"]',
46
Adrià Vilanova Martínez854cb912022-02-02 16:18:23 +010047 // Thread list items (used to inject the avatars and extra info)
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020048 'li',
49
Adrià Vilanova Martínez854cb912022-02-02 16:18:23 +010050 // Thread list item toolbelt (used for the extra info feature)
51 'ec-thread-summary .main .toolbelt',
52
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020053 // Thread list (used for the autorefresh feature)
54 'ec-thread-list',
55
56 // Unified profile iframe
57 'iframe',
Adrià Vilanova Martínez74a25202022-01-23 23:36:58 +010058
Adrià Vilanova Martíneze0d65f22022-11-06 18:49:35 +010059 // Canned response tags or toolbelt (for the extra info feature and the
60 // "import CR" popup for the workflows feature)
Adrià Vilanova Martínez6e4a68d2022-01-24 21:44:32 +010061 'ec-canned-response-row .tags',
62 'ec-canned-response-row .main .toolbelt',
63
Adrià Vilanova Martínez09b3bdb2022-02-05 00:15:05 +010064 // Question state chips container (for the extra info feature)
65 'ec-question .state-chips',
Adrià Vilanova Martínez6e4a68d2022-01-24 21:44:32 +010066
67 // Replies (for the extra info feature)
68 'ec-thread ec-message',
Adrià Vilanova Martínez4f56d562022-01-26 00:23:27 +010069
70 // User activity chart (for the per-forum stats feature)
71 'ec-unified-user .scTailwindUser_profileUserprofilesection ' +
72 'sc-tailwind-shared-activity-chart',
Adrià Vilanova Martínez5a8055b2022-09-29 13:05:19 +020073
74 // Thread page main content
75 'ec-thread > .page > .material-content > div[role="list"]',
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010076
77 // Thread page reply section (for the thread page toolbar)
78 kRepliesSectionSelector,
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020079];
80
81function handleCandidateNode(node) {
82 if (typeof node.classList !== 'undefined') {
83 if (('tagName' in node) && node.tagName == 'EC-APP') {
Adrià Vilanova Martíneza7ae3db2022-01-28 11:57:27 +010084 infiniteScroll.setUpIntersectionObserver(node, false);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020085
86 // Inject the dark mode button
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020087 // TODO(avm99963): make this feature dynamic.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020088 if (options.ccdarktheme && options.ccdarktheme_mode == 'switch') {
89 var rightControl = node.querySelector('header .right-control');
90 if (rightControl !== null)
91 injectDarkModeButton(rightControl, options.ccdarktheme_switch_status);
92 }
93 }
94
Adrià Vilanova Martíneza7ae3db2022-01-28 11:57:27 +010095 // To set up infinite scroll
96 if (node.classList.contains('scrollable-content')) {
97 infiniteScroll.setUpIntersectionObserver(node, true);
98 }
99
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200100 // Start the intersectionObserver for the "load more"/"load all" buttons
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200101 // inside a thread if the option is currently enabled.
102 if (node.classList.contains('load-more-bar')) {
Adrià Vilanova Martíneza7ae3db2022-01-28 11:57:27 +0100103 infiniteScroll.observeLoadMoreBar(node);
104 }
Adrià Vilanova Martínez88854972022-08-28 11:57:12 +0200105 if (node.classList.contains('scTailwindThreadMorebuttonbutton') ||
106 node.classList.contains('scTailwindThreadMessagegapbutton')) {
Adrià Vilanova Martíneza7ae3db2022-01-28 11:57:27 +0100107 infiniteScroll.observeLoadMoreInteropBtn(node);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200108 }
109
Adrià Vilanova Martínez7e8796c2022-01-23 21:46:46 +0100110 // Show additional details in the profile view.
111 if (node.matches('ec-unified-user .scTailwindUser_profileUsercardmain')) {
112 window.TWPTExtraInfo.injectAtProfileIfEnabled(node);
113 }
114
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200115 // Show the "previous posts" links if the option is currently enabled.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200116 // Here we're selecting the 'ec-user > div' element (unique child)
Adrià Vilanova Martínez1f652522021-10-14 00:23:23 +0200117 if (node.matches(
118 'ec-unified-user .scTailwindUser_profileUsercarddetails')) {
119 injectPreviousPostsLinksUnifiedProfileIfEnabled(
120 /* isCommunityConsole = */ true);
121 }
122
Adrià Vilanova Martínezeebc0ac2022-01-05 14:45:53 +0100123 // #!if ['chromium', 'chromium_mv3'].includes(browser_target)
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200124 // Fix the drag&drop issue with the rich text editor if the option is
125 // currently enabled.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200126 //
127 // We target both tags because in different contexts different
128 // elements containing the text editor get added to the DOM structure.
129 // Sometimes it's a EC-MOVABLE-DIALOG which already contains the
130 // EC-RICH-TEXT-EDITOR, and sometimes it's the EC-RICH-TEXT-EDITOR
131 // directly.
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200132 if (('tagName' in node) &&
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200133 (node.tagName == 'EC-MOVABLE-DIALOG' ||
134 node.tagName == 'EC-RICH-TEXT-EDITOR')) {
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200135 applyDragAndDropFixIfEnabled(node);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200136 }
Adrià Vilanova Martínezeebc0ac2022-01-05 14:45:53 +0100137 // #!endif
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200138
Adrià Vilanova Martínez2788d122022-10-10 22:06:25 +0200139 // Inject the worflows menu in the thread list if the option is currently
140 // enabled.
141 if (workflows.shouldAddThreadListBtn(node)) {
142 workflows.addThreadListBtnIfEnabled(node);
143 }
144
Adrià Vilanova Martínezb06c13d2022-06-20 17:30:14 +0000145 // Inject the batch lock button in the thread list if the option is
146 // currently enabled.
Adrià Vilanova Martínez4107b5e2022-10-09 23:11:11 +0200147 if (batchLock.shouldAddButton(node)) {
Adrià Vilanova Martínezb06c13d2022-06-20 17:30:14 +0000148 batchLock.addButtonIfEnabled(node);
149 }
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200150
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200151 // Inject avatar links to threads in the thread list. injectIfEnabled is
152 // responsible of determining whether it should run or not depending on its
153 // current setting.
Adrià Vilanova Martínez854cb912022-02-02 16:18:23 +0100154 //
155 // Also, inject extra info in the thread list.
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200156 if (('tagName' in node) && (node.tagName == 'LI') &&
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200157 node.querySelector('ec-thread-summary') !== null) {
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200158 avatars.injectIfEnabled(node);
Adrià Vilanova Martínez854cb912022-02-02 16:18:23 +0100159 window.TWPTExtraInfo.injectAtThreadListIfEnabled(node);
160 }
161
162 // Inject extra info in the toolbelt of an expanded thread list item.
163 if (node.matches('ec-thread-summary .main .toolbelt')) {
164 window.TWPTExtraInfo.injectAtExpandedThreadListIfEnabled(node);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200165 }
166
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200167 // Set up the autorefresh list feature. The setUp function is responsible
168 // of determining whether it should run or not depending on the current
169 // setting.
170 if (('tagName' in node) && node.tagName == 'EC-THREAD-LIST') {
avm99963fd222672021-08-12 23:23:01 +0200171 window.TWPTAutoRefresh.setUp();
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200172 }
173
174 // Redirect unified profile iframe to dark version if applicable
175 if (node.tagName == 'IFRAME' && isDarkThemeOn(options) &&
176 unifiedProfilesFix.checkIframe(node)) {
177 unifiedProfilesFix.fixIframe(node);
178 }
Adrià Vilanova Martínez74a25202022-01-23 23:36:58 +0100179
Adrià Vilanova Martíneze0d65f22022-11-06 18:49:35 +0100180 // Show additional details in the canned responses view (and add the
181 // "import" button if applicable for the workflows feature).
Adrià Vilanova Martínez74a25202022-01-23 23:36:58 +0100182 if (node.matches('ec-canned-response-row .tags')) {
183 window.TWPTExtraInfo.injectAtCRIfEnabled(node, /* isExpanded = */ false);
Adrià Vilanova Martíneze0d65f22022-11-06 18:49:35 +0100184 window.TWPTWorkflowsImport.addButtonIfEnabled(node);
Adrià Vilanova Martínez74a25202022-01-23 23:36:58 +0100185 }
186 if (node.matches('ec-canned-response-row .main .toolbelt')) {
187 const tags = node.parentNode?.querySelector?.('.tags');
Adrià Vilanova Martínez6e4a68d2022-01-24 21:44:32 +0100188 if (tags)
189 window.TWPTExtraInfo.injectAtCRIfEnabled(tags, /* isExpanded = */ true);
190 }
191
192 // Show additional details in the thread view.
Adrià Vilanova Martínez09b3bdb2022-02-05 00:15:05 +0100193 if (node.matches('ec-question .state-chips')) {
194 window.TWPTExtraInfo.injectAtQuestionIfEnabled(node);
Adrià Vilanova Martínez6e4a68d2022-01-24 21:44:32 +0100195 }
196 if (node.matches('ec-thread ec-message')) {
197 window.TWPTExtraInfo.injectAtMessageIfEnabled(node);
Adrià Vilanova Martínez74a25202022-01-23 23:36:58 +0100198 }
Adrià Vilanova Martínez4f56d562022-01-26 00:23:27 +0100199
200 // Inject per-forum stats section in the user profile
201 if (node.matches(
202 'ec-unified-user .scTailwindUser_profileUserprofilesection ' +
203 'sc-tailwind-shared-activity-chart')) {
204 window.TWPTExtraInfo.injectPerForumStatsIfEnabled(node);
205 }
Adrià Vilanova Martínez5a8055b2022-09-29 13:05:19 +0200206
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +0100207 // Inject old thread page design warning if applicable
Adrià Vilanova Martínezd2344c42022-09-30 13:14:29 +0200208 if (node.matches(
209 'ec-thread > .page > .material-content > div[role="list"]')) {
210 window.TWPTThreadPageDesignWarning.injectWarningIfApplicable(node);
Adrià Vilanova Martínez5a8055b2022-09-29 13:05:19 +0200211 }
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +0100212
213 // Inject thread toolbar
214 if (threadToolbar.shouldInject(node)) {
215 threadToolbar.injectIfApplicable(node);
216 }
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200217 }
218}
219
220function handleRemovedNode(node) {
221 // Remove snackbar when exiting thread list view
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200222 if ('tagName' in node && node.tagName == 'EC-THREAD-LIST') {
avm99963fd222672021-08-12 23:23:01 +0200223 window.TWPTAutoRefresh.hideUpdatePrompt();
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200224 }
225}
226
227function mutationCallback(mutationList, observer) {
228 mutationList.forEach((mutation) => {
229 if (mutation.type == 'childList') {
230 mutation.addedNodes.forEach(function(node) {
231 handleCandidateNode(node);
232 });
233
234 mutation.removedNodes.forEach(function(node) {
235 handleRemovedNode(node);
236 });
237 }
238 });
239}
240
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200241var observerOptions = {
242 childList: true,
243 subtree: true,
244};
245
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200246getOptions(null).then(items => {
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200247 options = items;
248
Adrià Vilanova Martínez27c69962021-07-17 23:32:51 +0200249 // Initialize classes needed by the mutation observer
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200250 avatars = new AvatarsHandler();
Adrià Vilanova Martíneza7ae3db2022-01-28 11:57:27 +0100251 infiniteScroll = new InfiniteScroll();
Adrià Vilanova Martínez2788d122022-10-10 22:06:25 +0200252 workflows = new Workflows();
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +0100253 threadToolbar = new ThreadToolbar();
Adrià Vilanova Martínez27c69962021-07-17 23:32:51 +0200254
Adrià Vilanova Martíneze0d65f22022-11-06 18:49:35 +0100255 // autoRefresh, extraInfo, threadPageDesignWarning and workflowsImport are
256 // initialized in start.js
avm99963d3f4ac02021-08-12 18:36:58 +0200257
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200258 // Before starting the mutation Observer, check whether we missed any
259 // mutations by manually checking whether some watched nodes already
260 // exist.
261 var cssSelectors = watchedNodesSelectors.join(',');
262 document.querySelectorAll(cssSelectors)
263 .forEach(node => handleCandidateNode(node));
264
265 mutationObserver = new MutationObserver(mutationCallback);
266 mutationObserver.observe(document.body, observerOptions);
267
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200268 // TODO(avm99963): The following features are not dynamic. Make them be.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200269 if (options.fixedtoolbar) {
270 injectStyles(
271 'ec-bulk-actions{position: sticky; top: 0; background: var(--TWPT-primary-background, #fff); z-index: 96;}');
272 }
273
274 if (options.increasecontrast) {
275 injectStyles(
276 '.thread-summary.read:not(.checked){background: var(--TWPT-thread-read-background, #ecedee)!important;}');
277 }
278
279 if (options.stickysidebarheaders) {
280 injectStyles(
281 'material-drawer .main-header{background: var(--TWPT-drawer-background, #fff)!important; position: sticky; top: 0; z-index: 1;}');
282 }
283
284 if (options.enhancedannouncementsdot) {
285 injectStylesheet(
286 chrome.runtime.getURL('css/enhanced_announcements_dot.css'));
287 }
288
289 if (options.repositionexpandthread) {
Adrià Vilanova Martínez27c69962021-07-17 23:32:51 +0200290 injectStylesheet(chrome.runtime.getURL('css/reposition_expand_thread.css'));
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200291 }
292
Adrià Vilanova Martínez9d27c212021-12-05 13:54:10 +0100293 if (options.imagemaxheight) {
294 injectStylesheet(chrome.runtime.getURL('css/image_max_height.css'));
295 }
296
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200297 if (options.ccforcehidedrawer) {
298 var drawer = document.querySelector('material-drawer');
299 if (drawer !== null && drawer.classList.contains('mat-drawer-expanded')) {
300 document.querySelector('.material-drawer-button').click();
301 }
302 }
303
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200304 // Batch lock
305 injectScript(chrome.runtime.getURL('batchLockInject.bundle.js'));
306 injectStylesheet(chrome.runtime.getURL('css/batchlock_inject.css'));
307 // Thread list avatars
308 injectStylesheet(chrome.runtime.getURL('css/thread_list_avatars.css'));
309 // Auto refresh list
310 injectStylesheet(chrome.runtime.getURL('css/autorefresh_list.css'));
Adrià Vilanova Martínez7e8796c2022-01-23 21:46:46 +0100311 // Extra info
312 injectStylesheet(chrome.runtime.getURL('css/extrainfo.css'));
avm9996337601bc2022-02-21 10:36:45 +0100313 injectStylesheet(chrome.runtime.getURL('css/extrainfo_perforumstats.css'));
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +0100314 // Workflows, Thread toolbar
315 injectScript(chrome.runtime.getURL('litComponentsInject.bundle.js'));
316 // Thread toolbar
317 injectStylesheet(chrome.runtime.getURL('css/thread_toolbar.css'));
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200318});