blob: a534f38b3305fadb0c09b9fa12c8760947653c05 [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ínez3465e772021-07-11 19:18:41 +020011import {unifiedProfilesFix} from './unifiedProfiles.js';
Adrià Vilanova Martínez1e10d192021-12-31 16:01:13 +010012import Workflows from './workflows/workflows.js';
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020013
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020014var mutationObserver, intersectionObserver, intersectionOptions, options,
Adrià Vilanova Martínez1e10d192021-12-31 16:01:13 +010015 avatars, 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
22 // Load more bar (for the "load more"/"load all" buttons)
23 '.load-more-bar',
24
Adrià Vilanova Martínez7e8796c2022-01-23 21:46:46 +010025 // User profile card inside ec-unified-user
26 'ec-unified-user .scTailwindUser_profileUsercardmain',
27
Adrià Vilanova Martínez531cd072021-12-05 20:15:43 +010028 // Username span/editor inside ec-unified-user (user profile view)
Adrià Vilanova Martínez1f652522021-10-14 00:23:23 +020029 'ec-unified-user .scTailwindUser_profileUsercarddetails',
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020030
31 // Rich text editor
32 'ec-movable-dialog',
33 'ec-rich-text-editor',
34
35 // Read/unread bulk action in the list of thread, for the batch lock feature
36 'ec-bulk-actions material-button[debugid="mark-read-button"]',
37 'ec-bulk-actions material-button[debugid="mark-unread-button"]',
38
39 // Thread list items (used to inject the avatars)
40 'li',
41
42 // Thread list (used for the autorefresh feature)
43 'ec-thread-list',
44
45 // Unified profile iframe
46 'iframe',
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020047];
48
49function handleCandidateNode(node) {
50 if (typeof node.classList !== 'undefined') {
51 if (('tagName' in node) && node.tagName == 'EC-APP') {
52 // Set up the intersectionObserver
53 if (typeof intersectionObserver === 'undefined') {
54 var scrollableContent = node.querySelector('.scrollable-content');
55 if (scrollableContent !== null) {
56 intersectionOptions = {
57 root: scrollableContent,
58 rootMargin: '0px',
59 threshold: 1.0,
60 };
61
62 intersectionObserver = new IntersectionObserver(
63 intersectionCallback, intersectionOptions);
64 }
65 }
66
67 // Inject the dark mode button
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020068 // TODO(avm99963): make this feature dynamic.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020069 if (options.ccdarktheme && options.ccdarktheme_mode == 'switch') {
70 var rightControl = node.querySelector('header .right-control');
71 if (rightControl !== null)
72 injectDarkModeButton(rightControl, options.ccdarktheme_switch_status);
73 }
74 }
75
76 // Start the intersectionObserver for the "load more"/"load all" buttons
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020077 // inside a thread if the option is currently enabled.
78 if (node.classList.contains('load-more-bar')) {
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020079 if (typeof intersectionObserver !== 'undefined') {
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020080 getOptions(['thread', 'threadall']).then(threadOptions => {
81 if (threadOptions.thread)
82 intersectionObserver.observe(
83 node.querySelector('.load-more-button'));
84 if (threadOptions.threadall)
85 intersectionObserver.observe(
86 node.querySelector('.load-all-button'));
87 });
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020088 } else {
89 console.warn(
90 '[infinitescroll] ' +
91 'The intersectionObserver is not ready yet.');
92 }
93 }
94
Adrià Vilanova Martínez7e8796c2022-01-23 21:46:46 +010095 // Show additional details in the profile view.
96 if (node.matches('ec-unified-user .scTailwindUser_profileUsercardmain')) {
97 window.TWPTExtraInfo.injectAtProfileIfEnabled(node);
98 }
99
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200100 // Show the "previous posts" links if the option is currently enabled.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200101 // Here we're selecting the 'ec-user > div' element (unique child)
Adrià Vilanova Martínez1f652522021-10-14 00:23:23 +0200102 if (node.matches(
103 'ec-unified-user .scTailwindUser_profileUsercarddetails')) {
104 injectPreviousPostsLinksUnifiedProfileIfEnabled(
105 /* isCommunityConsole = */ true);
106 }
107
Adrià Vilanova Martínezeebc0ac2022-01-05 14:45:53 +0100108 // #!if ['chromium', 'chromium_mv3'].includes(browser_target)
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200109 // Fix the drag&drop issue with the rich text editor if the option is
110 // currently enabled.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200111 //
112 // We target both tags because in different contexts different
113 // elements containing the text editor get added to the DOM structure.
114 // Sometimes it's a EC-MOVABLE-DIALOG which already contains the
115 // EC-RICH-TEXT-EDITOR, and sometimes it's the EC-RICH-TEXT-EDITOR
116 // directly.
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200117 if (('tagName' in node) &&
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200118 (node.tagName == 'EC-MOVABLE-DIALOG' ||
119 node.tagName == 'EC-RICH-TEXT-EDITOR')) {
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200120 applyDragAndDropFixIfEnabled(node);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200121 }
Adrià Vilanova Martínezeebc0ac2022-01-05 14:45:53 +0100122 // #!endif
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200123
Adrià Vilanova Martínez1e10d192021-12-31 16:01:13 +0100124 // Inject the batch lock and workflow buttons in the thread list if the
125 // corresponding options are currently enabled.
126 // The order is the inverse because the first one will be shown last.
Adrià Vilanova Martínez7e8796c2022-01-23 21:46:46 +0100127 if (batchLock.shouldAddButton(node)) batchLock.addButtonIfEnabled(node);
Adrià Vilanova Martínez1e10d192021-12-31 16:01:13 +0100128
129 if (workflows.shouldAddThreadListBtn(node))
130 workflows.addThreadListBtnIfEnabled(node);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200131
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200132 // Inject avatar links to threads in the thread list. injectIfEnabled is
133 // responsible of determining whether it should run or not depending on its
134 // current setting.
135 if (('tagName' in node) && (node.tagName == 'LI') &&
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200136 node.querySelector('ec-thread-summary') !== null) {
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200137 avatars.injectIfEnabled(node);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200138 }
139
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200140 // Set up the autorefresh list feature. The setUp function is responsible
141 // of determining whether it should run or not depending on the current
142 // setting.
143 if (('tagName' in node) && node.tagName == 'EC-THREAD-LIST') {
avm99963fd222672021-08-12 23:23:01 +0200144 window.TWPTAutoRefresh.setUp();
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200145 }
146
147 // Redirect unified profile iframe to dark version if applicable
148 if (node.tagName == 'IFRAME' && isDarkThemeOn(options) &&
149 unifiedProfilesFix.checkIframe(node)) {
150 unifiedProfilesFix.fixIframe(node);
151 }
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200152 }
153}
154
155function handleRemovedNode(node) {
156 // Remove snackbar when exiting thread list view
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200157 if ('tagName' in node && node.tagName == 'EC-THREAD-LIST') {
avm99963fd222672021-08-12 23:23:01 +0200158 window.TWPTAutoRefresh.hideUpdatePrompt();
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200159 }
160}
161
162function mutationCallback(mutationList, observer) {
163 mutationList.forEach((mutation) => {
164 if (mutation.type == 'childList') {
165 mutation.addedNodes.forEach(function(node) {
166 handleCandidateNode(node);
167 });
168
169 mutation.removedNodes.forEach(function(node) {
170 handleRemovedNode(node);
171 });
172 }
173 });
174}
175
176function intersectionCallback(entries, observer) {
177 entries.forEach(entry => {
178 if (entry.isIntersecting) {
179 entry.target.click();
180 }
181 });
182};
183
184var observerOptions = {
185 childList: true,
186 subtree: true,
187};
188
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200189getOptions(null).then(items => {
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200190 options = items;
191
Adrià Vilanova Martínez27c69962021-07-17 23:32:51 +0200192 // Initialize classes needed by the mutation observer
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200193 avatars = new AvatarsHandler();
Adrià Vilanova Martínez1e10d192021-12-31 16:01:13 +0100194 workflows = new Workflows();
Adrià Vilanova Martínez27c69962021-07-17 23:32:51 +0200195
avm99963b6f68b62021-08-12 23:13:06 +0200196 // autoRefresh is initialized in start.js
avm99963d3f4ac02021-08-12 18:36:58 +0200197
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200198 // Before starting the mutation Observer, check whether we missed any
199 // mutations by manually checking whether some watched nodes already
200 // exist.
201 var cssSelectors = watchedNodesSelectors.join(',');
202 document.querySelectorAll(cssSelectors)
203 .forEach(node => handleCandidateNode(node));
204
205 mutationObserver = new MutationObserver(mutationCallback);
206 mutationObserver.observe(document.body, observerOptions);
207
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200208 // TODO(avm99963): The following features are not dynamic. Make them be.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200209 if (options.fixedtoolbar) {
210 injectStyles(
211 'ec-bulk-actions{position: sticky; top: 0; background: var(--TWPT-primary-background, #fff); z-index: 96;}');
212 }
213
214 if (options.increasecontrast) {
215 injectStyles(
216 '.thread-summary.read:not(.checked){background: var(--TWPT-thread-read-background, #ecedee)!important;}');
217 }
218
219 if (options.stickysidebarheaders) {
220 injectStyles(
221 'material-drawer .main-header{background: var(--TWPT-drawer-background, #fff)!important; position: sticky; top: 0; z-index: 1;}');
222 }
223
224 if (options.enhancedannouncementsdot) {
225 injectStylesheet(
226 chrome.runtime.getURL('css/enhanced_announcements_dot.css'));
227 }
228
229 if (options.repositionexpandthread) {
Adrià Vilanova Martínez27c69962021-07-17 23:32:51 +0200230 injectStylesheet(chrome.runtime.getURL('css/reposition_expand_thread.css'));
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200231 }
232
Adrià Vilanova Martínez9d27c212021-12-05 13:54:10 +0100233 if (options.imagemaxheight) {
234 injectStylesheet(chrome.runtime.getURL('css/image_max_height.css'));
235 }
236
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200237 if (options.ccforcehidedrawer) {
238 var drawer = document.querySelector('material-drawer');
239 if (drawer !== null && drawer.classList.contains('mat-drawer-expanded')) {
240 document.querySelector('.material-drawer-button').click();
241 }
242 }
243
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200244 // Batch lock
245 injectScript(chrome.runtime.getURL('batchLockInject.bundle.js'));
246 injectStylesheet(chrome.runtime.getURL('css/batchlock_inject.css'));
247 // Thread list avatars
248 injectStylesheet(chrome.runtime.getURL('css/thread_list_avatars.css'));
249 // Auto refresh list
250 injectStylesheet(chrome.runtime.getURL('css/autorefresh_list.css'));
Adrià Vilanova Martínez7e8796c2022-01-23 21:46:46 +0100251 // Extra info
252 injectStylesheet(chrome.runtime.getURL('css/extrainfo.css'));
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200253});