blob: cc55aa4d03b56021bc170ba2c7e589f5c386c4c2 [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';
12
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020013var mutationObserver, intersectionObserver, intersectionOptions, options,
14 avatars;
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020015
16const watchedNodesSelectors = [
17 // App container (used to set up the intersection observer and inject the dark
18 // mode button)
19 'ec-app',
20
21 // Load more bar (for the "load more"/"load all" buttons)
22 '.load-more-bar',
23
Adrià Vilanova Martínez531cd072021-12-05 20:15:43 +010024 // Username span/editor inside ec-unified-user (user profile view)
Adrià Vilanova Martínez1f652522021-10-14 00:23:23 +020025 'ec-unified-user .scTailwindUser_profileUsercarddetails',
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020026
27 // Rich text editor
28 'ec-movable-dialog',
29 'ec-rich-text-editor',
30
31 // Read/unread bulk action in the list of thread, for the batch lock feature
32 'ec-bulk-actions material-button[debugid="mark-read-button"]',
33 'ec-bulk-actions material-button[debugid="mark-unread-button"]',
34
35 // Thread list items (used to inject the avatars)
36 'li',
37
38 // Thread list (used for the autorefresh feature)
39 'ec-thread-list',
40
41 // Unified profile iframe
42 'iframe',
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020043];
44
45function handleCandidateNode(node) {
46 if (typeof node.classList !== 'undefined') {
47 if (('tagName' in node) && node.tagName == 'EC-APP') {
48 // Set up the intersectionObserver
49 if (typeof intersectionObserver === 'undefined') {
50 var scrollableContent = node.querySelector('.scrollable-content');
51 if (scrollableContent !== null) {
52 intersectionOptions = {
53 root: scrollableContent,
54 rootMargin: '0px',
55 threshold: 1.0,
56 };
57
58 intersectionObserver = new IntersectionObserver(
59 intersectionCallback, intersectionOptions);
60 }
61 }
62
63 // Inject the dark mode button
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020064 // TODO(avm99963): make this feature dynamic.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020065 if (options.ccdarktheme && options.ccdarktheme_mode == 'switch') {
66 var rightControl = node.querySelector('header .right-control');
67 if (rightControl !== null)
68 injectDarkModeButton(rightControl, options.ccdarktheme_switch_status);
69 }
70 }
71
72 // Start the intersectionObserver for the "load more"/"load all" buttons
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020073 // inside a thread if the option is currently enabled.
74 if (node.classList.contains('load-more-bar')) {
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020075 if (typeof intersectionObserver !== 'undefined') {
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020076 getOptions(['thread', 'threadall']).then(threadOptions => {
77 if (threadOptions.thread)
78 intersectionObserver.observe(
79 node.querySelector('.load-more-button'));
80 if (threadOptions.threadall)
81 intersectionObserver.observe(
82 node.querySelector('.load-all-button'));
83 });
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020084 } else {
85 console.warn(
86 '[infinitescroll] ' +
87 'The intersectionObserver is not ready yet.');
88 }
89 }
90
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020091 // Show the "previous posts" links if the option is currently enabled.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020092 // Here we're selecting the 'ec-user > div' element (unique child)
Adrià Vilanova Martínez1f652522021-10-14 00:23:23 +020093 if (node.matches(
94 'ec-unified-user .scTailwindUser_profileUsercarddetails')) {
95 injectPreviousPostsLinksUnifiedProfileIfEnabled(
96 /* isCommunityConsole = */ true);
97 }
98
Adrià Vilanova Martínezeebc0ac2022-01-05 14:45:53 +010099 // #!if ['chromium', 'chromium_mv3'].includes(browser_target)
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200100 // Fix the drag&drop issue with the rich text editor if the option is
101 // currently enabled.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200102 //
103 // We target both tags because in different contexts different
104 // elements containing the text editor get added to the DOM structure.
105 // Sometimes it's a EC-MOVABLE-DIALOG which already contains the
106 // EC-RICH-TEXT-EDITOR, and sometimes it's the EC-RICH-TEXT-EDITOR
107 // directly.
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200108 if (('tagName' in node) &&
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200109 (node.tagName == 'EC-MOVABLE-DIALOG' ||
110 node.tagName == 'EC-RICH-TEXT-EDITOR')) {
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200111 applyDragAndDropFixIfEnabled(node);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200112 }
Adrià Vilanova Martínezeebc0ac2022-01-05 14:45:53 +0100113 // #!endif
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200114
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200115 // Inject the batch lock button in the thread list if the option is
116 // currently enabled.
117 if (batchLock.nodeIsReadToggleBtn(node)) {
118 batchLock.addButtonIfEnabled(node);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200119 }
120
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200121 // Inject avatar links to threads in the thread list. injectIfEnabled is
122 // responsible of determining whether it should run or not depending on its
123 // current setting.
124 if (('tagName' in node) && (node.tagName == 'LI') &&
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200125 node.querySelector('ec-thread-summary') !== null) {
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200126 avatars.injectIfEnabled(node);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200127 }
128
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200129 // Set up the autorefresh list feature. The setUp function is responsible
130 // of determining whether it should run or not depending on the current
131 // setting.
132 if (('tagName' in node) && node.tagName == 'EC-THREAD-LIST') {
avm99963fd222672021-08-12 23:23:01 +0200133 window.TWPTAutoRefresh.setUp();
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200134 }
135
136 // Redirect unified profile iframe to dark version if applicable
137 if (node.tagName == 'IFRAME' && isDarkThemeOn(options) &&
138 unifiedProfilesFix.checkIframe(node)) {
139 unifiedProfilesFix.fixIframe(node);
140 }
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200141 }
142}
143
144function handleRemovedNode(node) {
145 // Remove snackbar when exiting thread list view
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200146 if ('tagName' in node && node.tagName == 'EC-THREAD-LIST') {
avm99963fd222672021-08-12 23:23:01 +0200147 window.TWPTAutoRefresh.hideUpdatePrompt();
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200148 }
149}
150
151function mutationCallback(mutationList, observer) {
152 mutationList.forEach((mutation) => {
153 if (mutation.type == 'childList') {
154 mutation.addedNodes.forEach(function(node) {
155 handleCandidateNode(node);
156 });
157
158 mutation.removedNodes.forEach(function(node) {
159 handleRemovedNode(node);
160 });
161 }
162 });
163}
164
165function intersectionCallback(entries, observer) {
166 entries.forEach(entry => {
167 if (entry.isIntersecting) {
168 entry.target.click();
169 }
170 });
171};
172
173var observerOptions = {
174 childList: true,
175 subtree: true,
176};
177
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200178getOptions(null).then(items => {
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200179 options = items;
180
Adrià Vilanova Martínez27c69962021-07-17 23:32:51 +0200181 // Initialize classes needed by the mutation observer
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200182 avatars = new AvatarsHandler();
Adrià Vilanova Martínez27c69962021-07-17 23:32:51 +0200183
avm99963b6f68b62021-08-12 23:13:06 +0200184 // autoRefresh is initialized in start.js
avm99963d3f4ac02021-08-12 18:36:58 +0200185
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200186 // Before starting the mutation Observer, check whether we missed any
187 // mutations by manually checking whether some watched nodes already
188 // exist.
189 var cssSelectors = watchedNodesSelectors.join(',');
190 document.querySelectorAll(cssSelectors)
191 .forEach(node => handleCandidateNode(node));
192
193 mutationObserver = new MutationObserver(mutationCallback);
194 mutationObserver.observe(document.body, observerOptions);
195
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200196 // TODO(avm99963): The following features are not dynamic. Make them be.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200197 if (options.fixedtoolbar) {
198 injectStyles(
199 'ec-bulk-actions{position: sticky; top: 0; background: var(--TWPT-primary-background, #fff); z-index: 96;}');
200 }
201
202 if (options.increasecontrast) {
203 injectStyles(
204 '.thread-summary.read:not(.checked){background: var(--TWPT-thread-read-background, #ecedee)!important;}');
205 }
206
207 if (options.stickysidebarheaders) {
208 injectStyles(
209 'material-drawer .main-header{background: var(--TWPT-drawer-background, #fff)!important; position: sticky; top: 0; z-index: 1;}');
210 }
211
212 if (options.enhancedannouncementsdot) {
213 injectStylesheet(
214 chrome.runtime.getURL('css/enhanced_announcements_dot.css'));
215 }
216
217 if (options.repositionexpandthread) {
Adrià Vilanova Martínez27c69962021-07-17 23:32:51 +0200218 injectStylesheet(chrome.runtime.getURL('css/reposition_expand_thread.css'));
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200219 }
220
Adrià Vilanova Martínez9d27c212021-12-05 13:54:10 +0100221 if (options.imagemaxheight) {
222 injectStylesheet(chrome.runtime.getURL('css/image_max_height.css'));
223 }
224
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200225 if (options.ccforcehidedrawer) {
226 var drawer = document.querySelector('material-drawer');
227 if (drawer !== null && drawer.classList.contains('mat-drawer-expanded')) {
228 document.querySelector('.material-drawer-button').click();
229 }
230 }
231
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200232 // Batch lock
233 injectScript(chrome.runtime.getURL('batchLockInject.bundle.js'));
234 injectStylesheet(chrome.runtime.getURL('css/batchlock_inject.css'));
235 // Thread list avatars
236 injectStylesheet(chrome.runtime.getURL('css/thread_list_avatars.css'));
237 // Auto refresh list
238 injectStylesheet(chrome.runtime.getURL('css/autorefresh_list.css'));
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200239});