blob: d16a8638717076b4b6ac4f813b7ea22019c931f6 [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ínez3465e772021-07-11 19:18:41 +02003
Adrià Vilanova Martínez27c69962021-07-17 23:32:51 +02004import AvatarsHandler from './avatars.js';
Adrià Vilanova Martínez462280f2021-08-07 22:59:02 +02005import {batchLock} from './batchLock.js';
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +02006import {injectDarkModeButton, isDarkThemeOn} from './darkMode.js';
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +02007import {applyDragAndDropFixIfEnabled} from './dragAndDropFix.js';
8import {injectPreviousPostsLinksIfEnabled} from './profileHistoryLink.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
22 // Username span/editor inside ec-user (user profile view)
23 'ec-user .main-card .header > .name > span',
24 'ec-user .main-card .header > .name > ec-display-name-editor',
25
26 // Rich text editor
27 'ec-movable-dialog',
28 'ec-rich-text-editor',
29
30 // Read/unread bulk action in the list of thread, for the batch lock feature
31 'ec-bulk-actions material-button[debugid="mark-read-button"]',
32 'ec-bulk-actions material-button[debugid="mark-unread-button"]',
33
34 // Thread list items (used to inject the avatars)
35 'li',
36
37 // Thread list (used for the autorefresh feature)
38 'ec-thread-list',
39
40 // Unified profile iframe
41 'iframe',
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020042];
43
44function handleCandidateNode(node) {
45 if (typeof node.classList !== 'undefined') {
46 if (('tagName' in node) && node.tagName == 'EC-APP') {
47 // Set up the intersectionObserver
48 if (typeof intersectionObserver === 'undefined') {
49 var scrollableContent = node.querySelector('.scrollable-content');
50 if (scrollableContent !== null) {
51 intersectionOptions = {
52 root: scrollableContent,
53 rootMargin: '0px',
54 threshold: 1.0,
55 };
56
57 intersectionObserver = new IntersectionObserver(
58 intersectionCallback, intersectionOptions);
59 }
60 }
61
62 // Inject the dark mode button
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020063 // TODO(avm99963): make this feature dynamic.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020064 if (options.ccdarktheme && options.ccdarktheme_mode == 'switch') {
65 var rightControl = node.querySelector('header .right-control');
66 if (rightControl !== null)
67 injectDarkModeButton(rightControl, options.ccdarktheme_switch_status);
68 }
69 }
70
71 // Start the intersectionObserver for the "load more"/"load all" buttons
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020072 // inside a thread if the option is currently enabled.
73 if (node.classList.contains('load-more-bar')) {
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020074 if (typeof intersectionObserver !== 'undefined') {
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020075 getOptions(['thread', 'threadall']).then(threadOptions => {
76 if (threadOptions.thread)
77 intersectionObserver.observe(
78 node.querySelector('.load-more-button'));
79 if (threadOptions.threadall)
80 intersectionObserver.observe(
81 node.querySelector('.load-all-button'));
82 });
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020083 } else {
84 console.warn(
85 '[infinitescroll] ' +
86 'The intersectionObserver is not ready yet.');
87 }
88 }
89
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020090 // Show the "previous posts" links if the option is currently enabled.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020091 // Here we're selecting the 'ec-user > div' element (unique child)
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020092 if (node.matches('ec-user .main-card .header > .name > span') ||
93 node.matches(
94 'ec-user .main-card .header > .name > ec-display-name-editor')) {
95 injectPreviousPostsLinksIfEnabled(node);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020096 }
97
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020098 // Fix the drag&drop issue with the rich text editor if the option is
99 // currently enabled.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200100 //
101 // We target both tags because in different contexts different
102 // elements containing the text editor get added to the DOM structure.
103 // Sometimes it's a EC-MOVABLE-DIALOG which already contains the
104 // EC-RICH-TEXT-EDITOR, and sometimes it's the EC-RICH-TEXT-EDITOR
105 // directly.
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200106 if (('tagName' in node) &&
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200107 (node.tagName == 'EC-MOVABLE-DIALOG' ||
108 node.tagName == 'EC-RICH-TEXT-EDITOR')) {
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200109 applyDragAndDropFixIfEnabled(node);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200110 }
111
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200112 // Inject the batch lock button in the thread list if the option is
113 // currently enabled.
114 if (batchLock.nodeIsReadToggleBtn(node)) {
115 batchLock.addButtonIfEnabled(node);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200116 }
117
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200118 // Inject avatar links to threads in the thread list. injectIfEnabled is
119 // responsible of determining whether it should run or not depending on its
120 // current setting.
121 if (('tagName' in node) && (node.tagName == 'LI') &&
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200122 node.querySelector('ec-thread-summary') !== null) {
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200123 avatars.injectIfEnabled(node);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200124 }
125
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200126 // Set up the autorefresh list feature. The setUp function is responsible
127 // of determining whether it should run or not depending on the current
128 // setting.
129 if (('tagName' in node) && node.tagName == 'EC-THREAD-LIST') {
avm99963fd222672021-08-12 23:23:01 +0200130 window.TWPTAutoRefresh.setUp();
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200131 }
132
133 // Redirect unified profile iframe to dark version if applicable
134 if (node.tagName == 'IFRAME' && isDarkThemeOn(options) &&
135 unifiedProfilesFix.checkIframe(node)) {
136 unifiedProfilesFix.fixIframe(node);
137 }
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200138 }
139}
140
141function handleRemovedNode(node) {
142 // Remove snackbar when exiting thread list view
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200143 if ('tagName' in node && node.tagName == 'EC-THREAD-LIST') {
avm99963fd222672021-08-12 23:23:01 +0200144 window.TWPTAutoRefresh.hideUpdatePrompt();
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200145 }
146}
147
148function mutationCallback(mutationList, observer) {
149 mutationList.forEach((mutation) => {
150 if (mutation.type == 'childList') {
151 mutation.addedNodes.forEach(function(node) {
152 handleCandidateNode(node);
153 });
154
155 mutation.removedNodes.forEach(function(node) {
156 handleRemovedNode(node);
157 });
158 }
159 });
160}
161
162function intersectionCallback(entries, observer) {
163 entries.forEach(entry => {
164 if (entry.isIntersecting) {
165 entry.target.click();
166 }
167 });
168};
169
170var observerOptions = {
171 childList: true,
172 subtree: true,
173};
174
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200175getOptions(null).then(items => {
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200176 options = items;
177
Adrià Vilanova Martínez27c69962021-07-17 23:32:51 +0200178 // Initialize classes needed by the mutation observer
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200179 avatars = new AvatarsHandler();
Adrià Vilanova Martínez27c69962021-07-17 23:32:51 +0200180
avm99963b6f68b62021-08-12 23:13:06 +0200181 // autoRefresh is initialized in start.js
avm99963d3f4ac02021-08-12 18:36:58 +0200182
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200183 // Before starting the mutation Observer, check whether we missed any
184 // mutations by manually checking whether some watched nodes already
185 // exist.
186 var cssSelectors = watchedNodesSelectors.join(',');
187 document.querySelectorAll(cssSelectors)
188 .forEach(node => handleCandidateNode(node));
189
190 mutationObserver = new MutationObserver(mutationCallback);
191 mutationObserver.observe(document.body, observerOptions);
192
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200193 // TODO(avm99963): The following features are not dynamic. Make them be.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200194 if (options.fixedtoolbar) {
195 injectStyles(
196 'ec-bulk-actions{position: sticky; top: 0; background: var(--TWPT-primary-background, #fff); z-index: 96;}');
197 }
198
199 if (options.increasecontrast) {
200 injectStyles(
201 '.thread-summary.read:not(.checked){background: var(--TWPT-thread-read-background, #ecedee)!important;}');
202 }
203
204 if (options.stickysidebarheaders) {
205 injectStyles(
206 'material-drawer .main-header{background: var(--TWPT-drawer-background, #fff)!important; position: sticky; top: 0; z-index: 1;}');
207 }
208
209 if (options.enhancedannouncementsdot) {
210 injectStylesheet(
211 chrome.runtime.getURL('css/enhanced_announcements_dot.css'));
212 }
213
214 if (options.repositionexpandthread) {
Adrià Vilanova Martínez27c69962021-07-17 23:32:51 +0200215 injectStylesheet(chrome.runtime.getURL('css/reposition_expand_thread.css'));
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200216 }
217
218 if (options.ccforcehidedrawer) {
219 var drawer = document.querySelector('material-drawer');
220 if (drawer !== null && drawer.classList.contains('mat-drawer-expanded')) {
221 document.querySelector('.material-drawer-button').click();
222 }
223 }
224
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200225 // Batch lock
226 injectScript(chrome.runtime.getURL('batchLockInject.bundle.js'));
227 injectStylesheet(chrome.runtime.getURL('css/batchlock_inject.css'));
228 // Thread list avatars
229 injectStylesheet(chrome.runtime.getURL('css/thread_list_avatars.css'));
230 // Auto refresh list
231 injectStylesheet(chrome.runtime.getURL('css/autorefresh_list.css'));
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200232});