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