blob: 5053c38782cddc4bb4c890179e610cbcb19a3a3d [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íneza7ae3db2022-01-28 11:57:27 +010011import InfiniteScroll from './infiniteScroll.js';
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020012import {unifiedProfilesFix} from './unifiedProfiles.js';
13
Adrià Vilanova Martínezb06c13d2022-06-20 17:30:14 +000014var mutationObserver, options, avatars, infiniteScroll;
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020015
16const watchedNodesSelectors = [
17 // App container (used to set up the intersection observer and inject the dark
18 // mode button)
19 'ec-app',
20
Adrià Vilanova Martíneza7ae3db2022-01-28 11:57:27 +010021 // Scrollable content (used for the intersection observer)
22 '.scrollable-content',
23
24 // Load more bar and buttons
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020025 '.load-more-bar',
Adrià Vilanova Martíneza7ae3db2022-01-28 11:57:27 +010026 '.scTailwindThreadMorebuttonbutton',
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020027
Adrià Vilanova Martínez7e8796c2022-01-23 21:46:46 +010028 // User profile card inside ec-unified-user
29 'ec-unified-user .scTailwindUser_profileUsercardmain',
30
Adrià Vilanova Martínez531cd072021-12-05 20:15:43 +010031 // Username span/editor inside ec-unified-user (user profile view)
Adrià Vilanova Martínez1f652522021-10-14 00:23:23 +020032 'ec-unified-user .scTailwindUser_profileUsercarddetails',
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020033
34 // Rich text editor
35 'ec-movable-dialog',
36 'ec-rich-text-editor',
37
38 // Read/unread bulk action in the list of thread, for the batch lock feature
39 'ec-bulk-actions material-button[debugid="mark-read-button"]',
40 'ec-bulk-actions material-button[debugid="mark-unread-button"]',
41
Adrià Vilanova Martínez854cb912022-02-02 16:18:23 +010042 // Thread list items (used to inject the avatars and extra info)
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020043 'li',
44
Adrià Vilanova Martínez854cb912022-02-02 16:18:23 +010045 // Thread list item toolbelt (used for the extra info feature)
46 'ec-thread-summary .main .toolbelt',
47
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020048 // Thread list (used for the autorefresh feature)
49 'ec-thread-list',
50
51 // Unified profile iframe
52 'iframe',
Adrià Vilanova Martínez74a25202022-01-23 23:36:58 +010053
54 // Canned response tags or toolbelt (for the extra info feature)
Adrià Vilanova Martínez6e4a68d2022-01-24 21:44:32 +010055 'ec-canned-response-row .tags',
56 'ec-canned-response-row .main .toolbelt',
57
Adrià Vilanova Martínez09b3bdb2022-02-05 00:15:05 +010058 // Question state chips container (for the extra info feature)
59 'ec-question .state-chips',
Adrià Vilanova Martínez6e4a68d2022-01-24 21:44:32 +010060
61 // Replies (for the extra info feature)
62 'ec-thread ec-message',
Adrià Vilanova Martínez4f56d562022-01-26 00:23:27 +010063
64 // User activity chart (for the per-forum stats feature)
65 'ec-unified-user .scTailwindUser_profileUserprofilesection ' +
66 'sc-tailwind-shared-activity-chart',
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020067];
68
69function handleCandidateNode(node) {
70 if (typeof node.classList !== 'undefined') {
71 if (('tagName' in node) && node.tagName == 'EC-APP') {
Adrià Vilanova Martíneza7ae3db2022-01-28 11:57:27 +010072 infiniteScroll.setUpIntersectionObserver(node, false);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020073
74 // Inject the dark mode button
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020075 // TODO(avm99963): make this feature dynamic.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020076 if (options.ccdarktheme && options.ccdarktheme_mode == 'switch') {
77 var rightControl = node.querySelector('header .right-control');
78 if (rightControl !== null)
79 injectDarkModeButton(rightControl, options.ccdarktheme_switch_status);
80 }
81 }
82
Adrià Vilanova Martíneza7ae3db2022-01-28 11:57:27 +010083 // To set up infinite scroll
84 if (node.classList.contains('scrollable-content')) {
85 infiniteScroll.setUpIntersectionObserver(node, true);
86 }
87
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020088 // Start the intersectionObserver for the "load more"/"load all" buttons
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020089 // inside a thread if the option is currently enabled.
90 if (node.classList.contains('load-more-bar')) {
Adrià Vilanova Martíneza7ae3db2022-01-28 11:57:27 +010091 infiniteScroll.observeLoadMoreBar(node);
92 }
93 if (node.classList.contains('scTailwindThreadMorebuttonbutton')) {
94 infiniteScroll.observeLoadMoreInteropBtn(node);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020095 }
96
Adrià Vilanova Martínez7e8796c2022-01-23 21:46:46 +010097 // Show additional details in the profile view.
98 if (node.matches('ec-unified-user .scTailwindUser_profileUsercardmain')) {
99 window.TWPTExtraInfo.injectAtProfileIfEnabled(node);
100 }
101
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200102 // Show the "previous posts" links if the option is currently enabled.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200103 // Here we're selecting the 'ec-user > div' element (unique child)
Adrià Vilanova Martínez1f652522021-10-14 00:23:23 +0200104 if (node.matches(
105 'ec-unified-user .scTailwindUser_profileUsercarddetails')) {
106 injectPreviousPostsLinksUnifiedProfileIfEnabled(
107 /* isCommunityConsole = */ true);
108 }
109
Adrià Vilanova Martínezeebc0ac2022-01-05 14:45:53 +0100110 // #!if ['chromium', 'chromium_mv3'].includes(browser_target)
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200111 // Fix the drag&drop issue with the rich text editor if the option is
112 // currently enabled.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200113 //
114 // We target both tags because in different contexts different
115 // elements containing the text editor get added to the DOM structure.
116 // Sometimes it's a EC-MOVABLE-DIALOG which already contains the
117 // EC-RICH-TEXT-EDITOR, and sometimes it's the EC-RICH-TEXT-EDITOR
118 // directly.
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200119 if (('tagName' in node) &&
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200120 (node.tagName == 'EC-MOVABLE-DIALOG' ||
121 node.tagName == 'EC-RICH-TEXT-EDITOR')) {
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200122 applyDragAndDropFixIfEnabled(node);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200123 }
Adrià Vilanova Martínezeebc0ac2022-01-05 14:45:53 +0100124 // #!endif
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200125
Adrià Vilanova Martínezb06c13d2022-06-20 17:30:14 +0000126 // Inject the batch lock button in the thread list if the option is
127 // currently enabled.
128 if (batchLock.nodeIsReadToggleBtn(node)) {
129 batchLock.addButtonIfEnabled(node);
130 }
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200131
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200132 // Inject avatar links to threads in the thread list. injectIfEnabled is
133 // responsible of determining whether it should run or not depending on its
134 // current setting.
Adrià Vilanova Martínez854cb912022-02-02 16:18:23 +0100135 //
136 // Also, inject extra info in the thread list.
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200137 if (('tagName' in node) && (node.tagName == 'LI') &&
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200138 node.querySelector('ec-thread-summary') !== null) {
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200139 avatars.injectIfEnabled(node);
Adrià Vilanova Martínez854cb912022-02-02 16:18:23 +0100140 window.TWPTExtraInfo.injectAtThreadListIfEnabled(node);
141 }
142
143 // Inject extra info in the toolbelt of an expanded thread list item.
144 if (node.matches('ec-thread-summary .main .toolbelt')) {
145 window.TWPTExtraInfo.injectAtExpandedThreadListIfEnabled(node);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200146 }
147
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200148 // Set up the autorefresh list feature. The setUp function is responsible
149 // of determining whether it should run or not depending on the current
150 // setting.
151 if (('tagName' in node) && node.tagName == 'EC-THREAD-LIST') {
avm99963fd222672021-08-12 23:23:01 +0200152 window.TWPTAutoRefresh.setUp();
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200153 }
154
155 // Redirect unified profile iframe to dark version if applicable
156 if (node.tagName == 'IFRAME' && isDarkThemeOn(options) &&
157 unifiedProfilesFix.checkIframe(node)) {
158 unifiedProfilesFix.fixIframe(node);
159 }
Adrià Vilanova Martínez74a25202022-01-23 23:36:58 +0100160
161 // Show additional details in the canned responses view.
162 if (node.matches('ec-canned-response-row .tags')) {
163 window.TWPTExtraInfo.injectAtCRIfEnabled(node, /* isExpanded = */ false);
164 }
165 if (node.matches('ec-canned-response-row .main .toolbelt')) {
166 const tags = node.parentNode?.querySelector?.('.tags');
Adrià Vilanova Martínez6e4a68d2022-01-24 21:44:32 +0100167 if (tags)
168 window.TWPTExtraInfo.injectAtCRIfEnabled(tags, /* isExpanded = */ true);
169 }
170
171 // Show additional details in the thread view.
Adrià Vilanova Martínez09b3bdb2022-02-05 00:15:05 +0100172 if (node.matches('ec-question .state-chips')) {
173 window.TWPTExtraInfo.injectAtQuestionIfEnabled(node);
Adrià Vilanova Martínez6e4a68d2022-01-24 21:44:32 +0100174 }
175 if (node.matches('ec-thread ec-message')) {
176 window.TWPTExtraInfo.injectAtMessageIfEnabled(node);
Adrià Vilanova Martínez74a25202022-01-23 23:36:58 +0100177 }
Adrià Vilanova Martínez4f56d562022-01-26 00:23:27 +0100178
179 // Inject per-forum stats section in the user profile
180 if (node.matches(
181 'ec-unified-user .scTailwindUser_profileUserprofilesection ' +
182 'sc-tailwind-shared-activity-chart')) {
183 window.TWPTExtraInfo.injectPerForumStatsIfEnabled(node);
184 }
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200185 }
186}
187
188function handleRemovedNode(node) {
189 // Remove snackbar when exiting thread list view
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200190 if ('tagName' in node && node.tagName == 'EC-THREAD-LIST') {
avm99963fd222672021-08-12 23:23:01 +0200191 window.TWPTAutoRefresh.hideUpdatePrompt();
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200192 }
193}
194
195function mutationCallback(mutationList, observer) {
196 mutationList.forEach((mutation) => {
197 if (mutation.type == 'childList') {
198 mutation.addedNodes.forEach(function(node) {
199 handleCandidateNode(node);
200 });
201
202 mutation.removedNodes.forEach(function(node) {
203 handleRemovedNode(node);
204 });
205 }
206 });
207}
208
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200209var observerOptions = {
210 childList: true,
211 subtree: true,
212};
213
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200214getOptions(null).then(items => {
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200215 options = items;
216
Adrià Vilanova Martínez27c69962021-07-17 23:32:51 +0200217 // Initialize classes needed by the mutation observer
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200218 avatars = new AvatarsHandler();
Adrià Vilanova Martíneza7ae3db2022-01-28 11:57:27 +0100219 infiniteScroll = new InfiniteScroll();
Adrià Vilanova Martínez27c69962021-07-17 23:32:51 +0200220
Adrià Vilanova Martínez854cb912022-02-02 16:18:23 +0100221 // autoRefresh and extraInfo are initialized in start.js
avm99963d3f4ac02021-08-12 18:36:58 +0200222
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200223 // Before starting the mutation Observer, check whether we missed any
224 // mutations by manually checking whether some watched nodes already
225 // exist.
226 var cssSelectors = watchedNodesSelectors.join(',');
227 document.querySelectorAll(cssSelectors)
228 .forEach(node => handleCandidateNode(node));
229
230 mutationObserver = new MutationObserver(mutationCallback);
231 mutationObserver.observe(document.body, observerOptions);
232
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200233 // TODO(avm99963): The following features are not dynamic. Make them be.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200234 if (options.fixedtoolbar) {
235 injectStyles(
236 'ec-bulk-actions{position: sticky; top: 0; background: var(--TWPT-primary-background, #fff); z-index: 96;}');
237 }
238
239 if (options.increasecontrast) {
240 injectStyles(
241 '.thread-summary.read:not(.checked){background: var(--TWPT-thread-read-background, #ecedee)!important;}');
242 }
243
244 if (options.stickysidebarheaders) {
245 injectStyles(
246 'material-drawer .main-header{background: var(--TWPT-drawer-background, #fff)!important; position: sticky; top: 0; z-index: 1;}');
247 }
248
249 if (options.enhancedannouncementsdot) {
250 injectStylesheet(
251 chrome.runtime.getURL('css/enhanced_announcements_dot.css'));
252 }
253
254 if (options.repositionexpandthread) {
Adrià Vilanova Martínez27c69962021-07-17 23:32:51 +0200255 injectStylesheet(chrome.runtime.getURL('css/reposition_expand_thread.css'));
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200256 }
257
Adrià Vilanova Martínez9d27c212021-12-05 13:54:10 +0100258 if (options.imagemaxheight) {
259 injectStylesheet(chrome.runtime.getURL('css/image_max_height.css'));
260 }
261
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200262 if (options.ccforcehidedrawer) {
263 var drawer = document.querySelector('material-drawer');
264 if (drawer !== null && drawer.classList.contains('mat-drawer-expanded')) {
265 document.querySelector('.material-drawer-button').click();
266 }
267 }
268
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200269 // Batch lock
270 injectScript(chrome.runtime.getURL('batchLockInject.bundle.js'));
271 injectStylesheet(chrome.runtime.getURL('css/batchlock_inject.css'));
272 // Thread list avatars
273 injectStylesheet(chrome.runtime.getURL('css/thread_list_avatars.css'));
274 // Auto refresh list
275 injectStylesheet(chrome.runtime.getURL('css/autorefresh_list.css'));
Adrià Vilanova Martínez7e8796c2022-01-23 21:46:46 +0100276 // Extra info
277 injectStylesheet(chrome.runtime.getURL('css/extrainfo.css'));
avm9996337601bc2022-02-21 10:36:45 +0100278 injectStylesheet(chrome.runtime.getURL('css/extrainfo_perforumstats.css'));
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200279});