blob: f5d5a474fcc69d7372f5f80389bcae140311aafc [file] [log] [blame]
Adrià Vilanova Martínezd7951b22024-05-25 22:13:32 +02001import {injectScript, injectStyles, injectStylesheet} from '../../common/contentScriptsUtils';
Adrià Vilanova Martínezb523be92024-05-25 19:14:19 +02002import {getOptions} from '../../common/options/optionsUtils.js';
Adrià Vilanova Martínez825888a2023-07-23 01:53:48 +02003import XHRProxyKillSwitchHandler from '../../xhrInterceptor/killSwitchHandler.js';
Adrià Vilanova Martínez1f652522021-10-14 00:23:23 +02004import {injectPreviousPostsLinksUnifiedProfileIfEnabled} from '../utilsCommon/unifiedProfiles.js';
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +02005
Adrià Vilanova Martínez27c69962021-07-17 23:32:51 +02006import AvatarsHandler from './avatars.js';
Adrià Vilanova Martínez462280f2021-08-07 22:59:02 +02007import {batchLock} from './batchLock.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ínez115e3d82023-01-10 21:50:06 +010011import {default as FlattenThreads, kMatchingSelectors as kFlattenThreadMatchingSelectors} from './flattenThreads/flattenThreads.js';
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010012import {kRepliesSectionSelector} from './threadToolbar/constants.js';
13import ThreadToolbar from './threadToolbar/threadToolbar.js';
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020014
Adrià Vilanova Martínez917797e2024-05-25 22:41:25 +020015var mutationObserver, options, avatars, threadToolbar, flattenThreads;
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020016
17const watchedNodesSelectors = [
Adrià Vilanova Martíneza7ae3db2022-01-28 11:57:27 +010018 // Scrollable content (used for the intersection observer)
19 '.scrollable-content',
20
21 // Load more bar and buttons
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020022 '.load-more-bar',
Adrià Vilanova Martíneza7ae3db2022-01-28 11:57:27 +010023 '.scTailwindThreadMorebuttonbutton',
Adrià Vilanova Martínez88854972022-08-28 11:57:12 +020024 '.scTailwindThreadMessagegapbutton',
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020025
Adrià Vilanova Martínez7e8796c2022-01-23 21:46:46 +010026 // User profile card inside ec-unified-user
27 'ec-unified-user .scTailwindUser_profileUsercardmain',
28
Adrià Vilanova Martínez531cd072021-12-05 20:15:43 +010029 // Username span/editor inside ec-unified-user (user profile view)
Adrià Vilanova Martínez1f652522021-10-14 00:23:23 +020030 'ec-unified-user .scTailwindUser_profileUsercarddetails',
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020031
32 // Rich text editor
33 'ec-movable-dialog',
34 'ec-rich-text-editor',
35
36 // Read/unread bulk action in the list of thread, for the batch lock feature
37 'ec-bulk-actions material-button[debugid="mark-read-button"]',
38 'ec-bulk-actions material-button[debugid="mark-unread-button"]',
39
Adrià Vilanova Martínez66836fd2024-05-04 22:43:06 +020040 // Thread list items (used to inject the avatars)
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020041 'li',
42
43 // Thread list (used for the autorefresh feature)
44 'ec-thread-list',
45
Adrià Vilanova61f782f2024-05-31 22:57:24 +000046 // Thread page main content
47 'ec-thread > .page > .material-content > div[role="list"]',
48
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010049 // Thread page reply section (for the thread page toolbar)
50 kRepliesSectionSelector,
Adrià Vilanova Martínez412b7582022-12-30 01:35:30 +010051
52 // Reply payload (for the flatten threads UI)
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +010053 ...kFlattenThreadMatchingSelectors,
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020054];
55
56function handleCandidateNode(node) {
57 if (typeof node.classList !== 'undefined') {
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020058 // Show the "previous posts" links if the option is currently enabled.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020059 // Here we're selecting the 'ec-user > div' element (unique child)
Adrià Vilanova Martínez1f652522021-10-14 00:23:23 +020060 if (node.matches(
61 'ec-unified-user .scTailwindUser_profileUsercarddetails')) {
62 injectPreviousPostsLinksUnifiedProfileIfEnabled(
63 /* isCommunityConsole = */ true);
64 }
65
Adrià Vilanova Martínezeebc0ac2022-01-05 14:45:53 +010066 // #!if ['chromium', 'chromium_mv3'].includes(browser_target)
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020067 // Fix the drag&drop issue with the rich text editor if the option is
68 // currently enabled.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020069 //
70 // We target both tags because in different contexts different
71 // elements containing the text editor get added to the DOM structure.
72 // Sometimes it's a EC-MOVABLE-DIALOG which already contains the
73 // EC-RICH-TEXT-EDITOR, and sometimes it's the EC-RICH-TEXT-EDITOR
74 // directly.
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020075 if (('tagName' in node) &&
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020076 (node.tagName == 'EC-MOVABLE-DIALOG' ||
77 node.tagName == 'EC-RICH-TEXT-EDITOR')) {
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020078 applyDragAndDropFixIfEnabled(node);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020079 }
Adrià Vilanova Martínezeebc0ac2022-01-05 14:45:53 +010080 // #!endif
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020081
Adrià Vilanova Martínezb06c13d2022-06-20 17:30:14 +000082 // Inject the batch lock button in the thread list if the option is
83 // currently enabled.
Adrià Vilanova Martínez4107b5e2022-10-09 23:11:11 +020084 if (batchLock.shouldAddButton(node)) {
Adrià Vilanova Martínezb06c13d2022-06-20 17:30:14 +000085 batchLock.addButtonIfEnabled(node);
86 }
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020087
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020088 // Inject avatar links to threads in the thread list. injectIfEnabled is
89 // responsible of determining whether it should run or not depending on its
90 // current setting.
91 if (('tagName' in node) && (node.tagName == 'LI') &&
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020092 node.querySelector('ec-thread-summary') !== null) {
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020093 avatars.injectIfEnabled(node);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020094 }
95
Adrià Vilanova61f782f2024-05-31 22:57:24 +000096 // Inject old thread page design warning if applicable
97 if (node.matches(
98 'ec-thread > .page > .material-content > div[role="list"]')) {
99 window.TWPTThreadPageDesignWarning.injectWarningIfApplicable(node);
100 }
101
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +0100102 // Inject thread toolbar
103 if (threadToolbar.shouldInject(node)) {
104 threadToolbar.injectIfApplicable(node);
105 }
Adrià Vilanova Martínez412b7582022-12-30 01:35:30 +0100106
107 // Inject parent reply quote
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +0100108 if (flattenThreads.shouldInjectQuote(node)) {
109 flattenThreads.injectQuoteIfApplicable(node);
110 }
111
112 // Inject reply button in non-nested view
113 if (flattenThreads.shouldInjectReplyBtn(node)) {
114 flattenThreads.injectReplyBtnIfApplicable(node);
Adrià Vilanova Martínez412b7582022-12-30 01:35:30 +0100115 }
Adrià Vilanova Martínez4d599252023-02-04 20:54:54 +0100116
117 // Delete additional info in the edit message box
118 if (flattenThreads.isAdditionalInfoElement(node)) {
119 flattenThreads.deleteAdditionalInfoElementIfApplicable(node);
120 }
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200121 }
122}
123
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +0100124function handleRemovedNode(mutation, node) {
125 if (!('tagName' in node)) return;
126
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +0100127 // Readd reply button when the Community Console removes it
128 if (node.tagName == 'TWPT-FLATTEN-THREAD-REPLY-BUTTON') {
129 flattenThreads.injectReplyBtn(
130 mutation.target, JSON.parse(node.getAttribute('extraInfo')));
131 }
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200132}
133
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +0100134function mutationCallback(mutationList) {
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200135 mutationList.forEach((mutation) => {
136 if (mutation.type == 'childList') {
137 mutation.addedNodes.forEach(function(node) {
138 handleCandidateNode(node);
139 });
140
141 mutation.removedNodes.forEach(function(node) {
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +0100142 handleRemovedNode(mutation, node);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200143 });
144 }
145 });
146}
147
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200148var observerOptions = {
149 childList: true,
150 subtree: true,
151};
152
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200153getOptions(null).then(items => {
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200154 options = items;
155
Adrià Vilanova Martínez27c69962021-07-17 23:32:51 +0200156 // Initialize classes needed by the mutation observer
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200157 avatars = new AvatarsHandler();
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +0100158 threadToolbar = new ThreadToolbar();
Adrià Vilanova Martínez412b7582022-12-30 01:35:30 +0100159 flattenThreads = new FlattenThreads();
Adrià Vilanova Martínez27c69962021-07-17 23:32:51 +0200160
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200161 // Before starting the mutation Observer, check whether we missed any
162 // mutations by manually checking whether some watched nodes already
163 // exist.
164 var cssSelectors = watchedNodesSelectors.join(',');
165 document.querySelectorAll(cssSelectors)
166 .forEach(node => handleCandidateNode(node));
167
168 mutationObserver = new MutationObserver(mutationCallback);
169 mutationObserver.observe(document.body, observerOptions);
170
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200171 // TODO(avm99963): The following features are not dynamic. Make them be.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200172 if (options.fixedtoolbar) {
173 injectStyles(
Adrià Vilanovae8ba09a2024-02-29 19:25:39 +0000174 'ec-bulk-actions{position: sticky; top: 0; background: var(--TWPT-primary-background, #fff); z-index: 96;}');
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200175 }
176
177 if (options.increasecontrast) {
178 injectStyles(
179 '.thread-summary.read:not(.checked){background: var(--TWPT-thread-read-background, #ecedee)!important;}');
180 }
181
182 if (options.stickysidebarheaders) {
183 injectStyles(
184 'material-drawer .main-header{background: var(--TWPT-drawer-background, #fff)!important; position: sticky; top: 0; z-index: 1;}');
185 }
186
187 if (options.enhancedannouncementsdot) {
188 injectStylesheet(
189 chrome.runtime.getURL('css/enhanced_announcements_dot.css'));
190 }
191
192 if (options.repositionexpandthread) {
Adrià Vilanova Martínez27c69962021-07-17 23:32:51 +0200193 injectStylesheet(chrome.runtime.getURL('css/reposition_expand_thread.css'));
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200194 }
195
Adrià Vilanova Martínez9d27c212021-12-05 13:54:10 +0100196 if (options.imagemaxheight) {
197 injectStylesheet(chrome.runtime.getURL('css/image_max_height.css'));
198 }
199
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200200 if (options.ccforcehidedrawer) {
201 var drawer = document.querySelector('material-drawer');
202 if (drawer !== null && drawer.classList.contains('mat-drawer-expanded')) {
203 document.querySelector('.material-drawer-button').click();
204 }
205 }
206
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200207 // Batch lock
208 injectScript(chrome.runtime.getURL('batchLockInject.bundle.js'));
209 injectStylesheet(chrome.runtime.getURL('css/batchlock_inject.css'));
210 // Thread list avatars
211 injectStylesheet(chrome.runtime.getURL('css/thread_list_avatars.css'));
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +0100212 // Thread toolbar
213 injectStylesheet(chrome.runtime.getURL('css/thread_toolbar.css'));
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +0100214 // Flatten threads
215 injectStylesheet(chrome.runtime.getURL('css/flatten_threads.css'));
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200216});
Adrià Vilanova Martínez825888a2023-07-23 01:53:48 +0200217
218new XHRProxyKillSwitchHandler();