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