blob: 870f1c4cae1c7d3f19b7be28cb7093b56be40679 [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ínez531cd072021-12-05 20:15:43 +010025 // Username span/editor inside ec-unified-user (user profile view)
Adrià Vilanova Martínez1f652522021-10-14 00:23:23 +020026 'ec-unified-user .scTailwindUser_profileUsercarddetails',
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020027
28 // Rich text editor
29 'ec-movable-dialog',
30 'ec-rich-text-editor',
31
32 // Read/unread bulk action in the list of thread, for the batch lock feature
33 'ec-bulk-actions material-button[debugid="mark-read-button"]',
34 'ec-bulk-actions material-button[debugid="mark-unread-button"]',
35
36 // Thread list items (used to inject the avatars)
37 'li',
38
39 // Thread list (used for the autorefresh feature)
40 'ec-thread-list',
41
42 // Unified profile iframe
43 'iframe',
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020044];
45
46function handleCandidateNode(node) {
47 if (typeof node.classList !== 'undefined') {
48 if (('tagName' in node) && node.tagName == 'EC-APP') {
49 // Set up the intersectionObserver
50 if (typeof intersectionObserver === 'undefined') {
51 var scrollableContent = node.querySelector('.scrollable-content');
52 if (scrollableContent !== null) {
53 intersectionOptions = {
54 root: scrollableContent,
55 rootMargin: '0px',
56 threshold: 1.0,
57 };
58
59 intersectionObserver = new IntersectionObserver(
60 intersectionCallback, intersectionOptions);
61 }
62 }
63
64 // Inject the dark mode button
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020065 // TODO(avm99963): make this feature dynamic.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020066 if (options.ccdarktheme && options.ccdarktheme_mode == 'switch') {
67 var rightControl = node.querySelector('header .right-control');
68 if (rightControl !== null)
69 injectDarkModeButton(rightControl, options.ccdarktheme_switch_status);
70 }
71 }
72
73 // Start the intersectionObserver for the "load more"/"load all" buttons
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020074 // inside a thread if the option is currently enabled.
75 if (node.classList.contains('load-more-bar')) {
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020076 if (typeof intersectionObserver !== 'undefined') {
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020077 getOptions(['thread', 'threadall']).then(threadOptions => {
78 if (threadOptions.thread)
79 intersectionObserver.observe(
80 node.querySelector('.load-more-button'));
81 if (threadOptions.threadall)
82 intersectionObserver.observe(
83 node.querySelector('.load-all-button'));
84 });
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020085 } else {
86 console.warn(
87 '[infinitescroll] ' +
88 'The intersectionObserver is not ready yet.');
89 }
90 }
91
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020092 // Show the "previous posts" links if the option is currently enabled.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020093 // Here we're selecting the 'ec-user > div' element (unique child)
Adrià Vilanova Martínez1f652522021-10-14 00:23:23 +020094 if (node.matches(
95 'ec-unified-user .scTailwindUser_profileUsercarddetails')) {
96 injectPreviousPostsLinksUnifiedProfileIfEnabled(
97 /* isCommunityConsole = */ true);
98 }
99
Adrià Vilanova Martínezeebc0ac2022-01-05 14:45:53 +0100100 // #!if ['chromium', 'chromium_mv3'].includes(browser_target)
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200101 // Fix the drag&drop issue with the rich text editor if the option is
102 // currently enabled.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200103 //
104 // We target both tags because in different contexts different
105 // elements containing the text editor get added to the DOM structure.
106 // Sometimes it's a EC-MOVABLE-DIALOG which already contains the
107 // EC-RICH-TEXT-EDITOR, and sometimes it's the EC-RICH-TEXT-EDITOR
108 // directly.
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200109 if (('tagName' in node) &&
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200110 (node.tagName == 'EC-MOVABLE-DIALOG' ||
111 node.tagName == 'EC-RICH-TEXT-EDITOR')) {
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200112 applyDragAndDropFixIfEnabled(node);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200113 }
Adrià Vilanova Martínezeebc0ac2022-01-05 14:45:53 +0100114 // #!endif
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200115
Adrià Vilanova Martínez1e10d192021-12-31 16:01:13 +0100116 // Inject the batch lock and workflow buttons in the thread list if the
117 // corresponding options are currently enabled.
118 // The order is the inverse because the first one will be shown last.
119 if (batchLock.shouldAddButton(node))
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200120 batchLock.addButtonIfEnabled(node);
Adrià Vilanova Martínez1e10d192021-12-31 16:01:13 +0100121
122 if (workflows.shouldAddThreadListBtn(node))
123 workflows.addThreadListBtnIfEnabled(node);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200124
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200125 // Inject avatar links to threads in the thread list. injectIfEnabled is
126 // responsible of determining whether it should run or not depending on its
127 // current setting.
128 if (('tagName' in node) && (node.tagName == 'LI') &&
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200129 node.querySelector('ec-thread-summary') !== null) {
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200130 avatars.injectIfEnabled(node);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200131 }
132
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200133 // Set up the autorefresh list feature. The setUp function is responsible
134 // of determining whether it should run or not depending on the current
135 // setting.
136 if (('tagName' in node) && node.tagName == 'EC-THREAD-LIST') {
avm99963fd222672021-08-12 23:23:01 +0200137 window.TWPTAutoRefresh.setUp();
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200138 }
139
140 // Redirect unified profile iframe to dark version if applicable
141 if (node.tagName == 'IFRAME' && isDarkThemeOn(options) &&
142 unifiedProfilesFix.checkIframe(node)) {
143 unifiedProfilesFix.fixIframe(node);
144 }
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200145 }
146}
147
148function handleRemovedNode(node) {
149 // Remove snackbar when exiting thread list view
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200150 if ('tagName' in node && node.tagName == 'EC-THREAD-LIST') {
avm99963fd222672021-08-12 23:23:01 +0200151 window.TWPTAutoRefresh.hideUpdatePrompt();
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200152 }
153}
154
155function mutationCallback(mutationList, observer) {
156 mutationList.forEach((mutation) => {
157 if (mutation.type == 'childList') {
158 mutation.addedNodes.forEach(function(node) {
159 handleCandidateNode(node);
160 });
161
162 mutation.removedNodes.forEach(function(node) {
163 handleRemovedNode(node);
164 });
165 }
166 });
167}
168
169function intersectionCallback(entries, observer) {
170 entries.forEach(entry => {
171 if (entry.isIntersecting) {
172 entry.target.click();
173 }
174 });
175};
176
177var observerOptions = {
178 childList: true,
179 subtree: true,
180};
181
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200182getOptions(null).then(items => {
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200183 options = items;
184
Adrià Vilanova Martínez27c69962021-07-17 23:32:51 +0200185 // Initialize classes needed by the mutation observer
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200186 avatars = new AvatarsHandler();
Adrià Vilanova Martínez1e10d192021-12-31 16:01:13 +0100187 workflows = new Workflows();
Adrià Vilanova Martínez27c69962021-07-17 23:32:51 +0200188
avm99963b6f68b62021-08-12 23:13:06 +0200189 // autoRefresh is initialized in start.js
avm99963d3f4ac02021-08-12 18:36:58 +0200190
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200191 // Before starting the mutation Observer, check whether we missed any
192 // mutations by manually checking whether some watched nodes already
193 // exist.
194 var cssSelectors = watchedNodesSelectors.join(',');
195 document.querySelectorAll(cssSelectors)
196 .forEach(node => handleCandidateNode(node));
197
198 mutationObserver = new MutationObserver(mutationCallback);
199 mutationObserver.observe(document.body, observerOptions);
200
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200201 // TODO(avm99963): The following features are not dynamic. Make them be.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200202 if (options.fixedtoolbar) {
203 injectStyles(
204 'ec-bulk-actions{position: sticky; top: 0; background: var(--TWPT-primary-background, #fff); z-index: 96;}');
205 }
206
207 if (options.increasecontrast) {
208 injectStyles(
209 '.thread-summary.read:not(.checked){background: var(--TWPT-thread-read-background, #ecedee)!important;}');
210 }
211
212 if (options.stickysidebarheaders) {
213 injectStyles(
214 'material-drawer .main-header{background: var(--TWPT-drawer-background, #fff)!important; position: sticky; top: 0; z-index: 1;}');
215 }
216
217 if (options.enhancedannouncementsdot) {
218 injectStylesheet(
219 chrome.runtime.getURL('css/enhanced_announcements_dot.css'));
220 }
221
222 if (options.repositionexpandthread) {
Adrià Vilanova Martínez27c69962021-07-17 23:32:51 +0200223 injectStylesheet(chrome.runtime.getURL('css/reposition_expand_thread.css'));
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200224 }
225
Adrià Vilanova Martínez9d27c212021-12-05 13:54:10 +0100226 if (options.imagemaxheight) {
227 injectStylesheet(chrome.runtime.getURL('css/image_max_height.css'));
228 }
229
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200230 if (options.ccforcehidedrawer) {
231 var drawer = document.querySelector('material-drawer');
232 if (drawer !== null && drawer.classList.contains('mat-drawer-expanded')) {
233 document.querySelector('.material-drawer-button').click();
234 }
235 }
236
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200237 // Batch lock
238 injectScript(chrome.runtime.getURL('batchLockInject.bundle.js'));
239 injectStylesheet(chrome.runtime.getURL('css/batchlock_inject.css'));
240 // Thread list avatars
241 injectStylesheet(chrome.runtime.getURL('css/thread_list_avatars.css'));
242 // Auto refresh list
243 injectStylesheet(chrome.runtime.getURL('css/autorefresh_list.css'));
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200244});