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