blob: 73ed360d72f11f43dc2e0466b55ad6c28947ee12 [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ínez74a25202022-01-23 23:36:58 +010047
48 // Canned response tags or toolbelt (for the extra info feature)
49 '.tags',
50 '.toolbelt',
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020051];
52
53function handleCandidateNode(node) {
54 if (typeof node.classList !== 'undefined') {
55 if (('tagName' in node) && node.tagName == 'EC-APP') {
56 // Set up the intersectionObserver
57 if (typeof intersectionObserver === 'undefined') {
58 var scrollableContent = node.querySelector('.scrollable-content');
59 if (scrollableContent !== null) {
60 intersectionOptions = {
61 root: scrollableContent,
62 rootMargin: '0px',
63 threshold: 1.0,
64 };
65
66 intersectionObserver = new IntersectionObserver(
67 intersectionCallback, intersectionOptions);
68 }
69 }
70
71 // Inject the dark mode button
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020072 // TODO(avm99963): make this feature dynamic.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020073 if (options.ccdarktheme && options.ccdarktheme_mode == 'switch') {
74 var rightControl = node.querySelector('header .right-control');
75 if (rightControl !== null)
76 injectDarkModeButton(rightControl, options.ccdarktheme_switch_status);
77 }
78 }
79
80 // Start the intersectionObserver for the "load more"/"load all" buttons
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020081 // inside a thread if the option is currently enabled.
82 if (node.classList.contains('load-more-bar')) {
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020083 if (typeof intersectionObserver !== 'undefined') {
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020084 getOptions(['thread', 'threadall']).then(threadOptions => {
85 if (threadOptions.thread)
86 intersectionObserver.observe(
87 node.querySelector('.load-more-button'));
88 if (threadOptions.threadall)
89 intersectionObserver.observe(
90 node.querySelector('.load-all-button'));
91 });
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020092 } else {
93 console.warn(
94 '[infinitescroll] ' +
95 'The intersectionObserver is not ready yet.');
96 }
97 }
98
Adrià Vilanova Martínez7e8796c2022-01-23 21:46:46 +010099 // Show additional details in the profile view.
100 if (node.matches('ec-unified-user .scTailwindUser_profileUsercardmain')) {
101 window.TWPTExtraInfo.injectAtProfileIfEnabled(node);
102 }
103
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200104 // Show the "previous posts" links if the option is currently enabled.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200105 // Here we're selecting the 'ec-user > div' element (unique child)
Adrià Vilanova Martínez1f652522021-10-14 00:23:23 +0200106 if (node.matches(
107 'ec-unified-user .scTailwindUser_profileUsercarddetails')) {
108 injectPreviousPostsLinksUnifiedProfileIfEnabled(
109 /* isCommunityConsole = */ true);
110 }
111
Adrià Vilanova Martínezeebc0ac2022-01-05 14:45:53 +0100112 // #!if ['chromium', 'chromium_mv3'].includes(browser_target)
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200113 // Fix the drag&drop issue with the rich text editor if the option is
114 // currently enabled.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200115 //
116 // We target both tags because in different contexts different
117 // elements containing the text editor get added to the DOM structure.
118 // Sometimes it's a EC-MOVABLE-DIALOG which already contains the
119 // EC-RICH-TEXT-EDITOR, and sometimes it's the EC-RICH-TEXT-EDITOR
120 // directly.
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200121 if (('tagName' in node) &&
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200122 (node.tagName == 'EC-MOVABLE-DIALOG' ||
123 node.tagName == 'EC-RICH-TEXT-EDITOR')) {
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200124 applyDragAndDropFixIfEnabled(node);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200125 }
Adrià Vilanova Martínezeebc0ac2022-01-05 14:45:53 +0100126 // #!endif
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200127
Adrià Vilanova Martínez1e10d192021-12-31 16:01:13 +0100128 // Inject the batch lock and workflow buttons in the thread list if the
129 // corresponding options are currently enabled.
130 // The order is the inverse because the first one will be shown last.
Adrià Vilanova Martínez7e8796c2022-01-23 21:46:46 +0100131 if (batchLock.shouldAddButton(node)) batchLock.addButtonIfEnabled(node);
Adrià Vilanova Martínez1e10d192021-12-31 16:01:13 +0100132
133 if (workflows.shouldAddThreadListBtn(node))
134 workflows.addThreadListBtnIfEnabled(node);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200135
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200136 // Inject avatar links to threads in the thread list. injectIfEnabled is
137 // responsible of determining whether it should run or not depending on its
138 // current setting.
139 if (('tagName' in node) && (node.tagName == 'LI') &&
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200140 node.querySelector('ec-thread-summary') !== null) {
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200141 avatars.injectIfEnabled(node);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200142 }
143
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200144 // Set up the autorefresh list feature. The setUp function is responsible
145 // of determining whether it should run or not depending on the current
146 // setting.
147 if (('tagName' in node) && node.tagName == 'EC-THREAD-LIST') {
avm99963fd222672021-08-12 23:23:01 +0200148 window.TWPTAutoRefresh.setUp();
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200149 }
150
151 // Redirect unified profile iframe to dark version if applicable
152 if (node.tagName == 'IFRAME' && isDarkThemeOn(options) &&
153 unifiedProfilesFix.checkIframe(node)) {
154 unifiedProfilesFix.fixIframe(node);
155 }
Adrià Vilanova Martínez74a25202022-01-23 23:36:58 +0100156
157 // Show additional details in the canned responses view.
158 if (node.matches('ec-canned-response-row .tags')) {
159 window.TWPTExtraInfo.injectAtCRIfEnabled(node, /* isExpanded = */ false);
160 }
161 if (node.matches('ec-canned-response-row .main .toolbelt')) {
162 const tags = node.parentNode?.querySelector?.('.tags');
163 if (tags) window.TWPTExtraInfo.injectAtCRIfEnabled(tags, /* isExpanded = */ true);
164 }
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200165 }
166}
167
168function handleRemovedNode(node) {
169 // Remove snackbar when exiting thread list view
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200170 if ('tagName' in node && node.tagName == 'EC-THREAD-LIST') {
avm99963fd222672021-08-12 23:23:01 +0200171 window.TWPTAutoRefresh.hideUpdatePrompt();
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200172 }
173}
174
175function mutationCallback(mutationList, observer) {
176 mutationList.forEach((mutation) => {
177 if (mutation.type == 'childList') {
178 mutation.addedNodes.forEach(function(node) {
179 handleCandidateNode(node);
180 });
181
182 mutation.removedNodes.forEach(function(node) {
183 handleRemovedNode(node);
184 });
185 }
186 });
187}
188
189function intersectionCallback(entries, observer) {
190 entries.forEach(entry => {
191 if (entry.isIntersecting) {
192 entry.target.click();
193 }
194 });
195};
196
197var observerOptions = {
198 childList: true,
199 subtree: true,
200};
201
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200202getOptions(null).then(items => {
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200203 options = items;
204
Adrià Vilanova Martínez27c69962021-07-17 23:32:51 +0200205 // Initialize classes needed by the mutation observer
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200206 avatars = new AvatarsHandler();
Adrià Vilanova Martínez1e10d192021-12-31 16:01:13 +0100207 workflows = new Workflows();
Adrià Vilanova Martínez27c69962021-07-17 23:32:51 +0200208
avm99963b6f68b62021-08-12 23:13:06 +0200209 // autoRefresh is initialized in start.js
avm99963d3f4ac02021-08-12 18:36:58 +0200210
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200211 // Before starting the mutation Observer, check whether we missed any
212 // mutations by manually checking whether some watched nodes already
213 // exist.
214 var cssSelectors = watchedNodesSelectors.join(',');
215 document.querySelectorAll(cssSelectors)
216 .forEach(node => handleCandidateNode(node));
217
218 mutationObserver = new MutationObserver(mutationCallback);
219 mutationObserver.observe(document.body, observerOptions);
220
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200221 // TODO(avm99963): The following features are not dynamic. Make them be.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200222 if (options.fixedtoolbar) {
223 injectStyles(
224 'ec-bulk-actions{position: sticky; top: 0; background: var(--TWPT-primary-background, #fff); z-index: 96;}');
225 }
226
227 if (options.increasecontrast) {
228 injectStyles(
229 '.thread-summary.read:not(.checked){background: var(--TWPT-thread-read-background, #ecedee)!important;}');
230 }
231
232 if (options.stickysidebarheaders) {
233 injectStyles(
234 'material-drawer .main-header{background: var(--TWPT-drawer-background, #fff)!important; position: sticky; top: 0; z-index: 1;}');
235 }
236
237 if (options.enhancedannouncementsdot) {
238 injectStylesheet(
239 chrome.runtime.getURL('css/enhanced_announcements_dot.css'));
240 }
241
242 if (options.repositionexpandthread) {
Adrià Vilanova Martínez27c69962021-07-17 23:32:51 +0200243 injectStylesheet(chrome.runtime.getURL('css/reposition_expand_thread.css'));
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200244 }
245
Adrià Vilanova Martínez9d27c212021-12-05 13:54:10 +0100246 if (options.imagemaxheight) {
247 injectStylesheet(chrome.runtime.getURL('css/image_max_height.css'));
248 }
249
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200250 if (options.ccforcehidedrawer) {
251 var drawer = document.querySelector('material-drawer');
252 if (drawer !== null && drawer.classList.contains('mat-drawer-expanded')) {
253 document.querySelector('.material-drawer-button').click();
254 }
255 }
256
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200257 // Batch lock
258 injectScript(chrome.runtime.getURL('batchLockInject.bundle.js'));
259 injectStylesheet(chrome.runtime.getURL('css/batchlock_inject.css'));
260 // Thread list avatars
261 injectStylesheet(chrome.runtime.getURL('css/thread_list_avatars.css'));
262 // Auto refresh list
263 injectStylesheet(chrome.runtime.getURL('css/autorefresh_list.css'));
Adrià Vilanova Martínez7e8796c2022-01-23 21:46:46 +0100264 // Extra info
265 injectStylesheet(chrome.runtime.getURL('css/extrainfo.css'));
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200266});