Adrià Vilanova Martínez | d7951b2 | 2024-05-25 22:13:32 +0200 | [diff] [blame] | 1 | import {injectScript, injectStyles, injectStylesheet} from '../../common/contentScriptsUtils'; |
Adrià Vilanova Martínez | b523be9 | 2024-05-25 19:14:19 +0200 | [diff] [blame] | 2 | import {getOptions} from '../../common/options/optionsUtils.js'; |
Adrià Vilanova Martínez | 825888a | 2023-07-23 01:53:48 +0200 | [diff] [blame] | 3 | import XHRProxyKillSwitchHandler from '../../xhrInterceptor/killSwitchHandler.js'; |
Adrià Vilanova Martínez | 1f65252 | 2021-10-14 00:23:23 +0200 | [diff] [blame] | 4 | import {injectPreviousPostsLinksUnifiedProfileIfEnabled} from '../utilsCommon/unifiedProfiles.js'; |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 5 | |
Adrià Vilanova Martínez | 27c6996 | 2021-07-17 23:32:51 +0200 | [diff] [blame] | 6 | import AvatarsHandler from './avatars.js'; |
Adrià Vilanova Martínez | 462280f | 2021-08-07 22:59:02 +0200 | [diff] [blame] | 7 | import {batchLock} from './batchLock.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 | 115e3d8 | 2023-01-10 21:50:06 +0100 | [diff] [blame] | 11 | import {default as FlattenThreads, kMatchingSelectors as kFlattenThreadMatchingSelectors} from './flattenThreads/flattenThreads.js'; |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 12 | import {kRepliesSectionSelector} from './threadToolbar/constants.js'; |
| 13 | import ThreadToolbar from './threadToolbar/threadToolbar.js'; |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 14 | |
Adrià Vilanova Martínez | 917797e | 2024-05-25 22:41:25 +0200 | [diff] [blame] | 15 | var mutationObserver, options, avatars, threadToolbar, flattenThreads; |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 16 | |
| 17 | const watchedNodesSelectors = [ |
Adrià Vilanova Martínez | a7ae3db | 2022-01-28 11:57:27 +0100 | [diff] [blame] | 18 | // Scrollable content (used for the intersection observer) |
| 19 | '.scrollable-content', |
| 20 | |
| 21 | // Load more bar and buttons |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 22 | '.load-more-bar', |
Adrià Vilanova Martínez | a7ae3db | 2022-01-28 11:57:27 +0100 | [diff] [blame] | 23 | '.scTailwindThreadMorebuttonbutton', |
Adrià Vilanova Martínez | 8885497 | 2022-08-28 11:57:12 +0200 | [diff] [blame] | 24 | '.scTailwindThreadMessagegapbutton', |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 25 | |
Adrià Vilanova Martínez | 7e8796c | 2022-01-23 21:46:46 +0100 | [diff] [blame] | 26 | // User profile card inside ec-unified-user |
| 27 | 'ec-unified-user .scTailwindUser_profileUsercardmain', |
| 28 | |
Adrià Vilanova Martínez | 531cd07 | 2021-12-05 20:15:43 +0100 | [diff] [blame] | 29 | // Username span/editor inside ec-unified-user (user profile view) |
Adrià Vilanova Martínez | 1f65252 | 2021-10-14 00:23:23 +0200 | [diff] [blame] | 30 | 'ec-unified-user .scTailwindUser_profileUsercarddetails', |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 31 | |
| 32 | // Rich text editor |
| 33 | 'ec-movable-dialog', |
| 34 | 'ec-rich-text-editor', |
| 35 | |
| 36 | // Read/unread bulk action in the list of thread, for the batch lock feature |
| 37 | 'ec-bulk-actions material-button[debugid="mark-read-button"]', |
| 38 | 'ec-bulk-actions material-button[debugid="mark-unread-button"]', |
| 39 | |
Adrià Vilanova Martínez | 66836fd | 2024-05-04 22:43:06 +0200 | [diff] [blame] | 40 | // Thread list items (used to inject the avatars) |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 41 | 'li', |
| 42 | |
| 43 | // Thread list (used for the autorefresh feature) |
| 44 | 'ec-thread-list', |
| 45 | |
Adrià Vilanova | 61f782f | 2024-05-31 22:57:24 +0000 | [diff] [blame] | 46 | // Thread page main content |
| 47 | 'ec-thread > .page > .material-content > div[role="list"]', |
| 48 | |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 49 | // Thread page reply section (for the thread page toolbar) |
| 50 | kRepliesSectionSelector, |
Adrià Vilanova Martínez | 412b758 | 2022-12-30 01:35:30 +0100 | [diff] [blame] | 51 | |
| 52 | // Reply payload (for the flatten threads UI) |
Adrià Vilanova Martínez | 115e3d8 | 2023-01-10 21:50:06 +0100 | [diff] [blame] | 53 | ...kFlattenThreadMatchingSelectors, |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 54 | ]; |
| 55 | |
| 56 | function handleCandidateNode(node) { |
| 57 | if (typeof node.classList !== 'undefined') { |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 58 | // 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] | 59 | // 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] | 60 | if (node.matches( |
| 61 | 'ec-unified-user .scTailwindUser_profileUsercarddetails')) { |
| 62 | injectPreviousPostsLinksUnifiedProfileIfEnabled( |
| 63 | /* isCommunityConsole = */ true); |
| 64 | } |
| 65 | |
Adrià Vilanova Martínez | eebc0ac | 2022-01-05 14:45:53 +0100 | [diff] [blame] | 66 | // #!if ['chromium', 'chromium_mv3'].includes(browser_target) |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 67 | // Fix the drag&drop issue with the rich text editor if the option is |
| 68 | // currently enabled. |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 69 | // |
| 70 | // We target both tags because in different contexts different |
| 71 | // elements containing the text editor get added to the DOM structure. |
| 72 | // Sometimes it's a EC-MOVABLE-DIALOG which already contains the |
| 73 | // EC-RICH-TEXT-EDITOR, and sometimes it's the EC-RICH-TEXT-EDITOR |
| 74 | // directly. |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 75 | if (('tagName' in node) && |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 76 | (node.tagName == 'EC-MOVABLE-DIALOG' || |
| 77 | node.tagName == 'EC-RICH-TEXT-EDITOR')) { |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 78 | applyDragAndDropFixIfEnabled(node); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 79 | } |
Adrià Vilanova Martínez | eebc0ac | 2022-01-05 14:45:53 +0100 | [diff] [blame] | 80 | // #!endif |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 81 | |
Adrià Vilanova Martínez | b06c13d | 2022-06-20 17:30:14 +0000 | [diff] [blame] | 82 | // Inject the batch lock button in the thread list if the option is |
| 83 | // currently enabled. |
Adrià Vilanova Martínez | 4107b5e | 2022-10-09 23:11:11 +0200 | [diff] [blame] | 84 | if (batchLock.shouldAddButton(node)) { |
Adrià Vilanova Martínez | b06c13d | 2022-06-20 17:30:14 +0000 | [diff] [blame] | 85 | batchLock.addButtonIfEnabled(node); |
| 86 | } |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 87 | |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 88 | // Inject avatar links to threads in the thread list. injectIfEnabled is |
| 89 | // responsible of determining whether it should run or not depending on its |
| 90 | // current setting. |
| 91 | if (('tagName' in node) && (node.tagName == 'LI') && |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 92 | node.querySelector('ec-thread-summary') !== null) { |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 93 | avatars.injectIfEnabled(node); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 94 | } |
| 95 | |
Adrià Vilanova | 61f782f | 2024-05-31 22:57:24 +0000 | [diff] [blame] | 96 | // Inject old thread page design warning if applicable |
| 97 | if (node.matches( |
| 98 | 'ec-thread > .page > .material-content > div[role="list"]')) { |
| 99 | window.TWPTThreadPageDesignWarning.injectWarningIfApplicable(node); |
| 100 | } |
| 101 | |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 102 | // Inject thread toolbar |
| 103 | if (threadToolbar.shouldInject(node)) { |
| 104 | threadToolbar.injectIfApplicable(node); |
| 105 | } |
Adrià Vilanova Martínez | 412b758 | 2022-12-30 01:35:30 +0100 | [diff] [blame] | 106 | |
| 107 | // Inject parent reply quote |
Adrià Vilanova Martínez | 115e3d8 | 2023-01-10 21:50:06 +0100 | [diff] [blame] | 108 | if (flattenThreads.shouldInjectQuote(node)) { |
| 109 | flattenThreads.injectQuoteIfApplicable(node); |
| 110 | } |
| 111 | |
| 112 | // Inject reply button in non-nested view |
| 113 | if (flattenThreads.shouldInjectReplyBtn(node)) { |
| 114 | flattenThreads.injectReplyBtnIfApplicable(node); |
Adrià Vilanova Martínez | 412b758 | 2022-12-30 01:35:30 +0100 | [diff] [blame] | 115 | } |
Adrià Vilanova Martínez | 4d59925 | 2023-02-04 20:54:54 +0100 | [diff] [blame] | 116 | |
| 117 | // Delete additional info in the edit message box |
| 118 | if (flattenThreads.isAdditionalInfoElement(node)) { |
| 119 | flattenThreads.deleteAdditionalInfoElementIfApplicable(node); |
| 120 | } |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | |
Adrià Vilanova Martínez | 115e3d8 | 2023-01-10 21:50:06 +0100 | [diff] [blame] | 124 | function handleRemovedNode(mutation, node) { |
| 125 | if (!('tagName' in node)) return; |
| 126 | |
Adrià Vilanova Martínez | 115e3d8 | 2023-01-10 21:50:06 +0100 | [diff] [blame] | 127 | // Readd reply button when the Community Console removes it |
| 128 | if (node.tagName == 'TWPT-FLATTEN-THREAD-REPLY-BUTTON') { |
| 129 | flattenThreads.injectReplyBtn( |
| 130 | mutation.target, JSON.parse(node.getAttribute('extraInfo'))); |
| 131 | } |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 132 | } |
| 133 | |
Adrià Vilanova Martínez | 115e3d8 | 2023-01-10 21:50:06 +0100 | [diff] [blame] | 134 | function mutationCallback(mutationList) { |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 135 | mutationList.forEach((mutation) => { |
| 136 | if (mutation.type == 'childList') { |
| 137 | mutation.addedNodes.forEach(function(node) { |
| 138 | handleCandidateNode(node); |
| 139 | }); |
| 140 | |
| 141 | mutation.removedNodes.forEach(function(node) { |
Adrià Vilanova Martínez | 115e3d8 | 2023-01-10 21:50:06 +0100 | [diff] [blame] | 142 | handleRemovedNode(mutation, node); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 143 | }); |
| 144 | } |
| 145 | }); |
| 146 | } |
| 147 | |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 148 | var observerOptions = { |
| 149 | childList: true, |
| 150 | subtree: true, |
| 151 | }; |
| 152 | |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 153 | getOptions(null).then(items => { |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 154 | options = items; |
| 155 | |
Adrià Vilanova Martínez | 27c6996 | 2021-07-17 23:32:51 +0200 | [diff] [blame] | 156 | // Initialize classes needed by the mutation observer |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 157 | avatars = new AvatarsHandler(); |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 158 | threadToolbar = new ThreadToolbar(); |
Adrià Vilanova Martínez | 412b758 | 2022-12-30 01:35:30 +0100 | [diff] [blame] | 159 | flattenThreads = new FlattenThreads(); |
Adrià Vilanova Martínez | 27c6996 | 2021-07-17 23:32:51 +0200 | [diff] [blame] | 160 | |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 161 | // Before starting the mutation Observer, check whether we missed any |
| 162 | // mutations by manually checking whether some watched nodes already |
| 163 | // exist. |
| 164 | var cssSelectors = watchedNodesSelectors.join(','); |
| 165 | document.querySelectorAll(cssSelectors) |
| 166 | .forEach(node => handleCandidateNode(node)); |
| 167 | |
| 168 | mutationObserver = new MutationObserver(mutationCallback); |
| 169 | mutationObserver.observe(document.body, observerOptions); |
| 170 | |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 171 | // 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] | 172 | if (options.fixedtoolbar) { |
| 173 | injectStyles( |
Adrià Vilanova | e8ba09a | 2024-02-29 19:25:39 +0000 | [diff] [blame] | 174 | 'ec-bulk-actions{position: sticky; top: 0; background: var(--TWPT-primary-background, #fff); z-index: 96;}'); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | if (options.increasecontrast) { |
| 178 | injectStyles( |
| 179 | '.thread-summary.read:not(.checked){background: var(--TWPT-thread-read-background, #ecedee)!important;}'); |
| 180 | } |
| 181 | |
| 182 | if (options.stickysidebarheaders) { |
| 183 | injectStyles( |
| 184 | 'material-drawer .main-header{background: var(--TWPT-drawer-background, #fff)!important; position: sticky; top: 0; z-index: 1;}'); |
| 185 | } |
| 186 | |
| 187 | if (options.enhancedannouncementsdot) { |
| 188 | injectStylesheet( |
| 189 | chrome.runtime.getURL('css/enhanced_announcements_dot.css')); |
| 190 | } |
| 191 | |
| 192 | if (options.repositionexpandthread) { |
Adrià Vilanova Martínez | 27c6996 | 2021-07-17 23:32:51 +0200 | [diff] [blame] | 193 | injectStylesheet(chrome.runtime.getURL('css/reposition_expand_thread.css')); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 194 | } |
| 195 | |
Adrià Vilanova Martínez | 9d27c21 | 2021-12-05 13:54:10 +0100 | [diff] [blame] | 196 | if (options.imagemaxheight) { |
| 197 | injectStylesheet(chrome.runtime.getURL('css/image_max_height.css')); |
| 198 | } |
| 199 | |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 200 | if (options.ccforcehidedrawer) { |
| 201 | var drawer = document.querySelector('material-drawer'); |
| 202 | if (drawer !== null && drawer.classList.contains('mat-drawer-expanded')) { |
| 203 | document.querySelector('.material-drawer-button').click(); |
| 204 | } |
| 205 | } |
| 206 | |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 207 | // Batch lock |
| 208 | injectScript(chrome.runtime.getURL('batchLockInject.bundle.js')); |
| 209 | injectStylesheet(chrome.runtime.getURL('css/batchlock_inject.css')); |
| 210 | // Thread list avatars |
| 211 | injectStylesheet(chrome.runtime.getURL('css/thread_list_avatars.css')); |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 212 | // Thread toolbar |
| 213 | injectStylesheet(chrome.runtime.getURL('css/thread_toolbar.css')); |
Adrià Vilanova Martínez | 115e3d8 | 2023-01-10 21:50:06 +0100 | [diff] [blame] | 214 | // Flatten threads |
| 215 | injectStylesheet(chrome.runtime.getURL('css/flatten_threads.css')); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 216 | }); |
Adrià Vilanova Martínez | 825888a | 2023-07-23 01:53:48 +0200 | [diff] [blame] | 217 | |
| 218 | new XHRProxyKillSwitchHandler(); |