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