Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 1 | import {injectScript, injectStyles, injectStylesheet} from '../../common/contentScriptsUtils.js'; |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 2 | import {getOptions, isOptionEnabled} from '../../common/optionsUtils.js'; |
Adrià Vilanova Martínez | 1f65252 | 2021-10-14 00:23:23 +0200 | [diff] [blame] | 3 | import {injectPreviousPostsLinksUnifiedProfileIfEnabled} from '../utilsCommon/unifiedProfiles.js'; |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 4 | |
Adrià Vilanova Martínez | 27c6996 | 2021-07-17 23:32:51 +0200 | [diff] [blame] | 5 | import AvatarsHandler from './avatars.js'; |
Adrià Vilanova Martínez | 462280f | 2021-08-07 22:59:02 +0200 | [diff] [blame] | 6 | import {batchLock} from './batchLock.js'; |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 7 | import {injectDarkModeButton, isDarkThemeOn} from './darkMode.js'; |
Adrià Vilanova Martínez | eebc0ac | 2022-01-05 14:45:53 +0100 | [diff] [blame] | 8 | // #!if ['chromium', 'chromium_mv3'].includes(browser_target) |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 9 | import {applyDragAndDropFixIfEnabled} from './dragAndDropFix.js'; |
Adrià Vilanova Martínez | eebc0ac | 2022-01-05 14:45:53 +0100 | [diff] [blame] | 10 | // #!endif |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 11 | import {unifiedProfilesFix} from './unifiedProfiles.js'; |
Adrià Vilanova Martínez | 1e10d19 | 2021-12-31 16:01:13 +0100 | [diff] [blame] | 12 | import Workflows from './workflows/workflows.js'; |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 13 | |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 14 | var mutationObserver, intersectionObserver, intersectionOptions, options, |
Adrià Vilanova Martínez | 1e10d19 | 2021-12-31 16:01:13 +0100 | [diff] [blame] | 15 | avatars, workflows; |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 16 | |
| 17 | const 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ínez | 7e8796c | 2022-01-23 21:46:46 +0100 | [diff] [blame] | 25 | // User profile card inside ec-unified-user |
| 26 | 'ec-unified-user .scTailwindUser_profileUsercardmain', |
| 27 | |
Adrià Vilanova Martínez | 531cd07 | 2021-12-05 20:15:43 +0100 | [diff] [blame] | 28 | // Username span/editor inside ec-unified-user (user profile view) |
Adrià Vilanova Martínez | 1f65252 | 2021-10-14 00:23:23 +0200 | [diff] [blame] | 29 | 'ec-unified-user .scTailwindUser_profileUsercarddetails', |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 30 | |
| 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ínez | 74a2520 | 2022-01-23 23:36:58 +0100 | [diff] [blame] | 47 | |
| 48 | // Canned response tags or toolbelt (for the extra info feature) |
Adrià Vilanova Martínez | 6e4a68d | 2022-01-24 21:44:32 +0100 | [diff] [blame] | 49 | '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ínez | 4f56d56 | 2022-01-26 00:23:27 +0100 | [diff] [blame] | 57 | |
| 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ínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 61 | ]; |
| 62 | |
| 63 | function 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ínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 82 | // TODO(avm99963): make this feature dynamic. |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 83 | 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ínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 91 | // inside a thread if the option is currently enabled. |
| 92 | if (node.classList.contains('load-more-bar')) { |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 93 | if (typeof intersectionObserver !== 'undefined') { |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 94 | 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ínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 102 | } else { |
| 103 | console.warn( |
| 104 | '[infinitescroll] ' + |
| 105 | 'The intersectionObserver is not ready yet.'); |
| 106 | } |
| 107 | } |
| 108 | |
Adrià Vilanova Martínez | 7e8796c | 2022-01-23 21:46:46 +0100 | [diff] [blame] | 109 | // 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ínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 114 | // Show the "previous posts" links if the option is currently enabled. |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 115 | // Here we're selecting the 'ec-user > div' element (unique child) |
Adrià Vilanova Martínez | 1f65252 | 2021-10-14 00:23:23 +0200 | [diff] [blame] | 116 | if (node.matches( |
| 117 | 'ec-unified-user .scTailwindUser_profileUsercarddetails')) { |
| 118 | injectPreviousPostsLinksUnifiedProfileIfEnabled( |
| 119 | /* isCommunityConsole = */ true); |
| 120 | } |
| 121 | |
Adrià Vilanova Martínez | eebc0ac | 2022-01-05 14:45:53 +0100 | [diff] [blame] | 122 | // #!if ['chromium', 'chromium_mv3'].includes(browser_target) |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 123 | // Fix the drag&drop issue with the rich text editor if the option is |
| 124 | // currently enabled. |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 125 | // |
| 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ínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 131 | if (('tagName' in node) && |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 132 | (node.tagName == 'EC-MOVABLE-DIALOG' || |
| 133 | node.tagName == 'EC-RICH-TEXT-EDITOR')) { |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 134 | applyDragAndDropFixIfEnabled(node); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 135 | } |
Adrià Vilanova Martínez | eebc0ac | 2022-01-05 14:45:53 +0100 | [diff] [blame] | 136 | // #!endif |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 137 | |
Adrià Vilanova Martínez | 1e10d19 | 2021-12-31 16:01:13 +0100 | [diff] [blame] | 138 | // 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ínez | 7e8796c | 2022-01-23 21:46:46 +0100 | [diff] [blame] | 141 | if (batchLock.shouldAddButton(node)) batchLock.addButtonIfEnabled(node); |
Adrià Vilanova Martínez | 1e10d19 | 2021-12-31 16:01:13 +0100 | [diff] [blame] | 142 | |
| 143 | if (workflows.shouldAddThreadListBtn(node)) |
| 144 | workflows.addThreadListBtnIfEnabled(node); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 145 | |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 146 | // 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ínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 150 | node.querySelector('ec-thread-summary') !== null) { |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 151 | avatars.injectIfEnabled(node); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 152 | } |
| 153 | |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 154 | // 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') { |
avm99963 | fd22267 | 2021-08-12 23:23:01 +0200 | [diff] [blame] | 158 | window.TWPTAutoRefresh.setUp(); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 159 | } |
| 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ínez | 74a2520 | 2022-01-23 23:36:58 +0100 | [diff] [blame] | 166 | |
| 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ínez | 6e4a68d | 2022-01-24 21:44:32 +0100 | [diff] [blame] | 173 | 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ínez | 74a2520 | 2022-01-23 23:36:58 +0100 | [diff] [blame] | 184 | } |
Adrià Vilanova Martínez | 4f56d56 | 2022-01-26 00:23:27 +0100 | [diff] [blame] | 185 | |
| 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ínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
| 195 | function handleRemovedNode(node) { |
| 196 | // Remove snackbar when exiting thread list view |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 197 | if ('tagName' in node && node.tagName == 'EC-THREAD-LIST') { |
avm99963 | fd22267 | 2021-08-12 23:23:01 +0200 | [diff] [blame] | 198 | window.TWPTAutoRefresh.hideUpdatePrompt(); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | |
| 202 | function 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 | |
| 216 | function intersectionCallback(entries, observer) { |
| 217 | entries.forEach(entry => { |
| 218 | if (entry.isIntersecting) { |
| 219 | entry.target.click(); |
| 220 | } |
| 221 | }); |
| 222 | }; |
| 223 | |
| 224 | var observerOptions = { |
| 225 | childList: true, |
| 226 | subtree: true, |
| 227 | }; |
| 228 | |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 229 | getOptions(null).then(items => { |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 230 | options = items; |
| 231 | |
Adrià Vilanova Martínez | 27c6996 | 2021-07-17 23:32:51 +0200 | [diff] [blame] | 232 | // Initialize classes needed by the mutation observer |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 233 | avatars = new AvatarsHandler(); |
Adrià Vilanova Martínez | 1e10d19 | 2021-12-31 16:01:13 +0100 | [diff] [blame] | 234 | workflows = new Workflows(); |
Adrià Vilanova Martínez | 27c6996 | 2021-07-17 23:32:51 +0200 | [diff] [blame] | 235 | |
avm99963 | b6f68b6 | 2021-08-12 23:13:06 +0200 | [diff] [blame] | 236 | // autoRefresh is initialized in start.js |
avm99963 | d3f4ac0 | 2021-08-12 18:36:58 +0200 | [diff] [blame] | 237 | |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 238 | // 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ínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 248 | // TODO(avm99963): The following features are not dynamic. Make them be. |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 249 | 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ínez | 27c6996 | 2021-07-17 23:32:51 +0200 | [diff] [blame] | 270 | injectStylesheet(chrome.runtime.getURL('css/reposition_expand_thread.css')); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 271 | } |
| 272 | |
Adrià Vilanova Martínez | 9d27c21 | 2021-12-05 13:54:10 +0100 | [diff] [blame] | 273 | if (options.imagemaxheight) { |
| 274 | injectStylesheet(chrome.runtime.getURL('css/image_max_height.css')); |
| 275 | } |
| 276 | |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 277 | 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ínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 284 | // 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ínez | 7e8796c | 2022-01-23 21:46:46 +0100 | [diff] [blame] | 291 | // Extra info |
| 292 | injectStylesheet(chrome.runtime.getURL('css/extrainfo.css')); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 293 | }); |