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