blob: 0ffff41224e1f944d914a7cfa6dbcd3ea4b6c9db [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ínez3465e772021-07-11 19:18:41 +020011import {unifiedProfilesFix} from './unifiedProfiles.js';
Adrià Vilanova Martínez1e10d192021-12-31 16:01:13 +010012import Workflows from './workflows/workflows.js';
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020013
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020014var mutationObserver, intersectionObserver, intersectionOptions, options,
Adrià Vilanova Martínez1e10d192021-12-31 16:01:13 +010015 avatars, 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
22 // Load more bar (for the "load more"/"load all" buttons)
23 '.load-more-bar',
24
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
39 // Thread list items (used to inject the avatars)
40 'li',
41
42 // Thread list (used for the autorefresh feature)
43 'ec-thread-list',
44
45 // Unified profile iframe
46 'iframe',
Adrià Vilanova Martínez74a25202022-01-23 23:36:58 +010047
48 // Canned response tags or toolbelt (for the extra info feature)
Adrià Vilanova Martínez6e4a68d2022-01-24 21:44:32 +010049 'ec-canned-response-row .tags',
50 'ec-canned-response-row .main .toolbelt',
51
52 // Div containing ec-question (for the extra info feature)
53 'ec-thread div[role="list"]',
54
55 // Replies (for the extra info feature)
56 'ec-thread ec-message',
Adrià Vilanova Martínez4f56d562022-01-26 00:23:27 +010057
58 // User activity chart (for the per-forum stats feature)
59 'ec-unified-user .scTailwindUser_profileUserprofilesection ' +
60 'sc-tailwind-shared-activity-chart',
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020061];
62
63function handleCandidateNode(node) {
64 if (typeof node.classList !== 'undefined') {
65 if (('tagName' in node) && node.tagName == 'EC-APP') {
66 // Set up the intersectionObserver
67 if (typeof intersectionObserver === 'undefined') {
68 var scrollableContent = node.querySelector('.scrollable-content');
69 if (scrollableContent !== null) {
70 intersectionOptions = {
71 root: scrollableContent,
72 rootMargin: '0px',
73 threshold: 1.0,
74 };
75
76 intersectionObserver = new IntersectionObserver(
77 intersectionCallback, intersectionOptions);
78 }
79 }
80
81 // Inject the dark mode button
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020082 // TODO(avm99963): make this feature dynamic.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020083 if (options.ccdarktheme && options.ccdarktheme_mode == 'switch') {
84 var rightControl = node.querySelector('header .right-control');
85 if (rightControl !== null)
86 injectDarkModeButton(rightControl, options.ccdarktheme_switch_status);
87 }
88 }
89
90 // Start the intersectionObserver for the "load more"/"load all" buttons
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020091 // inside a thread if the option is currently enabled.
92 if (node.classList.contains('load-more-bar')) {
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +020093 if (typeof intersectionObserver !== 'undefined') {
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020094 getOptions(['thread', 'threadall']).then(threadOptions => {
95 if (threadOptions.thread)
96 intersectionObserver.observe(
97 node.querySelector('.load-more-button'));
98 if (threadOptions.threadall)
99 intersectionObserver.observe(
100 node.querySelector('.load-all-button'));
101 });
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200102 } else {
103 console.warn(
104 '[infinitescroll] ' +
105 'The intersectionObserver is not ready yet.');
106 }
107 }
108
Adrià Vilanova Martínez7e8796c2022-01-23 21:46:46 +0100109 // Show additional details in the profile view.
110 if (node.matches('ec-unified-user .scTailwindUser_profileUsercardmain')) {
111 window.TWPTExtraInfo.injectAtProfileIfEnabled(node);
112 }
113
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200114 // Show the "previous posts" links if the option is currently enabled.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200115 // Here we're selecting the 'ec-user > div' element (unique child)
Adrià Vilanova Martínez1f652522021-10-14 00:23:23 +0200116 if (node.matches(
117 'ec-unified-user .scTailwindUser_profileUsercarddetails')) {
118 injectPreviousPostsLinksUnifiedProfileIfEnabled(
119 /* isCommunityConsole = */ true);
120 }
121
Adrià Vilanova Martínezeebc0ac2022-01-05 14:45:53 +0100122 // #!if ['chromium', 'chromium_mv3'].includes(browser_target)
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200123 // Fix the drag&drop issue with the rich text editor if the option is
124 // currently enabled.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200125 //
126 // We target both tags because in different contexts different
127 // elements containing the text editor get added to the DOM structure.
128 // Sometimes it's a EC-MOVABLE-DIALOG which already contains the
129 // EC-RICH-TEXT-EDITOR, and sometimes it's the EC-RICH-TEXT-EDITOR
130 // directly.
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200131 if (('tagName' in node) &&
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200132 (node.tagName == 'EC-MOVABLE-DIALOG' ||
133 node.tagName == 'EC-RICH-TEXT-EDITOR')) {
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200134 applyDragAndDropFixIfEnabled(node);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200135 }
Adrià Vilanova Martínezeebc0ac2022-01-05 14:45:53 +0100136 // #!endif
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200137
Adrià Vilanova Martínez1e10d192021-12-31 16:01:13 +0100138 // Inject the batch lock and workflow buttons in the thread list if the
139 // corresponding options are currently enabled.
140 // The order is the inverse because the first one will be shown last.
Adrià Vilanova Martínez7e8796c2022-01-23 21:46:46 +0100141 if (batchLock.shouldAddButton(node)) batchLock.addButtonIfEnabled(node);
Adrià Vilanova Martínez1e10d192021-12-31 16:01:13 +0100142
143 if (workflows.shouldAddThreadListBtn(node))
144 workflows.addThreadListBtnIfEnabled(node);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200145
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200146 // Inject avatar links to threads in the thread list. injectIfEnabled is
147 // responsible of determining whether it should run or not depending on its
148 // current setting.
149 if (('tagName' in node) && (node.tagName == 'LI') &&
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200150 node.querySelector('ec-thread-summary') !== null) {
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200151 avatars.injectIfEnabled(node);
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200152 }
153
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200154 // Set up the autorefresh list feature. The setUp function is responsible
155 // of determining whether it should run or not depending on the current
156 // setting.
157 if (('tagName' in node) && node.tagName == 'EC-THREAD-LIST') {
avm99963fd222672021-08-12 23:23:01 +0200158 window.TWPTAutoRefresh.setUp();
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200159 }
160
161 // Redirect unified profile iframe to dark version if applicable
162 if (node.tagName == 'IFRAME' && isDarkThemeOn(options) &&
163 unifiedProfilesFix.checkIframe(node)) {
164 unifiedProfilesFix.fixIframe(node);
165 }
Adrià Vilanova Martínez74a25202022-01-23 23:36:58 +0100166
167 // Show additional details in the canned responses view.
168 if (node.matches('ec-canned-response-row .tags')) {
169 window.TWPTExtraInfo.injectAtCRIfEnabled(node, /* isExpanded = */ false);
170 }
171 if (node.matches('ec-canned-response-row .main .toolbelt')) {
172 const tags = node.parentNode?.querySelector?.('.tags');
Adrià Vilanova Martínez6e4a68d2022-01-24 21:44:32 +0100173 if (tags)
174 window.TWPTExtraInfo.injectAtCRIfEnabled(tags, /* isExpanded = */ true);
175 }
176
177 // Show additional details in the thread view.
178 if (node.matches('ec-thread div[role="list"]')) {
179 const question = node.querySelector('ec-question');
180 if (question) window.TWPTExtraInfo.injectAtQuestionIfEnabled(question);
181 }
182 if (node.matches('ec-thread ec-message')) {
183 window.TWPTExtraInfo.injectAtMessageIfEnabled(node);
Adrià Vilanova Martínez74a25202022-01-23 23:36:58 +0100184 }
Adrià Vilanova Martínez4f56d562022-01-26 00:23:27 +0100185
186 // Inject per-forum stats section in the user profile
187 if (node.matches(
188 'ec-unified-user .scTailwindUser_profileUserprofilesection ' +
189 'sc-tailwind-shared-activity-chart')) {
190 window.TWPTExtraInfo.injectPerForumStatsIfEnabled(node);
191 }
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200192 }
193}
194
195function handleRemovedNode(node) {
196 // Remove snackbar when exiting thread list view
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200197 if ('tagName' in node && node.tagName == 'EC-THREAD-LIST') {
avm99963fd222672021-08-12 23:23:01 +0200198 window.TWPTAutoRefresh.hideUpdatePrompt();
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200199 }
200}
201
202function mutationCallback(mutationList, observer) {
203 mutationList.forEach((mutation) => {
204 if (mutation.type == 'childList') {
205 mutation.addedNodes.forEach(function(node) {
206 handleCandidateNode(node);
207 });
208
209 mutation.removedNodes.forEach(function(node) {
210 handleRemovedNode(node);
211 });
212 }
213 });
214}
215
216function intersectionCallback(entries, observer) {
217 entries.forEach(entry => {
218 if (entry.isIntersecting) {
219 entry.target.click();
220 }
221 });
222};
223
224var observerOptions = {
225 childList: true,
226 subtree: true,
227};
228
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200229getOptions(null).then(items => {
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200230 options = items;
231
Adrià Vilanova Martínez27c69962021-07-17 23:32:51 +0200232 // Initialize classes needed by the mutation observer
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200233 avatars = new AvatarsHandler();
Adrià Vilanova Martínez1e10d192021-12-31 16:01:13 +0100234 workflows = new Workflows();
Adrià Vilanova Martínez27c69962021-07-17 23:32:51 +0200235
avm99963b6f68b62021-08-12 23:13:06 +0200236 // autoRefresh is initialized in start.js
avm99963d3f4ac02021-08-12 18:36:58 +0200237
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200238 // Before starting the mutation Observer, check whether we missed any
239 // mutations by manually checking whether some watched nodes already
240 // exist.
241 var cssSelectors = watchedNodesSelectors.join(',');
242 document.querySelectorAll(cssSelectors)
243 .forEach(node => handleCandidateNode(node));
244
245 mutationObserver = new MutationObserver(mutationCallback);
246 mutationObserver.observe(document.body, observerOptions);
247
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200248 // TODO(avm99963): The following features are not dynamic. Make them be.
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200249 if (options.fixedtoolbar) {
250 injectStyles(
251 'ec-bulk-actions{position: sticky; top: 0; background: var(--TWPT-primary-background, #fff); z-index: 96;}');
252 }
253
254 if (options.increasecontrast) {
255 injectStyles(
256 '.thread-summary.read:not(.checked){background: var(--TWPT-thread-read-background, #ecedee)!important;}');
257 }
258
259 if (options.stickysidebarheaders) {
260 injectStyles(
261 'material-drawer .main-header{background: var(--TWPT-drawer-background, #fff)!important; position: sticky; top: 0; z-index: 1;}');
262 }
263
264 if (options.enhancedannouncementsdot) {
265 injectStylesheet(
266 chrome.runtime.getURL('css/enhanced_announcements_dot.css'));
267 }
268
269 if (options.repositionexpandthread) {
Adrià Vilanova Martínez27c69962021-07-17 23:32:51 +0200270 injectStylesheet(chrome.runtime.getURL('css/reposition_expand_thread.css'));
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200271 }
272
Adrià Vilanova Martínez9d27c212021-12-05 13:54:10 +0100273 if (options.imagemaxheight) {
274 injectStylesheet(chrome.runtime.getURL('css/image_max_height.css'));
275 }
276
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200277 if (options.ccforcehidedrawer) {
278 var drawer = document.querySelector('material-drawer');
279 if (drawer !== null && drawer.classList.contains('mat-drawer-expanded')) {
280 document.querySelector('.material-drawer-button').click();
281 }
282 }
283
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +0200284 // Batch lock
285 injectScript(chrome.runtime.getURL('batchLockInject.bundle.js'));
286 injectStylesheet(chrome.runtime.getURL('css/batchlock_inject.css'));
287 // Thread list avatars
288 injectStylesheet(chrome.runtime.getURL('css/thread_list_avatars.css'));
289 // Auto refresh list
290 injectStylesheet(chrome.runtime.getURL('css/autorefresh_list.css'));
Adrià Vilanova Martínez7e8796c2022-01-23 21:46:46 +0100291 // Extra info
292 injectStylesheet(chrome.runtime.getURL('css/extrainfo.css'));
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +0200293});