blob: b0d9dbcd188a99ce47484d5a6a9a946209b69777 [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)
Adrià Vilanova Martínez6e4a68d2022-01-24 21:44:32 +010049 'ec-canned-response-row .tags',
50 'ec-canned-response-row .main .toolbelt',
51
52 // Div containing ec-question (for the extra info feature)
53 'ec-thread div[role="list"]',
54
55 // Replies (for the extra info feature)
56 'ec-thread ec-message',
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020057];
58
59function handleCandidateNode(node) {
60 if (typeof node.classList !== 'undefined') {
61 if (('tagName' in node) && node.tagName == 'EC-APP') {
62 // Set up the intersectionObserver
63 if (typeof intersectionObserver === 'undefined') {
64 var scrollableContent = node.querySelector('.scrollable-content');
65 if (scrollableContent !== null) {
66 intersectionOptions = {
67 root: scrollableContent,
68 rootMargin: '0px',
69 threshold: 1.0,
70 };
71
72 intersectionObserver = new IntersectionObserver(
73 intersectionCallback, intersectionOptions);
74 }
75 }
76
77 // Inject the dark mode button
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020078 // TODO(avm99963): make this feature dynamic.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020079 if (options.ccdarktheme && options.ccdarktheme_mode == 'switch') {
80 var rightControl = node.querySelector('header .right-control');
81 if (rightControl !== null)
82 injectDarkModeButton(rightControl, options.ccdarktheme_switch_status);
83 }
84 }
85
86 // Start the intersectionObserver for the "load more"/"load all" buttons
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020087 // inside a thread if the option is currently enabled.
88 if (node.classList.contains('load-more-bar')) {
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020089 if (typeof intersectionObserver !== 'undefined') {
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020090 getOptions(['thread', 'threadall']).then(threadOptions => {
91 if (threadOptions.thread)
92 intersectionObserver.observe(
93 node.querySelector('.load-more-button'));
94 if (threadOptions.threadall)
95 intersectionObserver.observe(
96 node.querySelector('.load-all-button'));
97 });
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020098 } else {
99 console.warn(
100 '[infinitescroll] ' +
101 'The intersectionObserver is not ready yet.');
102 }
103 }
104
Adrià Vilanova Martínez7e8796c2022-01-23 21:46:46 +0100105 // Show additional details in the profile view.
106 if (node.matches('ec-unified-user .scTailwindUser_profileUsercardmain')) {
107 window.TWPTExtraInfo.injectAtProfileIfEnabled(node);
108 }
109
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200110 // Show the "previous posts" links if the option is currently enabled.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200111 // Here we're selecting the 'ec-user > div' element (unique child)
Adrià Vilanova Martínez1f652522021-10-14 00:23:23 +0200112 if (node.matches(
113 'ec-unified-user .scTailwindUser_profileUsercarddetails')) {
114 injectPreviousPostsLinksUnifiedProfileIfEnabled(
115 /* isCommunityConsole = */ true);
116 }
117
Adrià Vilanova Martínezeebc0ac2022-01-05 14:45:53 +0100118 // #!if ['chromium', 'chromium_mv3'].includes(browser_target)
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200119 // Fix the drag&drop issue with the rich text editor if the option is
120 // currently enabled.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200121 //
122 // We target both tags because in different contexts different
123 // elements containing the text editor get added to the DOM structure.
124 // Sometimes it's a EC-MOVABLE-DIALOG which already contains the
125 // EC-RICH-TEXT-EDITOR, and sometimes it's the EC-RICH-TEXT-EDITOR
126 // directly.
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200127 if (('tagName' in node) &&
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200128 (node.tagName == 'EC-MOVABLE-DIALOG' ||
129 node.tagName == 'EC-RICH-TEXT-EDITOR')) {
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200130 applyDragAndDropFixIfEnabled(node);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200131 }
Adrià Vilanova Martínezeebc0ac2022-01-05 14:45:53 +0100132 // #!endif
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200133
Adrià Vilanova Martínez1e10d192021-12-31 16:01:13 +0100134 // Inject the batch lock and workflow buttons in the thread list if the
135 // corresponding options are currently enabled.
136 // The order is the inverse because the first one will be shown last.
Adrià Vilanova Martínez7e8796c2022-01-23 21:46:46 +0100137 if (batchLock.shouldAddButton(node)) batchLock.addButtonIfEnabled(node);
Adrià Vilanova Martínez1e10d192021-12-31 16:01:13 +0100138
139 if (workflows.shouldAddThreadListBtn(node))
140 workflows.addThreadListBtnIfEnabled(node);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200141
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200142 // Inject avatar links to threads in the thread list. injectIfEnabled is
143 // responsible of determining whether it should run or not depending on its
144 // current setting.
145 if (('tagName' in node) && (node.tagName == 'LI') &&
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200146 node.querySelector('ec-thread-summary') !== null) {
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200147 avatars.injectIfEnabled(node);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200148 }
149
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200150 // Set up the autorefresh list feature. The setUp function is responsible
151 // of determining whether it should run or not depending on the current
152 // setting.
153 if (('tagName' in node) && node.tagName == 'EC-THREAD-LIST') {
avm99963fd222672021-08-12 23:23:01 +0200154 window.TWPTAutoRefresh.setUp();
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200155 }
156
157 // Redirect unified profile iframe to dark version if applicable
158 if (node.tagName == 'IFRAME' && isDarkThemeOn(options) &&
159 unifiedProfilesFix.checkIframe(node)) {
160 unifiedProfilesFix.fixIframe(node);
161 }
Adrià Vilanova Martínez74a25202022-01-23 23:36:58 +0100162
163 // Show additional details in the canned responses view.
164 if (node.matches('ec-canned-response-row .tags')) {
165 window.TWPTExtraInfo.injectAtCRIfEnabled(node, /* isExpanded = */ false);
166 }
167 if (node.matches('ec-canned-response-row .main .toolbelt')) {
168 const tags = node.parentNode?.querySelector?.('.tags');
Adrià Vilanova Martínez6e4a68d2022-01-24 21:44:32 +0100169 if (tags)
170 window.TWPTExtraInfo.injectAtCRIfEnabled(tags, /* isExpanded = */ true);
171 }
172
173 // Show additional details in the thread view.
174 if (node.matches('ec-thread div[role="list"]')) {
175 const question = node.querySelector('ec-question');
176 if (question) window.TWPTExtraInfo.injectAtQuestionIfEnabled(question);
177 }
178 if (node.matches('ec-thread ec-message')) {
179 window.TWPTExtraInfo.injectAtMessageIfEnabled(node);
Adrià Vilanova Martínez74a25202022-01-23 23:36:58 +0100180 }
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200181 }
182}
183
184function handleRemovedNode(node) {
185 // Remove snackbar when exiting thread list view
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200186 if ('tagName' in node && node.tagName == 'EC-THREAD-LIST') {
avm99963fd222672021-08-12 23:23:01 +0200187 window.TWPTAutoRefresh.hideUpdatePrompt();
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200188 }
189}
190
191function mutationCallback(mutationList, observer) {
192 mutationList.forEach((mutation) => {
193 if (mutation.type == 'childList') {
194 mutation.addedNodes.forEach(function(node) {
195 handleCandidateNode(node);
196 });
197
198 mutation.removedNodes.forEach(function(node) {
199 handleRemovedNode(node);
200 });
201 }
202 });
203}
204
205function intersectionCallback(entries, observer) {
206 entries.forEach(entry => {
207 if (entry.isIntersecting) {
208 entry.target.click();
209 }
210 });
211};
212
213var observerOptions = {
214 childList: true,
215 subtree: true,
216};
217
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200218getOptions(null).then(items => {
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200219 options = items;
220
Adrià Vilanova Martínez27c69962021-07-17 23:32:51 +0200221 // Initialize classes needed by the mutation observer
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200222 avatars = new AvatarsHandler();
Adrià Vilanova Martínez1e10d192021-12-31 16:01:13 +0100223 workflows = new Workflows();
Adrià Vilanova Martínez27c69962021-07-17 23:32:51 +0200224
avm99963b6f68b62021-08-12 23:13:06 +0200225 // autoRefresh is initialized in start.js
avm99963d3f4ac02021-08-12 18:36:58 +0200226
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200227 // Before starting the mutation Observer, check whether we missed any
228 // mutations by manually checking whether some watched nodes already
229 // exist.
230 var cssSelectors = watchedNodesSelectors.join(',');
231 document.querySelectorAll(cssSelectors)
232 .forEach(node => handleCandidateNode(node));
233
234 mutationObserver = new MutationObserver(mutationCallback);
235 mutationObserver.observe(document.body, observerOptions);
236
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200237 // TODO(avm99963): The following features are not dynamic. Make them be.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200238 if (options.fixedtoolbar) {
239 injectStyles(
240 'ec-bulk-actions{position: sticky; top: 0; background: var(--TWPT-primary-background, #fff); z-index: 96;}');
241 }
242
243 if (options.increasecontrast) {
244 injectStyles(
245 '.thread-summary.read:not(.checked){background: var(--TWPT-thread-read-background, #ecedee)!important;}');
246 }
247
248 if (options.stickysidebarheaders) {
249 injectStyles(
250 'material-drawer .main-header{background: var(--TWPT-drawer-background, #fff)!important; position: sticky; top: 0; z-index: 1;}');
251 }
252
253 if (options.enhancedannouncementsdot) {
254 injectStylesheet(
255 chrome.runtime.getURL('css/enhanced_announcements_dot.css'));
256 }
257
258 if (options.repositionexpandthread) {
Adrià Vilanova Martínez27c69962021-07-17 23:32:51 +0200259 injectStylesheet(chrome.runtime.getURL('css/reposition_expand_thread.css'));
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200260 }
261
Adrià Vilanova Martínez9d27c212021-12-05 13:54:10 +0100262 if (options.imagemaxheight) {
263 injectStylesheet(chrome.runtime.getURL('css/image_max_height.css'));
264 }
265
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200266 if (options.ccforcehidedrawer) {
267 var drawer = document.querySelector('material-drawer');
268 if (drawer !== null && drawer.classList.contains('mat-drawer-expanded')) {
269 document.querySelector('.material-drawer-button').click();
270 }
271 }
272
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200273 // Batch lock
274 injectScript(chrome.runtime.getURL('batchLockInject.bundle.js'));
275 injectStylesheet(chrome.runtime.getURL('css/batchlock_inject.css'));
276 // Thread list avatars
277 injectStylesheet(chrome.runtime.getURL('css/thread_list_avatars.css'));
278 // Auto refresh list
279 injectStylesheet(chrome.runtime.getURL('css/autorefresh_list.css'));
Adrià Vilanova Martínez7e8796c2022-01-23 21:46:46 +0100280 // Extra info
281 injectStylesheet(chrome.runtime.getURL('css/extrainfo.css'));
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200282});