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 | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 57 | ]; |
| 58 | |
| 59 | function handleCandidateNode(node) { |
| 60 | if (typeof node.classList !== 'undefined') { |
| 61 | if (('tagName' in node) && node.tagName == 'EC-APP') { |
| 62 | // Set up the intersectionObserver |
| 63 | if (typeof intersectionObserver === 'undefined') { |
| 64 | var scrollableContent = node.querySelector('.scrollable-content'); |
| 65 | if (scrollableContent !== null) { |
| 66 | intersectionOptions = { |
| 67 | root: scrollableContent, |
| 68 | rootMargin: '0px', |
| 69 | threshold: 1.0, |
| 70 | }; |
| 71 | |
| 72 | intersectionObserver = new IntersectionObserver( |
| 73 | intersectionCallback, intersectionOptions); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // Inject the dark mode button |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 78 | // TODO(avm99963): make this feature dynamic. |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 79 | if (options.ccdarktheme && options.ccdarktheme_mode == 'switch') { |
| 80 | var rightControl = node.querySelector('header .right-control'); |
| 81 | if (rightControl !== null) |
| 82 | injectDarkModeButton(rightControl, options.ccdarktheme_switch_status); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | // Start the intersectionObserver for the "load more"/"load all" buttons |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 87 | // inside a thread if the option is currently enabled. |
| 88 | if (node.classList.contains('load-more-bar')) { |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 89 | if (typeof intersectionObserver !== 'undefined') { |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 90 | getOptions(['thread', 'threadall']).then(threadOptions => { |
| 91 | if (threadOptions.thread) |
| 92 | intersectionObserver.observe( |
| 93 | node.querySelector('.load-more-button')); |
| 94 | if (threadOptions.threadall) |
| 95 | intersectionObserver.observe( |
| 96 | node.querySelector('.load-all-button')); |
| 97 | }); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 98 | } else { |
| 99 | console.warn( |
| 100 | '[infinitescroll] ' + |
| 101 | 'The intersectionObserver is not ready yet.'); |
| 102 | } |
| 103 | } |
| 104 | |
Adrià Vilanova Martínez | 7e8796c | 2022-01-23 21:46:46 +0100 | [diff] [blame] | 105 | // Show additional details in the profile view. |
| 106 | if (node.matches('ec-unified-user .scTailwindUser_profileUsercardmain')) { |
| 107 | window.TWPTExtraInfo.injectAtProfileIfEnabled(node); |
| 108 | } |
| 109 | |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 110 | // 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] | 111 | // 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] | 112 | if (node.matches( |
| 113 | 'ec-unified-user .scTailwindUser_profileUsercarddetails')) { |
| 114 | injectPreviousPostsLinksUnifiedProfileIfEnabled( |
| 115 | /* isCommunityConsole = */ true); |
| 116 | } |
| 117 | |
Adrià Vilanova Martínez | eebc0ac | 2022-01-05 14:45:53 +0100 | [diff] [blame] | 118 | // #!if ['chromium', 'chromium_mv3'].includes(browser_target) |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 119 | // Fix the drag&drop issue with the rich text editor if the option is |
| 120 | // currently enabled. |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 121 | // |
| 122 | // We target both tags because in different contexts different |
| 123 | // elements containing the text editor get added to the DOM structure. |
| 124 | // Sometimes it's a EC-MOVABLE-DIALOG which already contains the |
| 125 | // EC-RICH-TEXT-EDITOR, and sometimes it's the EC-RICH-TEXT-EDITOR |
| 126 | // directly. |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 127 | if (('tagName' in node) && |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 128 | (node.tagName == 'EC-MOVABLE-DIALOG' || |
| 129 | node.tagName == 'EC-RICH-TEXT-EDITOR')) { |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 130 | applyDragAndDropFixIfEnabled(node); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 131 | } |
Adrià Vilanova Martínez | eebc0ac | 2022-01-05 14:45:53 +0100 | [diff] [blame] | 132 | // #!endif |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 133 | |
Adrià Vilanova Martínez | 1e10d19 | 2021-12-31 16:01:13 +0100 | [diff] [blame] | 134 | // Inject the batch lock and workflow buttons in the thread list if the |
| 135 | // corresponding options are currently enabled. |
| 136 | // 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] | 137 | if (batchLock.shouldAddButton(node)) batchLock.addButtonIfEnabled(node); |
Adrià Vilanova Martínez | 1e10d19 | 2021-12-31 16:01:13 +0100 | [diff] [blame] | 138 | |
| 139 | if (workflows.shouldAddThreadListBtn(node)) |
| 140 | workflows.addThreadListBtnIfEnabled(node); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 141 | |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 142 | // Inject avatar links to threads in the thread list. injectIfEnabled is |
| 143 | // responsible of determining whether it should run or not depending on its |
| 144 | // current setting. |
| 145 | if (('tagName' in node) && (node.tagName == 'LI') && |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 146 | node.querySelector('ec-thread-summary') !== null) { |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 147 | avatars.injectIfEnabled(node); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 148 | } |
| 149 | |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 150 | // Set up the autorefresh list feature. The setUp function is responsible |
| 151 | // of determining whether it should run or not depending on the current |
| 152 | // setting. |
| 153 | if (('tagName' in node) && node.tagName == 'EC-THREAD-LIST') { |
avm99963 | fd22267 | 2021-08-12 23:23:01 +0200 | [diff] [blame] | 154 | window.TWPTAutoRefresh.setUp(); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | // Redirect unified profile iframe to dark version if applicable |
| 158 | if (node.tagName == 'IFRAME' && isDarkThemeOn(options) && |
| 159 | unifiedProfilesFix.checkIframe(node)) { |
| 160 | unifiedProfilesFix.fixIframe(node); |
| 161 | } |
Adrià Vilanova Martínez | 74a2520 | 2022-01-23 23:36:58 +0100 | [diff] [blame] | 162 | |
| 163 | // Show additional details in the canned responses view. |
| 164 | if (node.matches('ec-canned-response-row .tags')) { |
| 165 | window.TWPTExtraInfo.injectAtCRIfEnabled(node, /* isExpanded = */ false); |
| 166 | } |
| 167 | if (node.matches('ec-canned-response-row .main .toolbelt')) { |
| 168 | const tags = node.parentNode?.querySelector?.('.tags'); |
Adrià Vilanova Martínez | 6e4a68d | 2022-01-24 21:44:32 +0100 | [diff] [blame] | 169 | if (tags) |
| 170 | window.TWPTExtraInfo.injectAtCRIfEnabled(tags, /* isExpanded = */ true); |
| 171 | } |
| 172 | |
| 173 | // Show additional details in the thread view. |
| 174 | if (node.matches('ec-thread div[role="list"]')) { |
| 175 | const question = node.querySelector('ec-question'); |
| 176 | if (question) window.TWPTExtraInfo.injectAtQuestionIfEnabled(question); |
| 177 | } |
| 178 | if (node.matches('ec-thread ec-message')) { |
| 179 | window.TWPTExtraInfo.injectAtMessageIfEnabled(node); |
Adrià Vilanova Martínez | 74a2520 | 2022-01-23 23:36:58 +0100 | [diff] [blame] | 180 | } |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
| 184 | function handleRemovedNode(node) { |
| 185 | // Remove snackbar when exiting thread list view |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 186 | if ('tagName' in node && node.tagName == 'EC-THREAD-LIST') { |
avm99963 | fd22267 | 2021-08-12 23:23:01 +0200 | [diff] [blame] | 187 | window.TWPTAutoRefresh.hideUpdatePrompt(); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
| 191 | function mutationCallback(mutationList, observer) { |
| 192 | mutationList.forEach((mutation) => { |
| 193 | if (mutation.type == 'childList') { |
| 194 | mutation.addedNodes.forEach(function(node) { |
| 195 | handleCandidateNode(node); |
| 196 | }); |
| 197 | |
| 198 | mutation.removedNodes.forEach(function(node) { |
| 199 | handleRemovedNode(node); |
| 200 | }); |
| 201 | } |
| 202 | }); |
| 203 | } |
| 204 | |
| 205 | function intersectionCallback(entries, observer) { |
| 206 | entries.forEach(entry => { |
| 207 | if (entry.isIntersecting) { |
| 208 | entry.target.click(); |
| 209 | } |
| 210 | }); |
| 211 | }; |
| 212 | |
| 213 | var observerOptions = { |
| 214 | childList: true, |
| 215 | subtree: true, |
| 216 | }; |
| 217 | |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 218 | getOptions(null).then(items => { |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 219 | options = items; |
| 220 | |
Adrià Vilanova Martínez | 27c6996 | 2021-07-17 23:32:51 +0200 | [diff] [blame] | 221 | // Initialize classes needed by the mutation observer |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 222 | avatars = new AvatarsHandler(); |
Adrià Vilanova Martínez | 1e10d19 | 2021-12-31 16:01:13 +0100 | [diff] [blame] | 223 | workflows = new Workflows(); |
Adrià Vilanova Martínez | 27c6996 | 2021-07-17 23:32:51 +0200 | [diff] [blame] | 224 | |
avm99963 | b6f68b6 | 2021-08-12 23:13:06 +0200 | [diff] [blame] | 225 | // autoRefresh is initialized in start.js |
avm99963 | d3f4ac0 | 2021-08-12 18:36:58 +0200 | [diff] [blame] | 226 | |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 227 | // Before starting the mutation Observer, check whether we missed any |
| 228 | // mutations by manually checking whether some watched nodes already |
| 229 | // exist. |
| 230 | var cssSelectors = watchedNodesSelectors.join(','); |
| 231 | document.querySelectorAll(cssSelectors) |
| 232 | .forEach(node => handleCandidateNode(node)); |
| 233 | |
| 234 | mutationObserver = new MutationObserver(mutationCallback); |
| 235 | mutationObserver.observe(document.body, observerOptions); |
| 236 | |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 237 | // 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] | 238 | if (options.fixedtoolbar) { |
| 239 | injectStyles( |
| 240 | 'ec-bulk-actions{position: sticky; top: 0; background: var(--TWPT-primary-background, #fff); z-index: 96;}'); |
| 241 | } |
| 242 | |
| 243 | if (options.increasecontrast) { |
| 244 | injectStyles( |
| 245 | '.thread-summary.read:not(.checked){background: var(--TWPT-thread-read-background, #ecedee)!important;}'); |
| 246 | } |
| 247 | |
| 248 | if (options.stickysidebarheaders) { |
| 249 | injectStyles( |
| 250 | 'material-drawer .main-header{background: var(--TWPT-drawer-background, #fff)!important; position: sticky; top: 0; z-index: 1;}'); |
| 251 | } |
| 252 | |
| 253 | if (options.enhancedannouncementsdot) { |
| 254 | injectStylesheet( |
| 255 | chrome.runtime.getURL('css/enhanced_announcements_dot.css')); |
| 256 | } |
| 257 | |
| 258 | if (options.repositionexpandthread) { |
Adrià Vilanova Martínez | 27c6996 | 2021-07-17 23:32:51 +0200 | [diff] [blame] | 259 | injectStylesheet(chrome.runtime.getURL('css/reposition_expand_thread.css')); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 260 | } |
| 261 | |
Adrià Vilanova Martínez | 9d27c21 | 2021-12-05 13:54:10 +0100 | [diff] [blame] | 262 | if (options.imagemaxheight) { |
| 263 | injectStylesheet(chrome.runtime.getURL('css/image_max_height.css')); |
| 264 | } |
| 265 | |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 266 | if (options.ccforcehidedrawer) { |
| 267 | var drawer = document.querySelector('material-drawer'); |
| 268 | if (drawer !== null && drawer.classList.contains('mat-drawer-expanded')) { |
| 269 | document.querySelector('.material-drawer-button').click(); |
| 270 | } |
| 271 | } |
| 272 | |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 273 | // Batch lock |
| 274 | injectScript(chrome.runtime.getURL('batchLockInject.bundle.js')); |
| 275 | injectStylesheet(chrome.runtime.getURL('css/batchlock_inject.css')); |
| 276 | // Thread list avatars |
| 277 | injectStylesheet(chrome.runtime.getURL('css/thread_list_avatars.css')); |
| 278 | // Auto refresh list |
| 279 | injectStylesheet(chrome.runtime.getURL('css/autorefresh_list.css')); |
Adrià Vilanova Martínez | 7e8796c | 2022-01-23 21:46:46 +0100 | [diff] [blame] | 280 | // Extra info |
| 281 | injectStylesheet(chrome.runtime.getURL('css/extrainfo.css')); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 282 | }); |