blob: d8d0ba0e1029499ad4e0420e69e008dcf8015b09 [file] [log] [blame]
Adrià Vilanova Martínez3b585fd2024-11-10 19:17:31 +01001import {injectScript, 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ínez2d9be8d2022-12-28 00:50:14 +010011import {kRepliesSectionSelector} from './threadToolbar/constants.js';
12import ThreadToolbar from './threadToolbar/threadToolbar.js';
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020013
Adrià Vilanova Martínez3c13a762024-11-09 23:03:32 +010014var mutationObserver, options, avatars, threadToolbar;
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020015
16const watchedNodesSelectors = [
Adrià Vilanova Martíneza7ae3db2022-01-28 11:57:27 +010017 // Scrollable content (used for the intersection observer)
18 '.scrollable-content',
19
20 // Load more bar and buttons
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020021 '.load-more-bar',
Adrià Vilanova Martíneza7ae3db2022-01-28 11:57:27 +010022 '.scTailwindThreadMorebuttonbutton',
Adrià Vilanova Martínez88854972022-08-28 11:57:12 +020023 '.scTailwindThreadMessagegapbutton',
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020024
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
Adrià Vilanova Martínez66836fd2024-05-04 22:43:06 +020039 // Thread list items (used to inject the avatars)
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020040 'li',
41
42 // Thread list (used for the autorefresh feature)
43 'ec-thread-list',
44
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010045 // Thread page reply section (for the thread page toolbar)
46 kRepliesSectionSelector,
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020047];
48
49function handleCandidateNode(node) {
50 if (typeof node.classList !== 'undefined') {
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020051 // Show the "previous posts" links if the option is currently enabled.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020052 // Here we're selecting the 'ec-user > div' element (unique child)
Adrià Vilanova Martínez1f652522021-10-14 00:23:23 +020053 if (node.matches(
54 'ec-unified-user .scTailwindUser_profileUsercarddetails')) {
55 injectPreviousPostsLinksUnifiedProfileIfEnabled(
56 /* isCommunityConsole = */ true);
57 }
58
Adrià Vilanova Martínezeebc0ac2022-01-05 14:45:53 +010059 // #!if ['chromium', 'chromium_mv3'].includes(browser_target)
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020060 // Fix the drag&drop issue with the rich text editor if the option is
61 // currently enabled.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020062 //
63 // We target both tags because in different contexts different
64 // elements containing the text editor get added to the DOM structure.
65 // Sometimes it's a EC-MOVABLE-DIALOG which already contains the
66 // EC-RICH-TEXT-EDITOR, and sometimes it's the EC-RICH-TEXT-EDITOR
67 // directly.
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020068 if (('tagName' in node) &&
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020069 (node.tagName == 'EC-MOVABLE-DIALOG' ||
70 node.tagName == 'EC-RICH-TEXT-EDITOR')) {
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020071 applyDragAndDropFixIfEnabled(node);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020072 }
Adrià Vilanova Martínezeebc0ac2022-01-05 14:45:53 +010073 // #!endif
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020074
Adrià Vilanova Martínezb06c13d2022-06-20 17:30:14 +000075 // Inject the batch lock button in the thread list if the option is
76 // currently enabled.
Adrià Vilanova Martínez4107b5e2022-10-09 23:11:11 +020077 if (batchLock.shouldAddButton(node)) {
Adrià Vilanova Martínezb06c13d2022-06-20 17:30:14 +000078 batchLock.addButtonIfEnabled(node);
79 }
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020080
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020081 // Inject avatar links to threads in the thread list. injectIfEnabled is
82 // responsible of determining whether it should run or not depending on its
83 // current setting.
84 if (('tagName' in node) && (node.tagName == 'LI') &&
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020085 node.querySelector('ec-thread-summary') !== null) {
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020086 avatars.injectIfEnabled(node);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020087 }
88
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +010089 // Inject thread toolbar
90 if (threadToolbar.shouldInject(node)) {
91 threadToolbar.injectIfApplicable(node);
92 }
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +010093 }
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020094}
95
Adrià Vilanova Martínez115e3d82023-01-10 21:50:06 +010096function mutationCallback(mutationList) {
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020097 mutationList.forEach((mutation) => {
98 if (mutation.type == 'childList') {
99 mutation.addedNodes.forEach(function(node) {
100 handleCandidateNode(node);
101 });
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200102 }
103 });
104}
105
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200106var observerOptions = {
107 childList: true,
108 subtree: true,
109};
110
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200111getOptions(null).then(items => {
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200112 options = items;
113
Adrià Vilanova Martínez27c69962021-07-17 23:32:51 +0200114 // Initialize classes needed by the mutation observer
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200115 avatars = new AvatarsHandler();
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +0100116 threadToolbar = new ThreadToolbar();
Adrià Vilanova Martínez27c69962021-07-17 23:32:51 +0200117
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200118 // Before starting the mutation Observer, check whether we missed any
119 // mutations by manually checking whether some watched nodes already
120 // exist.
121 var cssSelectors = watchedNodesSelectors.join(',');
122 document.querySelectorAll(cssSelectors)
123 .forEach(node => handleCandidateNode(node));
124
125 mutationObserver = new MutationObserver(mutationCallback);
126 mutationObserver.observe(document.body, observerOptions);
127
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200128 if (options.ccforcehidedrawer) {
129 var drawer = document.querySelector('material-drawer');
130 if (drawer !== null && drawer.classList.contains('mat-drawer-expanded')) {
131 document.querySelector('.material-drawer-button').click();
132 }
133 }
134
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200135 // Batch lock
136 injectScript(chrome.runtime.getURL('batchLockInject.bundle.js'));
137 injectStylesheet(chrome.runtime.getURL('css/batchlock_inject.css'));
138 // Thread list avatars
139 injectStylesheet(chrome.runtime.getURL('css/thread_list_avatars.css'));
Adrià Vilanova Martínez2d9be8d2022-12-28 00:50:14 +0100140 // Thread toolbar
141 injectStylesheet(chrome.runtime.getURL('css/thread_toolbar.css'));
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200142});
Adrià Vilanova Martínez825888a2023-07-23 01:53:48 +0200143
144new XHRProxyKillSwitchHandler();