blob: 964a647a706aad7a287598c21ef021d746e7e158 [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ínez3465e772021-07-11 19:18:41 +020012import {unifiedProfilesFix} from './unifiedProfiles.js';
Adrià Vilanova Martínez2788d122022-10-10 22:06:25 +020013import Workflows from './workflows/workflows.js';
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020014
Adrià Vilanova Martínez2788d122022-10-10 22:06:25 +020015var mutationObserver, options, avatars, infiniteScroll, workflows;
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020016
17const watchedNodesSelectors = [
18 // App container (used to set up the intersection observer and inject the dark
19 // mode button)
20 'ec-app',
21
Adrià Vilanova Martíneza7ae3db2022-01-28 11:57:27 +010022 // Scrollable content (used for the intersection observer)
23 '.scrollable-content',
24
25 // Load more bar and buttons
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020026 '.load-more-bar',
Adrià Vilanova Martíneza7ae3db2022-01-28 11:57:27 +010027 '.scTailwindThreadMorebuttonbutton',
Adrià Vilanova Martínez88854972022-08-28 11:57:12 +020028 '.scTailwindThreadMessagegapbutton',
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020029
Adrià Vilanova Martínez7e8796c2022-01-23 21:46:46 +010030 // User profile card inside ec-unified-user
31 'ec-unified-user .scTailwindUser_profileUsercardmain',
32
Adrià Vilanova Martínez531cd072021-12-05 20:15:43 +010033 // Username span/editor inside ec-unified-user (user profile view)
Adrià Vilanova Martínez1f652522021-10-14 00:23:23 +020034 'ec-unified-user .scTailwindUser_profileUsercarddetails',
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020035
36 // Rich text editor
37 'ec-movable-dialog',
38 'ec-rich-text-editor',
39
40 // Read/unread bulk action in the list of thread, for the batch lock feature
41 'ec-bulk-actions material-button[debugid="mark-read-button"]',
42 'ec-bulk-actions material-button[debugid="mark-unread-button"]',
43
Adrià Vilanova Martínez854cb912022-02-02 16:18:23 +010044 // Thread list items (used to inject the avatars and extra info)
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020045 'li',
46
Adrià Vilanova Martínez854cb912022-02-02 16:18:23 +010047 // Thread list item toolbelt (used for the extra info feature)
48 'ec-thread-summary .main .toolbelt',
49
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020050 // Thread list (used for the autorefresh feature)
51 'ec-thread-list',
52
53 // Unified profile iframe
54 'iframe',
Adrià Vilanova Martínez74a25202022-01-23 23:36:58 +010055
56 // Canned response tags or toolbelt (for the extra info feature)
Adrià Vilanova Martínez6e4a68d2022-01-24 21:44:32 +010057 'ec-canned-response-row .tags',
58 'ec-canned-response-row .main .toolbelt',
59
Adrià Vilanova Martínez09b3bdb2022-02-05 00:15:05 +010060 // Question state chips container (for the extra info feature)
61 'ec-question .state-chips',
Adrià Vilanova Martínez6e4a68d2022-01-24 21:44:32 +010062
63 // Replies (for the extra info feature)
64 'ec-thread ec-message',
Adrià Vilanova Martínez4f56d562022-01-26 00:23:27 +010065
66 // User activity chart (for the per-forum stats feature)
67 'ec-unified-user .scTailwindUser_profileUserprofilesection ' +
68 'sc-tailwind-shared-activity-chart',
Adrià Vilanova Martínez5a8055b2022-09-29 13:05:19 +020069
70 // Thread page main content
71 'ec-thread > .page > .material-content > div[role="list"]',
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020072];
73
74function handleCandidateNode(node) {
75 if (typeof node.classList !== 'undefined') {
76 if (('tagName' in node) && node.tagName == 'EC-APP') {
Adrià Vilanova Martíneza7ae3db2022-01-28 11:57:27 +010077 infiniteScroll.setUpIntersectionObserver(node, false);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020078
79 // Inject the dark mode button
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020080 // TODO(avm99963): make this feature dynamic.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020081 if (options.ccdarktheme && options.ccdarktheme_mode == 'switch') {
82 var rightControl = node.querySelector('header .right-control');
83 if (rightControl !== null)
84 injectDarkModeButton(rightControl, options.ccdarktheme_switch_status);
85 }
86 }
87
Adrià Vilanova Martíneza7ae3db2022-01-28 11:57:27 +010088 // To set up infinite scroll
89 if (node.classList.contains('scrollable-content')) {
90 infiniteScroll.setUpIntersectionObserver(node, true);
91 }
92
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020093 // Start the intersectionObserver for the "load more"/"load all" buttons
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020094 // inside a thread if the option is currently enabled.
95 if (node.classList.contains('load-more-bar')) {
Adrià Vilanova Martíneza7ae3db2022-01-28 11:57:27 +010096 infiniteScroll.observeLoadMoreBar(node);
97 }
Adrià Vilanova Martínez88854972022-08-28 11:57:12 +020098 if (node.classList.contains('scTailwindThreadMorebuttonbutton') ||
99 node.classList.contains('scTailwindThreadMessagegapbutton')) {
Adrià Vilanova Martíneza7ae3db2022-01-28 11:57:27 +0100100 infiniteScroll.observeLoadMoreInteropBtn(node);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200101 }
102
Adrià Vilanova Martínez7e8796c2022-01-23 21:46:46 +0100103 // Show additional details in the profile view.
104 if (node.matches('ec-unified-user .scTailwindUser_profileUsercardmain')) {
105 window.TWPTExtraInfo.injectAtProfileIfEnabled(node);
106 }
107
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200108 // Show the "previous posts" links if the option is currently enabled.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200109 // Here we're selecting the 'ec-user > div' element (unique child)
Adrià Vilanova Martínez1f652522021-10-14 00:23:23 +0200110 if (node.matches(
111 'ec-unified-user .scTailwindUser_profileUsercarddetails')) {
112 injectPreviousPostsLinksUnifiedProfileIfEnabled(
113 /* isCommunityConsole = */ true);
114 }
115
Adrià Vilanova Martínezeebc0ac2022-01-05 14:45:53 +0100116 // #!if ['chromium', 'chromium_mv3'].includes(browser_target)
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200117 // Fix the drag&drop issue with the rich text editor if the option is
118 // currently enabled.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200119 //
120 // We target both tags because in different contexts different
121 // elements containing the text editor get added to the DOM structure.
122 // Sometimes it's a EC-MOVABLE-DIALOG which already contains the
123 // EC-RICH-TEXT-EDITOR, and sometimes it's the EC-RICH-TEXT-EDITOR
124 // directly.
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200125 if (('tagName' in node) &&
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200126 (node.tagName == 'EC-MOVABLE-DIALOG' ||
127 node.tagName == 'EC-RICH-TEXT-EDITOR')) {
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200128 applyDragAndDropFixIfEnabled(node);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200129 }
Adrià Vilanova Martínezeebc0ac2022-01-05 14:45:53 +0100130 // #!endif
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200131
Adrià Vilanova Martínez2788d122022-10-10 22:06:25 +0200132 // Inject the worflows menu in the thread list if the option is currently
133 // enabled.
134 if (workflows.shouldAddThreadListBtn(node)) {
135 workflows.addThreadListBtnIfEnabled(node);
136 }
137
Adrià Vilanova Martínezb06c13d2022-06-20 17:30:14 +0000138 // Inject the batch lock button in the thread list if the option is
139 // currently enabled.
Adrià Vilanova Martínez4107b5e2022-10-09 23:11:11 +0200140 if (batchLock.shouldAddButton(node)) {
Adrià Vilanova Martínezb06c13d2022-06-20 17:30:14 +0000141 batchLock.addButtonIfEnabled(node);
142 }
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200143
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200144 // Inject avatar links to threads in the thread list. injectIfEnabled is
145 // responsible of determining whether it should run or not depending on its
146 // current setting.
Adrià Vilanova Martínez854cb912022-02-02 16:18:23 +0100147 //
148 // Also, inject extra info in the thread list.
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200149 if (('tagName' in node) && (node.tagName == 'LI') &&
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200150 node.querySelector('ec-thread-summary') !== null) {
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200151 avatars.injectIfEnabled(node);
Adrià Vilanova Martínez854cb912022-02-02 16:18:23 +0100152 window.TWPTExtraInfo.injectAtThreadListIfEnabled(node);
153 }
154
155 // Inject extra info in the toolbelt of an expanded thread list item.
156 if (node.matches('ec-thread-summary .main .toolbelt')) {
157 window.TWPTExtraInfo.injectAtExpandedThreadListIfEnabled(node);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200158 }
159
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200160 // Set up the autorefresh list feature. The setUp function is responsible
161 // of determining whether it should run or not depending on the current
162 // setting.
163 if (('tagName' in node) && node.tagName == 'EC-THREAD-LIST') {
avm99963fd222672021-08-12 23:23:01 +0200164 window.TWPTAutoRefresh.setUp();
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200165 }
166
167 // Redirect unified profile iframe to dark version if applicable
168 if (node.tagName == 'IFRAME' && isDarkThemeOn(options) &&
169 unifiedProfilesFix.checkIframe(node)) {
170 unifiedProfilesFix.fixIframe(node);
171 }
Adrià Vilanova Martínez74a25202022-01-23 23:36:58 +0100172
173 // Show additional details in the canned responses view.
174 if (node.matches('ec-canned-response-row .tags')) {
175 window.TWPTExtraInfo.injectAtCRIfEnabled(node, /* isExpanded = */ false);
176 }
177 if (node.matches('ec-canned-response-row .main .toolbelt')) {
178 const tags = node.parentNode?.querySelector?.('.tags');
Adrià Vilanova Martínez6e4a68d2022-01-24 21:44:32 +0100179 if (tags)
180 window.TWPTExtraInfo.injectAtCRIfEnabled(tags, /* isExpanded = */ true);
181 }
182
183 // Show additional details in the thread view.
Adrià Vilanova Martínez09b3bdb2022-02-05 00:15:05 +0100184 if (node.matches('ec-question .state-chips')) {
185 window.TWPTExtraInfo.injectAtQuestionIfEnabled(node);
Adrià Vilanova Martínez6e4a68d2022-01-24 21:44:32 +0100186 }
187 if (node.matches('ec-thread ec-message')) {
188 window.TWPTExtraInfo.injectAtMessageIfEnabled(node);
Adrià Vilanova Martínez74a25202022-01-23 23:36:58 +0100189 }
Adrià Vilanova Martínez4f56d562022-01-26 00:23:27 +0100190
191 // Inject per-forum stats section in the user profile
192 if (node.matches(
193 'ec-unified-user .scTailwindUser_profileUserprofilesection ' +
194 'sc-tailwind-shared-activity-chart')) {
195 window.TWPTExtraInfo.injectPerForumStatsIfEnabled(node);
196 }
Adrià Vilanova Martínez5a8055b2022-09-29 13:05:19 +0200197
Adrià Vilanova Martínezd2344c42022-09-30 13:14:29 +0200198 if (node.matches(
199 'ec-thread > .page > .material-content > div[role="list"]')) {
200 window.TWPTThreadPageDesignWarning.injectWarningIfApplicable(node);
Adrià Vilanova Martínez5a8055b2022-09-29 13:05:19 +0200201 }
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200202 }
203}
204
205function handleRemovedNode(node) {
206 // Remove snackbar when exiting thread list view
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200207 if ('tagName' in node && node.tagName == 'EC-THREAD-LIST') {
avm99963fd222672021-08-12 23:23:01 +0200208 window.TWPTAutoRefresh.hideUpdatePrompt();
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200209 }
210}
211
212function mutationCallback(mutationList, observer) {
213 mutationList.forEach((mutation) => {
214 if (mutation.type == 'childList') {
215 mutation.addedNodes.forEach(function(node) {
216 handleCandidateNode(node);
217 });
218
219 mutation.removedNodes.forEach(function(node) {
220 handleRemovedNode(node);
221 });
222 }
223 });
224}
225
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200226var observerOptions = {
227 childList: true,
228 subtree: true,
229};
230
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200231getOptions(null).then(items => {
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200232 options = items;
233
Adrià Vilanova Martínez27c69962021-07-17 23:32:51 +0200234 // Initialize classes needed by the mutation observer
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200235 avatars = new AvatarsHandler();
Adrià Vilanova Martíneza7ae3db2022-01-28 11:57:27 +0100236 infiniteScroll = new InfiniteScroll();
Adrià Vilanova Martínez2788d122022-10-10 22:06:25 +0200237 workflows = new Workflows();
Adrià Vilanova Martínez27c69962021-07-17 23:32:51 +0200238
Adrià Vilanova Martínezd2344c42022-09-30 13:14:29 +0200239 // autoRefresh, extraInfo and threadPageDesignWarning are initialized in
240 // start.js
avm99963d3f4ac02021-08-12 18:36:58 +0200241
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200242 // Before starting the mutation Observer, check whether we missed any
243 // mutations by manually checking whether some watched nodes already
244 // exist.
245 var cssSelectors = watchedNodesSelectors.join(',');
246 document.querySelectorAll(cssSelectors)
247 .forEach(node => handleCandidateNode(node));
248
249 mutationObserver = new MutationObserver(mutationCallback);
250 mutationObserver.observe(document.body, observerOptions);
251
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200252 // TODO(avm99963): The following features are not dynamic. Make them be.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200253 if (options.fixedtoolbar) {
254 injectStyles(
255 'ec-bulk-actions{position: sticky; top: 0; background: var(--TWPT-primary-background, #fff); z-index: 96;}');
256 }
257
258 if (options.increasecontrast) {
259 injectStyles(
260 '.thread-summary.read:not(.checked){background: var(--TWPT-thread-read-background, #ecedee)!important;}');
261 }
262
263 if (options.stickysidebarheaders) {
264 injectStyles(
265 'material-drawer .main-header{background: var(--TWPT-drawer-background, #fff)!important; position: sticky; top: 0; z-index: 1;}');
266 }
267
268 if (options.enhancedannouncementsdot) {
269 injectStylesheet(
270 chrome.runtime.getURL('css/enhanced_announcements_dot.css'));
271 }
272
273 if (options.repositionexpandthread) {
Adrià Vilanova Martínez27c69962021-07-17 23:32:51 +0200274 injectStylesheet(chrome.runtime.getURL('css/reposition_expand_thread.css'));
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200275 }
276
Adrià Vilanova Martínez9d27c212021-12-05 13:54:10 +0100277 if (options.imagemaxheight) {
278 injectStylesheet(chrome.runtime.getURL('css/image_max_height.css'));
279 }
280
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200281 if (options.ccforcehidedrawer) {
282 var drawer = document.querySelector('material-drawer');
283 if (drawer !== null && drawer.classList.contains('mat-drawer-expanded')) {
284 document.querySelector('.material-drawer-button').click();
285 }
286 }
287
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200288 // Batch lock
289 injectScript(chrome.runtime.getURL('batchLockInject.bundle.js'));
290 injectStylesheet(chrome.runtime.getURL('css/batchlock_inject.css'));
291 // Thread list avatars
292 injectStylesheet(chrome.runtime.getURL('css/thread_list_avatars.css'));
293 // Auto refresh list
294 injectStylesheet(chrome.runtime.getURL('css/autorefresh_list.css'));
Adrià Vilanova Martínez7e8796c2022-01-23 21:46:46 +0100295 // Extra info
296 injectStylesheet(chrome.runtime.getURL('css/extrainfo.css'));
avm9996337601bc2022-02-21 10:36:45 +0100297 injectStylesheet(chrome.runtime.getURL('css/extrainfo_perforumstats.css'));
Adrià Vilanova Martínez2788d122022-10-10 22:06:25 +0200298 // Workflows
299 injectScript(chrome.runtime.getURL('workflowComponentsInject.bundle.js'));
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200300});