blob: 75de36f543938095fa5f56b672d42c42624041b5 [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';
Adrià Vilanova Martínez1e10d192021-12-31 16:01:13 +010013import Workflows from './workflows/workflows.js';
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020014
Adrià Vilanova Martíneza7ae3db2022-01-28 11:57:27 +010015var mutationObserver, options, avatars, infiniteScroll, 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
Adrià Vilanova Martíneza7ae3db2022-01-28 11:57:27 +010022 // Scrollable content (used for the intersection observer)
23 '.scrollable-content',
24
25 // Load more bar and buttons
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020026 '.load-more-bar',
Adrià Vilanova Martíneza7ae3db2022-01-28 11:57:27 +010027 '.scTailwindThreadMorebuttonbutton',
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020028
Adrià Vilanova Martínez7e8796c2022-01-23 21:46:46 +010029 // User profile card inside ec-unified-user
30 'ec-unified-user .scTailwindUser_profileUsercardmain',
31
Adrià Vilanova Martínez531cd072021-12-05 20:15:43 +010032 // Username span/editor inside ec-unified-user (user profile view)
Adrià Vilanova Martínez1f652522021-10-14 00:23:23 +020033 'ec-unified-user .scTailwindUser_profileUsercarddetails',
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020034
35 // Rich text editor
36 'ec-movable-dialog',
37 'ec-rich-text-editor',
38
39 // Read/unread bulk action in the list of thread, for the batch lock feature
40 'ec-bulk-actions material-button[debugid="mark-read-button"]',
41 'ec-bulk-actions material-button[debugid="mark-unread-button"]',
42
43 // Thread list items (used to inject the avatars)
44 'li',
45
46 // Thread list (used for the autorefresh feature)
47 'ec-thread-list',
48
49 // Unified profile iframe
50 'iframe',
Adrià Vilanova Martínez74a25202022-01-23 23:36:58 +010051
52 // Canned response tags or toolbelt (for the extra info feature)
Adrià Vilanova Martínez6e4a68d2022-01-24 21:44:32 +010053 'ec-canned-response-row .tags',
54 'ec-canned-response-row .main .toolbelt',
55
56 // Div containing ec-question (for the extra info feature)
57 'ec-thread div[role="list"]',
58
59 // Replies (for the extra info feature)
60 'ec-thread ec-message',
Adrià Vilanova Martínez4f56d562022-01-26 00:23:27 +010061
62 // User activity chart (for the per-forum stats feature)
63 'ec-unified-user .scTailwindUser_profileUserprofilesection ' +
64 'sc-tailwind-shared-activity-chart',
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020065];
66
67function handleCandidateNode(node) {
68 if (typeof node.classList !== 'undefined') {
69 if (('tagName' in node) && node.tagName == 'EC-APP') {
Adrià Vilanova Martíneza7ae3db2022-01-28 11:57:27 +010070 infiniteScroll.setUpIntersectionObserver(node, false);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020071
72 // Inject the dark mode button
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020073 // TODO(avm99963): make this feature dynamic.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020074 if (options.ccdarktheme && options.ccdarktheme_mode == 'switch') {
75 var rightControl = node.querySelector('header .right-control');
76 if (rightControl !== null)
77 injectDarkModeButton(rightControl, options.ccdarktheme_switch_status);
78 }
79 }
80
Adrià Vilanova Martíneza7ae3db2022-01-28 11:57:27 +010081 // To set up infinite scroll
82 if (node.classList.contains('scrollable-content')) {
83 infiniteScroll.setUpIntersectionObserver(node, true);
84 }
85
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020086 // 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íneza7ae3db2022-01-28 11:57:27 +010089 infiniteScroll.observeLoadMoreBar(node);
90 }
91 if (node.classList.contains('scTailwindThreadMorebuttonbutton')) {
92 infiniteScroll.observeLoadMoreInteropBtn(node);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020093 }
94
Adrià Vilanova Martínez7e8796c2022-01-23 21:46:46 +010095 // Show additional details in the profile view.
96 if (node.matches('ec-unified-user .scTailwindUser_profileUsercardmain')) {
97 window.TWPTExtraInfo.injectAtProfileIfEnabled(node);
98 }
99
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200100 // Show the "previous posts" links if the option is currently enabled.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200101 // Here we're selecting the 'ec-user > div' element (unique child)
Adrià Vilanova Martínez1f652522021-10-14 00:23:23 +0200102 if (node.matches(
103 'ec-unified-user .scTailwindUser_profileUsercarddetails')) {
104 injectPreviousPostsLinksUnifiedProfileIfEnabled(
105 /* isCommunityConsole = */ true);
106 }
107
Adrià Vilanova Martínezeebc0ac2022-01-05 14:45:53 +0100108 // #!if ['chromium', 'chromium_mv3'].includes(browser_target)
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200109 // Fix the drag&drop issue with the rich text editor if the option is
110 // currently enabled.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200111 //
112 // We target both tags because in different contexts different
113 // elements containing the text editor get added to the DOM structure.
114 // Sometimes it's a EC-MOVABLE-DIALOG which already contains the
115 // EC-RICH-TEXT-EDITOR, and sometimes it's the EC-RICH-TEXT-EDITOR
116 // directly.
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200117 if (('tagName' in node) &&
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200118 (node.tagName == 'EC-MOVABLE-DIALOG' ||
119 node.tagName == 'EC-RICH-TEXT-EDITOR')) {
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200120 applyDragAndDropFixIfEnabled(node);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200121 }
Adrià Vilanova Martínezeebc0ac2022-01-05 14:45:53 +0100122 // #!endif
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200123
Adrià Vilanova Martínez1e10d192021-12-31 16:01:13 +0100124 // Inject the batch lock and workflow buttons in the thread list if the
125 // corresponding options are currently enabled.
126 // The order is the inverse because the first one will be shown last.
Adrià Vilanova Martínez7e8796c2022-01-23 21:46:46 +0100127 if (batchLock.shouldAddButton(node)) batchLock.addButtonIfEnabled(node);
Adrià Vilanova Martínez1e10d192021-12-31 16:01:13 +0100128
129 if (workflows.shouldAddThreadListBtn(node))
130 workflows.addThreadListBtnIfEnabled(node);
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.
135 if (('tagName' in node) && (node.tagName == 'LI') &&
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200136 node.querySelector('ec-thread-summary') !== null) {
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200137 avatars.injectIfEnabled(node);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200138 }
139
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200140 // Set up the autorefresh list feature. The setUp function is responsible
141 // of determining whether it should run or not depending on the current
142 // setting.
143 if (('tagName' in node) && node.tagName == 'EC-THREAD-LIST') {
avm99963fd222672021-08-12 23:23:01 +0200144 window.TWPTAutoRefresh.setUp();
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200145 }
146
147 // Redirect unified profile iframe to dark version if applicable
148 if (node.tagName == 'IFRAME' && isDarkThemeOn(options) &&
149 unifiedProfilesFix.checkIframe(node)) {
150 unifiedProfilesFix.fixIframe(node);
151 }
Adrià Vilanova Martínez74a25202022-01-23 23:36:58 +0100152
153 // Show additional details in the canned responses view.
154 if (node.matches('ec-canned-response-row .tags')) {
155 window.TWPTExtraInfo.injectAtCRIfEnabled(node, /* isExpanded = */ false);
156 }
157 if (node.matches('ec-canned-response-row .main .toolbelt')) {
158 const tags = node.parentNode?.querySelector?.('.tags');
Adrià Vilanova Martínez6e4a68d2022-01-24 21:44:32 +0100159 if (tags)
160 window.TWPTExtraInfo.injectAtCRIfEnabled(tags, /* isExpanded = */ true);
161 }
162
163 // Show additional details in the thread view.
164 if (node.matches('ec-thread div[role="list"]')) {
165 const question = node.querySelector('ec-question');
166 if (question) window.TWPTExtraInfo.injectAtQuestionIfEnabled(question);
167 }
168 if (node.matches('ec-thread ec-message')) {
169 window.TWPTExtraInfo.injectAtMessageIfEnabled(node);
Adrià Vilanova Martínez74a25202022-01-23 23:36:58 +0100170 }
Adrià Vilanova Martínez4f56d562022-01-26 00:23:27 +0100171
172 // Inject per-forum stats section in the user profile
173 if (node.matches(
174 'ec-unified-user .scTailwindUser_profileUserprofilesection ' +
175 'sc-tailwind-shared-activity-chart')) {
176 window.TWPTExtraInfo.injectPerForumStatsIfEnabled(node);
177 }
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200178 }
179}
180
181function handleRemovedNode(node) {
182 // Remove snackbar when exiting thread list view
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200183 if ('tagName' in node && node.tagName == 'EC-THREAD-LIST') {
avm99963fd222672021-08-12 23:23:01 +0200184 window.TWPTAutoRefresh.hideUpdatePrompt();
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200185 }
186}
187
188function mutationCallback(mutationList, observer) {
189 mutationList.forEach((mutation) => {
190 if (mutation.type == 'childList') {
191 mutation.addedNodes.forEach(function(node) {
192 handleCandidateNode(node);
193 });
194
195 mutation.removedNodes.forEach(function(node) {
196 handleRemovedNode(node);
197 });
198 }
199 });
200}
201
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200202var observerOptions = {
203 childList: true,
204 subtree: true,
205};
206
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200207getOptions(null).then(items => {
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200208 options = items;
209
Adrià Vilanova Martínez27c69962021-07-17 23:32:51 +0200210 // Initialize classes needed by the mutation observer
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200211 avatars = new AvatarsHandler();
Adrià Vilanova Martíneza7ae3db2022-01-28 11:57:27 +0100212 infiniteScroll = new InfiniteScroll();
Adrià Vilanova Martínez1e10d192021-12-31 16:01:13 +0100213 workflows = new Workflows();
Adrià Vilanova Martínez27c69962021-07-17 23:32:51 +0200214
avm99963b6f68b62021-08-12 23:13:06 +0200215 // autoRefresh is initialized in start.js
avm99963d3f4ac02021-08-12 18:36:58 +0200216
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200217 // Before starting the mutation Observer, check whether we missed any
218 // mutations by manually checking whether some watched nodes already
219 // exist.
220 var cssSelectors = watchedNodesSelectors.join(',');
221 document.querySelectorAll(cssSelectors)
222 .forEach(node => handleCandidateNode(node));
223
224 mutationObserver = new MutationObserver(mutationCallback);
225 mutationObserver.observe(document.body, observerOptions);
226
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200227 // TODO(avm99963): The following features are not dynamic. Make them be.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200228 if (options.fixedtoolbar) {
229 injectStyles(
230 'ec-bulk-actions{position: sticky; top: 0; background: var(--TWPT-primary-background, #fff); z-index: 96;}');
231 }
232
233 if (options.increasecontrast) {
234 injectStyles(
235 '.thread-summary.read:not(.checked){background: var(--TWPT-thread-read-background, #ecedee)!important;}');
236 }
237
238 if (options.stickysidebarheaders) {
239 injectStyles(
240 'material-drawer .main-header{background: var(--TWPT-drawer-background, #fff)!important; position: sticky; top: 0; z-index: 1;}');
241 }
242
243 if (options.enhancedannouncementsdot) {
244 injectStylesheet(
245 chrome.runtime.getURL('css/enhanced_announcements_dot.css'));
246 }
247
248 if (options.repositionexpandthread) {
Adrià Vilanova Martínez27c69962021-07-17 23:32:51 +0200249 injectStylesheet(chrome.runtime.getURL('css/reposition_expand_thread.css'));
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200250 }
251
Adrià Vilanova Martínez9d27c212021-12-05 13:54:10 +0100252 if (options.imagemaxheight) {
253 injectStylesheet(chrome.runtime.getURL('css/image_max_height.css'));
254 }
255
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200256 if (options.ccforcehidedrawer) {
257 var drawer = document.querySelector('material-drawer');
258 if (drawer !== null && drawer.classList.contains('mat-drawer-expanded')) {
259 document.querySelector('.material-drawer-button').click();
260 }
261 }
262
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200263 // Batch lock
264 injectScript(chrome.runtime.getURL('batchLockInject.bundle.js'));
265 injectStylesheet(chrome.runtime.getURL('css/batchlock_inject.css'));
266 // Thread list avatars
267 injectStylesheet(chrome.runtime.getURL('css/thread_list_avatars.css'));
268 // Auto refresh list
269 injectStylesheet(chrome.runtime.getURL('css/autorefresh_list.css'));
Adrià Vilanova Martínez7e8796c2022-01-23 21:46:46 +0100270 // Extra info
271 injectStylesheet(chrome.runtime.getURL('css/extrainfo.css'));
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200272});