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 | 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 | a7ae3db | 2022-01-28 11:57:27 +0100 | [diff] [blame] | 11 | import InfiniteScroll from './infiniteScroll.js'; |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 12 | import {unifiedProfilesFix} from './unifiedProfiles.js'; |
Adrià Vilanova Martínez | 1e10d19 | 2021-12-31 16:01:13 +0100 | [diff] [blame] | 13 | import Workflows from './workflows/workflows.js'; |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 14 | |
Adrià Vilanova Martínez | a7ae3db | 2022-01-28 11:57:27 +0100 | [diff] [blame] | 15 | var mutationObserver, options, avatars, infiniteScroll, workflows; |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 16 | |
| 17 | const watchedNodesSelectors = [ |
| 18 | // App container (used to set up the intersection observer and inject the dark |
| 19 | // mode button) |
| 20 | 'ec-app', |
| 21 | |
Adrià Vilanova Martínez | a7ae3db | 2022-01-28 11:57:27 +0100 | [diff] [blame] | 22 | // Scrollable content (used for the intersection observer) |
| 23 | '.scrollable-content', |
| 24 | |
| 25 | // Load more bar and buttons |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 26 | '.load-more-bar', |
Adrià Vilanova Martínez | a7ae3db | 2022-01-28 11:57:27 +0100 | [diff] [blame] | 27 | '.scTailwindThreadMorebuttonbutton', |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 28 | |
Adrià Vilanova Martínez | 7e8796c | 2022-01-23 21:46:46 +0100 | [diff] [blame] | 29 | // User profile card inside ec-unified-user |
| 30 | 'ec-unified-user .scTailwindUser_profileUsercardmain', |
| 31 | |
Adrià Vilanova Martínez | 531cd07 | 2021-12-05 20:15:43 +0100 | [diff] [blame] | 32 | // Username span/editor inside ec-unified-user (user profile view) |
Adrià Vilanova Martínez | 1f65252 | 2021-10-14 00:23:23 +0200 | [diff] [blame] | 33 | 'ec-unified-user .scTailwindUser_profileUsercarddetails', |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 34 | |
| 35 | // Rich text editor |
| 36 | 'ec-movable-dialog', |
| 37 | 'ec-rich-text-editor', |
| 38 | |
| 39 | // Read/unread bulk action in the list of thread, for the batch lock feature |
| 40 | 'ec-bulk-actions material-button[debugid="mark-read-button"]', |
| 41 | 'ec-bulk-actions material-button[debugid="mark-unread-button"]', |
| 42 | |
| 43 | // Thread list items (used to inject the avatars) |
| 44 | 'li', |
| 45 | |
| 46 | // Thread list (used for the autorefresh feature) |
| 47 | 'ec-thread-list', |
| 48 | |
| 49 | // Unified profile iframe |
| 50 | 'iframe', |
Adrià Vilanova Martínez | 74a2520 | 2022-01-23 23:36:58 +0100 | [diff] [blame] | 51 | |
| 52 | // Canned response tags or toolbelt (for the extra info feature) |
Adrià Vilanova Martínez | 6e4a68d | 2022-01-24 21:44:32 +0100 | [diff] [blame] | 53 | 'ec-canned-response-row .tags', |
| 54 | 'ec-canned-response-row .main .toolbelt', |
| 55 | |
| 56 | // Div containing ec-question (for the extra info feature) |
| 57 | 'ec-thread div[role="list"]', |
| 58 | |
| 59 | // Replies (for the extra info feature) |
| 60 | 'ec-thread ec-message', |
Adrià Vilanova Martínez | 4f56d56 | 2022-01-26 00:23:27 +0100 | [diff] [blame] | 61 | |
| 62 | // User activity chart (for the per-forum stats feature) |
| 63 | 'ec-unified-user .scTailwindUser_profileUserprofilesection ' + |
| 64 | 'sc-tailwind-shared-activity-chart', |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 65 | ]; |
| 66 | |
| 67 | function handleCandidateNode(node) { |
| 68 | if (typeof node.classList !== 'undefined') { |
| 69 | if (('tagName' in node) && node.tagName == 'EC-APP') { |
Adrià Vilanova Martínez | a7ae3db | 2022-01-28 11:57:27 +0100 | [diff] [blame] | 70 | infiniteScroll.setUpIntersectionObserver(node, false); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 71 | |
| 72 | // Inject the dark mode button |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 73 | // TODO(avm99963): make this feature dynamic. |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 74 | if (options.ccdarktheme && options.ccdarktheme_mode == 'switch') { |
| 75 | var rightControl = node.querySelector('header .right-control'); |
| 76 | if (rightControl !== null) |
| 77 | injectDarkModeButton(rightControl, options.ccdarktheme_switch_status); |
| 78 | } |
| 79 | } |
| 80 | |
Adrià Vilanova Martínez | a7ae3db | 2022-01-28 11:57:27 +0100 | [diff] [blame] | 81 | // To set up infinite scroll |
| 82 | if (node.classList.contains('scrollable-content')) { |
| 83 | infiniteScroll.setUpIntersectionObserver(node, true); |
| 84 | } |
| 85 | |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 86 | // Start the intersectionObserver for the "load more"/"load all" buttons |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 87 | // inside a thread if the option is currently enabled. |
| 88 | if (node.classList.contains('load-more-bar')) { |
Adrià Vilanova Martínez | a7ae3db | 2022-01-28 11:57:27 +0100 | [diff] [blame] | 89 | infiniteScroll.observeLoadMoreBar(node); |
| 90 | } |
| 91 | if (node.classList.contains('scTailwindThreadMorebuttonbutton')) { |
| 92 | infiniteScroll.observeLoadMoreInteropBtn(node); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 93 | } |
| 94 | |
Adrià Vilanova Martínez | 7e8796c | 2022-01-23 21:46:46 +0100 | [diff] [blame] | 95 | // Show additional details in the profile view. |
| 96 | if (node.matches('ec-unified-user .scTailwindUser_profileUsercardmain')) { |
| 97 | window.TWPTExtraInfo.injectAtProfileIfEnabled(node); |
| 98 | } |
| 99 | |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 100 | // 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] | 101 | // 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] | 102 | if (node.matches( |
| 103 | 'ec-unified-user .scTailwindUser_profileUsercarddetails')) { |
| 104 | injectPreviousPostsLinksUnifiedProfileIfEnabled( |
| 105 | /* isCommunityConsole = */ true); |
| 106 | } |
| 107 | |
Adrià Vilanova Martínez | eebc0ac | 2022-01-05 14:45:53 +0100 | [diff] [blame] | 108 | // #!if ['chromium', 'chromium_mv3'].includes(browser_target) |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 109 | // Fix the drag&drop issue with the rich text editor if the option is |
| 110 | // currently enabled. |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 111 | // |
| 112 | // We target both tags because in different contexts different |
| 113 | // elements containing the text editor get added to the DOM structure. |
| 114 | // Sometimes it's a EC-MOVABLE-DIALOG which already contains the |
| 115 | // EC-RICH-TEXT-EDITOR, and sometimes it's the EC-RICH-TEXT-EDITOR |
| 116 | // directly. |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 117 | if (('tagName' in node) && |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 118 | (node.tagName == 'EC-MOVABLE-DIALOG' || |
| 119 | node.tagName == 'EC-RICH-TEXT-EDITOR')) { |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 120 | applyDragAndDropFixIfEnabled(node); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 121 | } |
Adrià Vilanova Martínez | eebc0ac | 2022-01-05 14:45:53 +0100 | [diff] [blame] | 122 | // #!endif |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 123 | |
Adrià Vilanova Martínez | 1e10d19 | 2021-12-31 16:01:13 +0100 | [diff] [blame] | 124 | // Inject the batch lock and workflow buttons in the thread list if the |
| 125 | // corresponding options are currently enabled. |
| 126 | // The order is the inverse because the first one will be shown last. |
Adrià Vilanova Martínez | 7e8796c | 2022-01-23 21:46:46 +0100 | [diff] [blame] | 127 | if (batchLock.shouldAddButton(node)) batchLock.addButtonIfEnabled(node); |
Adrià Vilanova Martínez | 1e10d19 | 2021-12-31 16:01:13 +0100 | [diff] [blame] | 128 | |
| 129 | if (workflows.shouldAddThreadListBtn(node)) |
| 130 | workflows.addThreadListBtnIfEnabled(node); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 131 | |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 132 | // Inject avatar links to threads in the thread list. injectIfEnabled is |
| 133 | // responsible of determining whether it should run or not depending on its |
| 134 | // current setting. |
| 135 | if (('tagName' in node) && (node.tagName == 'LI') && |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 136 | node.querySelector('ec-thread-summary') !== null) { |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 137 | avatars.injectIfEnabled(node); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 138 | } |
| 139 | |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 140 | // Set up the autorefresh list feature. The setUp function is responsible |
| 141 | // of determining whether it should run or not depending on the current |
| 142 | // setting. |
| 143 | if (('tagName' in node) && node.tagName == 'EC-THREAD-LIST') { |
avm99963 | fd22267 | 2021-08-12 23:23:01 +0200 | [diff] [blame] | 144 | window.TWPTAutoRefresh.setUp(); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | // Redirect unified profile iframe to dark version if applicable |
| 148 | if (node.tagName == 'IFRAME' && isDarkThemeOn(options) && |
| 149 | unifiedProfilesFix.checkIframe(node)) { |
| 150 | unifiedProfilesFix.fixIframe(node); |
| 151 | } |
Adrià Vilanova Martínez | 74a2520 | 2022-01-23 23:36:58 +0100 | [diff] [blame] | 152 | |
| 153 | // Show additional details in the canned responses view. |
| 154 | if (node.matches('ec-canned-response-row .tags')) { |
| 155 | window.TWPTExtraInfo.injectAtCRIfEnabled(node, /* isExpanded = */ false); |
| 156 | } |
| 157 | if (node.matches('ec-canned-response-row .main .toolbelt')) { |
| 158 | const tags = node.parentNode?.querySelector?.('.tags'); |
Adrià Vilanova Martínez | 6e4a68d | 2022-01-24 21:44:32 +0100 | [diff] [blame] | 159 | if (tags) |
| 160 | window.TWPTExtraInfo.injectAtCRIfEnabled(tags, /* isExpanded = */ true); |
| 161 | } |
| 162 | |
| 163 | // Show additional details in the thread view. |
| 164 | if (node.matches('ec-thread div[role="list"]')) { |
| 165 | const question = node.querySelector('ec-question'); |
| 166 | if (question) window.TWPTExtraInfo.injectAtQuestionIfEnabled(question); |
| 167 | } |
| 168 | if (node.matches('ec-thread ec-message')) { |
| 169 | window.TWPTExtraInfo.injectAtMessageIfEnabled(node); |
Adrià Vilanova Martínez | 74a2520 | 2022-01-23 23:36:58 +0100 | [diff] [blame] | 170 | } |
Adrià Vilanova Martínez | 4f56d56 | 2022-01-26 00:23:27 +0100 | [diff] [blame] | 171 | |
| 172 | // Inject per-forum stats section in the user profile |
| 173 | if (node.matches( |
| 174 | 'ec-unified-user .scTailwindUser_profileUserprofilesection ' + |
| 175 | 'sc-tailwind-shared-activity-chart')) { |
| 176 | window.TWPTExtraInfo.injectPerForumStatsIfEnabled(node); |
| 177 | } |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | |
| 181 | function handleRemovedNode(node) { |
| 182 | // Remove snackbar when exiting thread list view |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 183 | if ('tagName' in node && node.tagName == 'EC-THREAD-LIST') { |
avm99963 | fd22267 | 2021-08-12 23:23:01 +0200 | [diff] [blame] | 184 | window.TWPTAutoRefresh.hideUpdatePrompt(); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | |
| 188 | function mutationCallback(mutationList, observer) { |
| 189 | mutationList.forEach((mutation) => { |
| 190 | if (mutation.type == 'childList') { |
| 191 | mutation.addedNodes.forEach(function(node) { |
| 192 | handleCandidateNode(node); |
| 193 | }); |
| 194 | |
| 195 | mutation.removedNodes.forEach(function(node) { |
| 196 | handleRemovedNode(node); |
| 197 | }); |
| 198 | } |
| 199 | }); |
| 200 | } |
| 201 | |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 202 | var observerOptions = { |
| 203 | childList: true, |
| 204 | subtree: true, |
| 205 | }; |
| 206 | |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 207 | getOptions(null).then(items => { |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 208 | options = items; |
| 209 | |
Adrià Vilanova Martínez | 27c6996 | 2021-07-17 23:32:51 +0200 | [diff] [blame] | 210 | // Initialize classes needed by the mutation observer |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 211 | avatars = new AvatarsHandler(); |
Adrià Vilanova Martínez | a7ae3db | 2022-01-28 11:57:27 +0100 | [diff] [blame] | 212 | infiniteScroll = new InfiniteScroll(); |
Adrià Vilanova Martínez | 1e10d19 | 2021-12-31 16:01:13 +0100 | [diff] [blame] | 213 | workflows = new Workflows(); |
Adrià Vilanova Martínez | 27c6996 | 2021-07-17 23:32:51 +0200 | [diff] [blame] | 214 | |
avm99963 | b6f68b6 | 2021-08-12 23:13:06 +0200 | [diff] [blame] | 215 | // autoRefresh is initialized in start.js |
avm99963 | d3f4ac0 | 2021-08-12 18:36:58 +0200 | [diff] [blame] | 216 | |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 217 | // Before starting the mutation Observer, check whether we missed any |
| 218 | // mutations by manually checking whether some watched nodes already |
| 219 | // exist. |
| 220 | var cssSelectors = watchedNodesSelectors.join(','); |
| 221 | document.querySelectorAll(cssSelectors) |
| 222 | .forEach(node => handleCandidateNode(node)); |
| 223 | |
| 224 | mutationObserver = new MutationObserver(mutationCallback); |
| 225 | mutationObserver.observe(document.body, observerOptions); |
| 226 | |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 227 | // 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] | 228 | if (options.fixedtoolbar) { |
| 229 | injectStyles( |
| 230 | 'ec-bulk-actions{position: sticky; top: 0; background: var(--TWPT-primary-background, #fff); z-index: 96;}'); |
| 231 | } |
| 232 | |
| 233 | if (options.increasecontrast) { |
| 234 | injectStyles( |
| 235 | '.thread-summary.read:not(.checked){background: var(--TWPT-thread-read-background, #ecedee)!important;}'); |
| 236 | } |
| 237 | |
| 238 | if (options.stickysidebarheaders) { |
| 239 | injectStyles( |
| 240 | 'material-drawer .main-header{background: var(--TWPT-drawer-background, #fff)!important; position: sticky; top: 0; z-index: 1;}'); |
| 241 | } |
| 242 | |
| 243 | if (options.enhancedannouncementsdot) { |
| 244 | injectStylesheet( |
| 245 | chrome.runtime.getURL('css/enhanced_announcements_dot.css')); |
| 246 | } |
| 247 | |
| 248 | if (options.repositionexpandthread) { |
Adrià Vilanova Martínez | 27c6996 | 2021-07-17 23:32:51 +0200 | [diff] [blame] | 249 | injectStylesheet(chrome.runtime.getURL('css/reposition_expand_thread.css')); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 250 | } |
| 251 | |
Adrià Vilanova Martínez | 9d27c21 | 2021-12-05 13:54:10 +0100 | [diff] [blame] | 252 | if (options.imagemaxheight) { |
| 253 | injectStylesheet(chrome.runtime.getURL('css/image_max_height.css')); |
| 254 | } |
| 255 | |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 256 | if (options.ccforcehidedrawer) { |
| 257 | var drawer = document.querySelector('material-drawer'); |
| 258 | if (drawer !== null && drawer.classList.contains('mat-drawer-expanded')) { |
| 259 | document.querySelector('.material-drawer-button').click(); |
| 260 | } |
| 261 | } |
| 262 | |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 263 | // Batch lock |
| 264 | injectScript(chrome.runtime.getURL('batchLockInject.bundle.js')); |
| 265 | injectStylesheet(chrome.runtime.getURL('css/batchlock_inject.css')); |
| 266 | // Thread list avatars |
| 267 | injectStylesheet(chrome.runtime.getURL('css/thread_list_avatars.css')); |
| 268 | // Auto refresh list |
| 269 | injectStylesheet(chrome.runtime.getURL('css/autorefresh_list.css')); |
Adrià Vilanova Martínez | 7e8796c | 2022-01-23 21:46:46 +0100 | [diff] [blame] | 270 | // Extra info |
| 271 | injectStylesheet(chrome.runtime.getURL('css/extrainfo.css')); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 272 | }); |