blob: fe075d90dd68834fac74037054ff1da0255092e4 [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ínezd269c622021-09-04 18:35:55 +02008import {applyDragAndDropFixIfEnabled} from './dragAndDropFix.js';
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +02009import {unifiedProfilesFix} from './unifiedProfiles.js';
10
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020011var mutationObserver, intersectionObserver, intersectionOptions, options,
12 avatars;
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020013
14const watchedNodesSelectors = [
15 // App container (used to set up the intersection observer and inject the dark
16 // mode button)
17 'ec-app',
18
19 // Load more bar (for the "load more"/"load all" buttons)
20 '.load-more-bar',
21
Adrià Vilanova Martínez531cd072021-12-05 20:15:43 +010022 // Username span/editor inside ec-unified-user (user profile view)
Adrià Vilanova Martínez1f652522021-10-14 00:23:23 +020023 'ec-unified-user .scTailwindUser_profileUsercarddetails',
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020024
25 // Rich text editor
26 'ec-movable-dialog',
27 'ec-rich-text-editor',
28
29 // Read/unread bulk action in the list of thread, for the batch lock feature
30 'ec-bulk-actions material-button[debugid="mark-read-button"]',
31 'ec-bulk-actions material-button[debugid="mark-unread-button"]',
32
33 // Thread list items (used to inject the avatars)
34 'li',
35
36 // Thread list (used for the autorefresh feature)
37 'ec-thread-list',
38
39 // Unified profile iframe
40 'iframe',
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020041];
42
43function handleCandidateNode(node) {
44 if (typeof node.classList !== 'undefined') {
45 if (('tagName' in node) && node.tagName == 'EC-APP') {
46 // Set up the intersectionObserver
47 if (typeof intersectionObserver === 'undefined') {
48 var scrollableContent = node.querySelector('.scrollable-content');
49 if (scrollableContent !== null) {
50 intersectionOptions = {
51 root: scrollableContent,
52 rootMargin: '0px',
53 threshold: 1.0,
54 };
55
56 intersectionObserver = new IntersectionObserver(
57 intersectionCallback, intersectionOptions);
58 }
59 }
60
61 // Inject the dark mode button
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020062 // TODO(avm99963): make this feature dynamic.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020063 if (options.ccdarktheme && options.ccdarktheme_mode == 'switch') {
64 var rightControl = node.querySelector('header .right-control');
65 if (rightControl !== null)
66 injectDarkModeButton(rightControl, options.ccdarktheme_switch_status);
67 }
68 }
69
70 // Start the intersectionObserver for the "load more"/"load all" buttons
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020071 // inside a thread if the option is currently enabled.
72 if (node.classList.contains('load-more-bar')) {
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020073 if (typeof intersectionObserver !== 'undefined') {
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020074 getOptions(['thread', 'threadall']).then(threadOptions => {
75 if (threadOptions.thread)
76 intersectionObserver.observe(
77 node.querySelector('.load-more-button'));
78 if (threadOptions.threadall)
79 intersectionObserver.observe(
80 node.querySelector('.load-all-button'));
81 });
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020082 } else {
83 console.warn(
84 '[infinitescroll] ' +
85 'The intersectionObserver is not ready yet.');
86 }
87 }
88
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020089 // Show the "previous posts" links if the option is currently enabled.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020090 // Here we're selecting the 'ec-user > div' element (unique child)
Adrià Vilanova Martínez1f652522021-10-14 00:23:23 +020091 if (node.matches(
92 'ec-unified-user .scTailwindUser_profileUsercarddetails')) {
93 injectPreviousPostsLinksUnifiedProfileIfEnabled(
94 /* isCommunityConsole = */ true);
95 }
96
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020097 // Fix the drag&drop issue with the rich text editor if the option is
98 // currently enabled.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020099 //
100 // We target both tags because in different contexts different
101 // elements containing the text editor get added to the DOM structure.
102 // Sometimes it's a EC-MOVABLE-DIALOG which already contains the
103 // EC-RICH-TEXT-EDITOR, and sometimes it's the EC-RICH-TEXT-EDITOR
104 // directly.
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200105 if (('tagName' in node) &&
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200106 (node.tagName == 'EC-MOVABLE-DIALOG' ||
107 node.tagName == 'EC-RICH-TEXT-EDITOR')) {
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200108 applyDragAndDropFixIfEnabled(node);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200109 }
110
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200111 // Inject the batch lock button in the thread list if the option is
112 // currently enabled.
113 if (batchLock.nodeIsReadToggleBtn(node)) {
114 batchLock.addButtonIfEnabled(node);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200115 }
116
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200117 // Inject avatar links to threads in the thread list. injectIfEnabled is
118 // responsible of determining whether it should run or not depending on its
119 // current setting.
120 if (('tagName' in node) && (node.tagName == 'LI') &&
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200121 node.querySelector('ec-thread-summary') !== null) {
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200122 avatars.injectIfEnabled(node);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200123 }
124
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200125 // Set up the autorefresh list feature. The setUp function is responsible
126 // of determining whether it should run or not depending on the current
127 // setting.
128 if (('tagName' in node) && node.tagName == 'EC-THREAD-LIST') {
avm99963fd222672021-08-12 23:23:01 +0200129 window.TWPTAutoRefresh.setUp();
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200130 }
131
132 // Redirect unified profile iframe to dark version if applicable
133 if (node.tagName == 'IFRAME' && isDarkThemeOn(options) &&
134 unifiedProfilesFix.checkIframe(node)) {
135 unifiedProfilesFix.fixIframe(node);
136 }
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200137 }
138}
139
140function handleRemovedNode(node) {
141 // Remove snackbar when exiting thread list view
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200142 if ('tagName' in node && node.tagName == 'EC-THREAD-LIST') {
avm99963fd222672021-08-12 23:23:01 +0200143 window.TWPTAutoRefresh.hideUpdatePrompt();
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200144 }
145}
146
147function mutationCallback(mutationList, observer) {
148 mutationList.forEach((mutation) => {
149 if (mutation.type == 'childList') {
150 mutation.addedNodes.forEach(function(node) {
151 handleCandidateNode(node);
152 });
153
154 mutation.removedNodes.forEach(function(node) {
155 handleRemovedNode(node);
156 });
157 }
158 });
159}
160
161function intersectionCallback(entries, observer) {
162 entries.forEach(entry => {
163 if (entry.isIntersecting) {
164 entry.target.click();
165 }
166 });
167};
168
169var observerOptions = {
170 childList: true,
171 subtree: true,
172};
173
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200174getOptions(null).then(items => {
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200175 options = items;
176
Adrià Vilanova Martínez27c69962021-07-17 23:32:51 +0200177 // Initialize classes needed by the mutation observer
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200178 avatars = new AvatarsHandler();
Adrià Vilanova Martínez27c69962021-07-17 23:32:51 +0200179
avm99963b6f68b62021-08-12 23:13:06 +0200180 // autoRefresh is initialized in start.js
avm99963d3f4ac02021-08-12 18:36:58 +0200181
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200182 // Before starting the mutation Observer, check whether we missed any
183 // mutations by manually checking whether some watched nodes already
184 // exist.
185 var cssSelectors = watchedNodesSelectors.join(',');
186 document.querySelectorAll(cssSelectors)
187 .forEach(node => handleCandidateNode(node));
188
189 mutationObserver = new MutationObserver(mutationCallback);
190 mutationObserver.observe(document.body, observerOptions);
191
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200192 // TODO(avm99963): The following features are not dynamic. Make them be.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200193 if (options.fixedtoolbar) {
194 injectStyles(
195 'ec-bulk-actions{position: sticky; top: 0; background: var(--TWPT-primary-background, #fff); z-index: 96;}');
196 }
197
198 if (options.increasecontrast) {
199 injectStyles(
200 '.thread-summary.read:not(.checked){background: var(--TWPT-thread-read-background, #ecedee)!important;}');
201 }
202
203 if (options.stickysidebarheaders) {
204 injectStyles(
205 'material-drawer .main-header{background: var(--TWPT-drawer-background, #fff)!important; position: sticky; top: 0; z-index: 1;}');
206 }
207
208 if (options.enhancedannouncementsdot) {
209 injectStylesheet(
210 chrome.runtime.getURL('css/enhanced_announcements_dot.css'));
211 }
212
213 if (options.repositionexpandthread) {
Adrià Vilanova Martínez27c69962021-07-17 23:32:51 +0200214 injectStylesheet(chrome.runtime.getURL('css/reposition_expand_thread.css'));
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200215 }
216
Adrià Vilanova Martínez9d27c212021-12-05 13:54:10 +0100217 if (options.imagemaxheight) {
218 injectStylesheet(chrome.runtime.getURL('css/image_max_height.css'));
219 }
220
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200221 if (options.ccforcehidedrawer) {
222 var drawer = document.querySelector('material-drawer');
223 if (drawer !== null && drawer.classList.contains('mat-drawer-expanded')) {
224 document.querySelector('.material-drawer-button').click();
225 }
226 }
227
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200228 // Batch lock
229 injectScript(chrome.runtime.getURL('batchLockInject.bundle.js'));
230 injectStylesheet(chrome.runtime.getURL('css/batchlock_inject.css'));
231 // Thread list avatars
232 injectStylesheet(chrome.runtime.getURL('css/thread_list_avatars.css'));
233 // Auto refresh list
234 injectStylesheet(chrome.runtime.getURL('css/autorefresh_list.css'));
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200235});