blob: 890f3b0a52d9a45d9a7c5953774d3de61f108fb9 [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à Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010046 // Thread page reply section (for the thread page toolbar)
47 kRepliesSectionSelector,
Adrià Vilanova Martínez412b7582022-12-30 01:35:30 +010048
49 // Reply payload (for the flatten threads UI)
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +010050 ...kFlattenThreadMatchingSelectors,
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020051];
52
53function handleCandidateNode(node) {
54 if (typeof node.classList !== 'undefined') {
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020055 // Show the "previous posts" links if the option is currently enabled.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020056 // Here we're selecting the 'ec-user > div' element (unique child)
Adrià Vilanova Martínez1f652522021-10-14 00:23:23 +020057 if (node.matches(
58 'ec-unified-user .scTailwindUser_profileUsercarddetails')) {
59 injectPreviousPostsLinksUnifiedProfileIfEnabled(
60 /* isCommunityConsole = */ true);
61 }
62
Adrià Vilanova Martínezeebc0ac2022-01-05 14:45:53 +010063 // #!if ['chromium', 'chromium_mv3'].includes(browser_target)
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020064 // Fix the drag&drop issue with the rich text editor if the option is
65 // currently enabled.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020066 //
67 // We target both tags because in different contexts different
68 // elements containing the text editor get added to the DOM structure.
69 // Sometimes it's a EC-MOVABLE-DIALOG which already contains the
70 // EC-RICH-TEXT-EDITOR, and sometimes it's the EC-RICH-TEXT-EDITOR
71 // directly.
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020072 if (('tagName' in node) &&
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020073 (node.tagName == 'EC-MOVABLE-DIALOG' ||
74 node.tagName == 'EC-RICH-TEXT-EDITOR')) {
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020075 applyDragAndDropFixIfEnabled(node);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020076 }
Adrià Vilanova Martínezeebc0ac2022-01-05 14:45:53 +010077 // #!endif
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020078
Adrià Vilanova Martínezb06c13d2022-06-20 17:30:14 +000079 // Inject the batch lock button in the thread list if the option is
80 // currently enabled.
Adrià Vilanova Martínez4107b5e2022-10-09 23:11:11 +020081 if (batchLock.shouldAddButton(node)) {
Adrià Vilanova Martínezb06c13d2022-06-20 17:30:14 +000082 batchLock.addButtonIfEnabled(node);
83 }
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020084
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020085 // Inject avatar links to threads in the thread list. injectIfEnabled is
86 // responsible of determining whether it should run or not depending on its
87 // current setting.
88 if (('tagName' in node) && (node.tagName == 'LI') &&
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020089 node.querySelector('ec-thread-summary') !== null) {
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020090 avatars.injectIfEnabled(node);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020091 }
92
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010093 // Inject thread toolbar
94 if (threadToolbar.shouldInject(node)) {
95 threadToolbar.injectIfApplicable(node);
96 }
Adrià Vilanova Martínez412b7582022-12-30 01:35:30 +010097
98 // Inject parent reply quote
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +010099 if (flattenThreads.shouldInjectQuote(node)) {
100 flattenThreads.injectQuoteIfApplicable(node);
101 }
102
103 // Inject reply button in non-nested view
104 if (flattenThreads.shouldInjectReplyBtn(node)) {
105 flattenThreads.injectReplyBtnIfApplicable(node);
Adrià Vilanova Martínez412b7582022-12-30 01:35:30 +0100106 }
Adrià Vilanova Martínez4d599252023-02-04 20:54:54 +0100107
108 // Delete additional info in the edit message box
109 if (flattenThreads.isAdditionalInfoElement(node)) {
110 flattenThreads.deleteAdditionalInfoElementIfApplicable(node);
111 }
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200112 }
113}
114
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +0100115function handleRemovedNode(mutation, node) {
116 if (!('tagName' in node)) return;
117
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +0100118 // Readd reply button when the Community Console removes it
119 if (node.tagName == 'TWPT-FLATTEN-THREAD-REPLY-BUTTON') {
120 flattenThreads.injectReplyBtn(
121 mutation.target, JSON.parse(node.getAttribute('extraInfo')));
122 }
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200123}
124
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +0100125function mutationCallback(mutationList) {
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200126 mutationList.forEach((mutation) => {
127 if (mutation.type == 'childList') {
128 mutation.addedNodes.forEach(function(node) {
129 handleCandidateNode(node);
130 });
131
132 mutation.removedNodes.forEach(function(node) {
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +0100133 handleRemovedNode(mutation, node);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200134 });
135 }
136 });
137}
138
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200139var observerOptions = {
140 childList: true,
141 subtree: true,
142};
143
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200144getOptions(null).then(items => {
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200145 options = items;
146
Adrià Vilanova Martínez27c69962021-07-17 23:32:51 +0200147 // Initialize classes needed by the mutation observer
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200148 avatars = new AvatarsHandler();
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +0100149 threadToolbar = new ThreadToolbar();
Adrià Vilanova Martínez412b7582022-12-30 01:35:30 +0100150 flattenThreads = new FlattenThreads();
Adrià Vilanova Martínez27c69962021-07-17 23:32:51 +0200151
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200152 // Before starting the mutation Observer, check whether we missed any
153 // mutations by manually checking whether some watched nodes already
154 // exist.
155 var cssSelectors = watchedNodesSelectors.join(',');
156 document.querySelectorAll(cssSelectors)
157 .forEach(node => handleCandidateNode(node));
158
159 mutationObserver = new MutationObserver(mutationCallback);
160 mutationObserver.observe(document.body, observerOptions);
161
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200162 // TODO(avm99963): The following features are not dynamic. Make them be.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200163 if (options.fixedtoolbar) {
164 injectStyles(
Adrià Vilanovae8ba09a2024-02-29 19:25:39 +0000165 '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 +0200166 }
167
168 if (options.increasecontrast) {
169 injectStyles(
170 '.thread-summary.read:not(.checked){background: var(--TWPT-thread-read-background, #ecedee)!important;}');
171 }
172
173 if (options.stickysidebarheaders) {
174 injectStyles(
175 'material-drawer .main-header{background: var(--TWPT-drawer-background, #fff)!important; position: sticky; top: 0; z-index: 1;}');
176 }
177
178 if (options.enhancedannouncementsdot) {
179 injectStylesheet(
180 chrome.runtime.getURL('css/enhanced_announcements_dot.css'));
181 }
182
183 if (options.repositionexpandthread) {
Adrià Vilanova Martínez27c69962021-07-17 23:32:51 +0200184 injectStylesheet(chrome.runtime.getURL('css/reposition_expand_thread.css'));
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200185 }
186
Adrià Vilanova Martínez9d27c212021-12-05 13:54:10 +0100187 if (options.imagemaxheight) {
188 injectStylesheet(chrome.runtime.getURL('css/image_max_height.css'));
189 }
190
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200191 if (options.ccforcehidedrawer) {
192 var drawer = document.querySelector('material-drawer');
193 if (drawer !== null && drawer.classList.contains('mat-drawer-expanded')) {
194 document.querySelector('.material-drawer-button').click();
195 }
196 }
197
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200198 // Batch lock
199 injectScript(chrome.runtime.getURL('batchLockInject.bundle.js'));
200 injectStylesheet(chrome.runtime.getURL('css/batchlock_inject.css'));
201 // Thread list avatars
202 injectStylesheet(chrome.runtime.getURL('css/thread_list_avatars.css'));
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +0100203 // Thread toolbar
204 injectStylesheet(chrome.runtime.getURL('css/thread_toolbar.css'));
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +0100205 // Flatten threads
206 injectStylesheet(chrome.runtime.getURL('css/flatten_threads.css'));
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200207});
Adrià Vilanova Martínez825888a2023-07-23 01:53:48 +0200208
209new XHRProxyKillSwitchHandler();