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 | 4d59925 | 2023-02-04 20:54:54 +0100 | [diff] [blame] | 2 | import {getOptions} from '../../common/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 | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 8 | import {injectDarkModeButton, isDarkThemeOn} from './darkMode.js'; |
Adrià Vilanova Martínez | eebc0ac | 2022-01-05 14:45:53 +0100 | [diff] [blame] | 9 | // #!if ['chromium', 'chromium_mv3'].includes(browser_target) |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 10 | import {applyDragAndDropFixIfEnabled} from './dragAndDropFix.js'; |
Adrià Vilanova Martínez | eebc0ac | 2022-01-05 14:45:53 +0100 | [diff] [blame] | 11 | // #!endif |
Adrià Vilanova Martínez | 115e3d8 | 2023-01-10 21:50:06 +0100 | [diff] [blame] | 12 | import {default as FlattenThreads, kMatchingSelectors as kFlattenThreadMatchingSelectors} from './flattenThreads/flattenThreads.js'; |
Adrià Vilanova Martínez | a7ae3db | 2022-01-28 11:57:27 +0100 | [diff] [blame] | 13 | import InfiniteScroll from './infiniteScroll.js'; |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 14 | import {kRepliesSectionSelector} from './threadToolbar/constants.js'; |
| 15 | import ThreadToolbar from './threadToolbar/threadToolbar.js'; |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 16 | import {unifiedProfilesFix} from './unifiedProfiles.js'; |
Adrià Vilanova Martínez | 2788d12 | 2022-10-10 22:06:25 +0200 | [diff] [blame] | 17 | import Workflows from './workflows/workflows.js'; |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 18 | |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 19 | var mutationObserver, options, avatars, infiniteScroll, workflows, |
Adrià Vilanova Martínez | 412b758 | 2022-12-30 01:35:30 +0100 | [diff] [blame] | 20 | threadToolbar, flattenThreads; |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 21 | |
| 22 | const watchedNodesSelectors = [ |
| 23 | // App container (used to set up the intersection observer and inject the dark |
| 24 | // mode button) |
| 25 | 'ec-app', |
| 26 | |
Adrià Vilanova Martínez | a7ae3db | 2022-01-28 11:57:27 +0100 | [diff] [blame] | 27 | // Scrollable content (used for the intersection observer) |
| 28 | '.scrollable-content', |
| 29 | |
| 30 | // Load more bar and buttons |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 31 | '.load-more-bar', |
Adrià Vilanova Martínez | a7ae3db | 2022-01-28 11:57:27 +0100 | [diff] [blame] | 32 | '.scTailwindThreadMorebuttonbutton', |
Adrià Vilanova Martínez | 8885497 | 2022-08-28 11:57:12 +0200 | [diff] [blame] | 33 | '.scTailwindThreadMessagegapbutton', |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 34 | |
Adrià Vilanova Martínez | 7e8796c | 2022-01-23 21:46:46 +0100 | [diff] [blame] | 35 | // User profile card inside ec-unified-user |
| 36 | 'ec-unified-user .scTailwindUser_profileUsercardmain', |
| 37 | |
Adrià Vilanova Martínez | 531cd07 | 2021-12-05 20:15:43 +0100 | [diff] [blame] | 38 | // Username span/editor inside ec-unified-user (user profile view) |
Adrià Vilanova Martínez | 1f65252 | 2021-10-14 00:23:23 +0200 | [diff] [blame] | 39 | 'ec-unified-user .scTailwindUser_profileUsercarddetails', |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 40 | |
| 41 | // Rich text editor |
| 42 | 'ec-movable-dialog', |
| 43 | 'ec-rich-text-editor', |
| 44 | |
| 45 | // Read/unread bulk action in the list of thread, for the batch lock feature |
| 46 | 'ec-bulk-actions material-button[debugid="mark-read-button"]', |
| 47 | 'ec-bulk-actions material-button[debugid="mark-unread-button"]', |
| 48 | |
Adrià Vilanova Martínez | 854cb91 | 2022-02-02 16:18:23 +0100 | [diff] [blame] | 49 | // Thread list items (used to inject the avatars and extra info) |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 50 | 'li', |
| 51 | |
Adrià Vilanova Martínez | 854cb91 | 2022-02-02 16:18:23 +0100 | [diff] [blame] | 52 | // Thread list item toolbelt (used for the extra info feature) |
| 53 | 'ec-thread-summary .main .toolbelt', |
| 54 | |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 55 | // Thread list (used for the autorefresh feature) |
| 56 | 'ec-thread-list', |
| 57 | |
| 58 | // Unified profile iframe |
| 59 | 'iframe', |
Adrià Vilanova Martínez | 74a2520 | 2022-01-23 23:36:58 +0100 | [diff] [blame] | 60 | |
Adrià Vilanova Martínez | e0d65f2 | 2022-11-06 18:49:35 +0100 | [diff] [blame] | 61 | // Canned response tags or toolbelt (for the extra info feature and the |
| 62 | // "import CR" popup for the workflows feature) |
Adrià Vilanova Martínez | 6e4a68d | 2022-01-24 21:44:32 +0100 | [diff] [blame] | 63 | 'ec-canned-response-row .tags', |
| 64 | 'ec-canned-response-row .main .toolbelt', |
| 65 | |
Adrià Vilanova Martínez | 09b3bdb | 2022-02-05 00:15:05 +0100 | [diff] [blame] | 66 | // Question state chips container (for the extra info feature) |
| 67 | 'ec-question .state-chips', |
Adrià Vilanova Martínez | 6e4a68d | 2022-01-24 21:44:32 +0100 | [diff] [blame] | 68 | |
| 69 | // Replies (for the extra info feature) |
| 70 | 'ec-thread ec-message', |
Adrià Vilanova Martínez | 4f56d56 | 2022-01-26 00:23:27 +0100 | [diff] [blame] | 71 | |
| 72 | // User activity chart (for the per-forum stats feature) |
| 73 | 'ec-unified-user .scTailwindUser_profileUserprofilesection ' + |
| 74 | 'sc-tailwind-shared-activity-chart', |
Adrià Vilanova Martínez | 5a8055b | 2022-09-29 13:05:19 +0200 | [diff] [blame] | 75 | |
| 76 | // Thread page main content |
| 77 | 'ec-thread > .page > .material-content > div[role="list"]', |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 78 | |
| 79 | // Thread page reply section (for the thread page toolbar) |
| 80 | kRepliesSectionSelector, |
Adrià Vilanova Martínez | 412b758 | 2022-12-30 01:35:30 +0100 | [diff] [blame] | 81 | |
| 82 | // Reply payload (for the flatten threads UI) |
Adrià Vilanova Martínez | 115e3d8 | 2023-01-10 21:50:06 +0100 | [diff] [blame] | 83 | ...kFlattenThreadMatchingSelectors, |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 84 | ]; |
| 85 | |
| 86 | function handleCandidateNode(node) { |
| 87 | if (typeof node.classList !== 'undefined') { |
| 88 | if (('tagName' in node) && node.tagName == 'EC-APP') { |
Adrià Vilanova Martínez | a7ae3db | 2022-01-28 11:57:27 +0100 | [diff] [blame] | 89 | infiniteScroll.setUpIntersectionObserver(node, false); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 90 | |
| 91 | // Inject the dark mode button |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 92 | // TODO(avm99963): make this feature dynamic. |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 93 | if (options.ccdarktheme && options.ccdarktheme_mode == 'switch') { |
| 94 | var rightControl = node.querySelector('header .right-control'); |
| 95 | if (rightControl !== null) |
| 96 | injectDarkModeButton(rightControl, options.ccdarktheme_switch_status); |
| 97 | } |
| 98 | } |
| 99 | |
Adrià Vilanova Martínez | a7ae3db | 2022-01-28 11:57:27 +0100 | [diff] [blame] | 100 | // To set up infinite scroll |
| 101 | if (node.classList.contains('scrollable-content')) { |
| 102 | infiniteScroll.setUpIntersectionObserver(node, true); |
| 103 | } |
| 104 | |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 105 | // Start the intersectionObserver for the "load more"/"load all" buttons |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 106 | // inside a thread if the option is currently enabled. |
| 107 | if (node.classList.contains('load-more-bar')) { |
Adrià Vilanova Martínez | a7ae3db | 2022-01-28 11:57:27 +0100 | [diff] [blame] | 108 | infiniteScroll.observeLoadMoreBar(node); |
| 109 | } |
Adrià Vilanova Martínez | 8885497 | 2022-08-28 11:57:12 +0200 | [diff] [blame] | 110 | if (node.classList.contains('scTailwindThreadMorebuttonbutton') || |
| 111 | node.classList.contains('scTailwindThreadMessagegapbutton')) { |
Adrià Vilanova Martínez | a7ae3db | 2022-01-28 11:57:27 +0100 | [diff] [blame] | 112 | infiniteScroll.observeLoadMoreInteropBtn(node); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 113 | } |
| 114 | |
Adrià Vilanova Martínez | 7e8796c | 2022-01-23 21:46:46 +0100 | [diff] [blame] | 115 | // Show additional details in the profile view. |
| 116 | if (node.matches('ec-unified-user .scTailwindUser_profileUsercardmain')) { |
| 117 | window.TWPTExtraInfo.injectAtProfileIfEnabled(node); |
| 118 | } |
| 119 | |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 120 | // 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] | 121 | // 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] | 122 | if (node.matches( |
| 123 | 'ec-unified-user .scTailwindUser_profileUsercarddetails')) { |
| 124 | injectPreviousPostsLinksUnifiedProfileIfEnabled( |
| 125 | /* isCommunityConsole = */ true); |
| 126 | } |
| 127 | |
Adrià Vilanova Martínez | eebc0ac | 2022-01-05 14:45:53 +0100 | [diff] [blame] | 128 | // #!if ['chromium', 'chromium_mv3'].includes(browser_target) |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 129 | // Fix the drag&drop issue with the rich text editor if the option is |
| 130 | // currently enabled. |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 131 | // |
| 132 | // We target both tags because in different contexts different |
| 133 | // elements containing the text editor get added to the DOM structure. |
| 134 | // Sometimes it's a EC-MOVABLE-DIALOG which already contains the |
| 135 | // EC-RICH-TEXT-EDITOR, and sometimes it's the EC-RICH-TEXT-EDITOR |
| 136 | // directly. |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 137 | if (('tagName' in node) && |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 138 | (node.tagName == 'EC-MOVABLE-DIALOG' || |
| 139 | node.tagName == 'EC-RICH-TEXT-EDITOR')) { |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 140 | applyDragAndDropFixIfEnabled(node); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 141 | } |
Adrià Vilanova Martínez | eebc0ac | 2022-01-05 14:45:53 +0100 | [diff] [blame] | 142 | // #!endif |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 143 | |
Adrià Vilanova Martínez | 2788d12 | 2022-10-10 22:06:25 +0200 | [diff] [blame] | 144 | // Inject the worflows menu in the thread list if the option is currently |
| 145 | // enabled. |
| 146 | if (workflows.shouldAddThreadListBtn(node)) { |
| 147 | workflows.addThreadListBtnIfEnabled(node); |
| 148 | } |
| 149 | |
Adrià Vilanova Martínez | b06c13d | 2022-06-20 17:30:14 +0000 | [diff] [blame] | 150 | // Inject the batch lock button in the thread list if the option is |
| 151 | // currently enabled. |
Adrià Vilanova Martínez | 4107b5e | 2022-10-09 23:11:11 +0200 | [diff] [blame] | 152 | if (batchLock.shouldAddButton(node)) { |
Adrià Vilanova Martínez | b06c13d | 2022-06-20 17:30:14 +0000 | [diff] [blame] | 153 | batchLock.addButtonIfEnabled(node); |
| 154 | } |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 155 | |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 156 | // Inject avatar links to threads in the thread list. injectIfEnabled is |
| 157 | // responsible of determining whether it should run or not depending on its |
| 158 | // current setting. |
Adrià Vilanova Martínez | 854cb91 | 2022-02-02 16:18:23 +0100 | [diff] [blame] | 159 | // |
| 160 | // Also, inject extra info in the thread list. |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 161 | if (('tagName' in node) && (node.tagName == 'LI') && |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 162 | node.querySelector('ec-thread-summary') !== null) { |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 163 | avatars.injectIfEnabled(node); |
Adrià Vilanova Martínez | 854cb91 | 2022-02-02 16:18:23 +0100 | [diff] [blame] | 164 | window.TWPTExtraInfo.injectAtThreadListIfEnabled(node); |
| 165 | } |
| 166 | |
| 167 | // Inject extra info in the toolbelt of an expanded thread list item. |
| 168 | if (node.matches('ec-thread-summary .main .toolbelt')) { |
| 169 | window.TWPTExtraInfo.injectAtExpandedThreadListIfEnabled(node); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 170 | } |
| 171 | |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 172 | // Set up the autorefresh list feature. The setUp function is responsible |
| 173 | // of determining whether it should run or not depending on the current |
| 174 | // setting. |
| 175 | if (('tagName' in node) && node.tagName == 'EC-THREAD-LIST') { |
avm99963 | fd22267 | 2021-08-12 23:23:01 +0200 | [diff] [blame] | 176 | window.TWPTAutoRefresh.setUp(); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | // Redirect unified profile iframe to dark version if applicable |
| 180 | if (node.tagName == 'IFRAME' && isDarkThemeOn(options) && |
| 181 | unifiedProfilesFix.checkIframe(node)) { |
| 182 | unifiedProfilesFix.fixIframe(node); |
| 183 | } |
Adrià Vilanova Martínez | 74a2520 | 2022-01-23 23:36:58 +0100 | [diff] [blame] | 184 | |
Adrià Vilanova Martínez | e0d65f2 | 2022-11-06 18:49:35 +0100 | [diff] [blame] | 185 | // Show additional details in the canned responses view (and add the |
| 186 | // "import" button if applicable for the workflows feature). |
Adrià Vilanova Martínez | 74a2520 | 2022-01-23 23:36:58 +0100 | [diff] [blame] | 187 | if (node.matches('ec-canned-response-row .tags')) { |
| 188 | window.TWPTExtraInfo.injectAtCRIfEnabled(node, /* isExpanded = */ false); |
Adrià Vilanova Martínez | e0d65f2 | 2022-11-06 18:49:35 +0100 | [diff] [blame] | 189 | window.TWPTWorkflowsImport.addButtonIfEnabled(node); |
Adrià Vilanova Martínez | 74a2520 | 2022-01-23 23:36:58 +0100 | [diff] [blame] | 190 | } |
| 191 | if (node.matches('ec-canned-response-row .main .toolbelt')) { |
| 192 | const tags = node.parentNode?.querySelector?.('.tags'); |
Adrià Vilanova Martínez | 6e4a68d | 2022-01-24 21:44:32 +0100 | [diff] [blame] | 193 | if (tags) |
| 194 | window.TWPTExtraInfo.injectAtCRIfEnabled(tags, /* isExpanded = */ true); |
| 195 | } |
| 196 | |
| 197 | // Show additional details in the thread view. |
Adrià Vilanova Martínez | 09b3bdb | 2022-02-05 00:15:05 +0100 | [diff] [blame] | 198 | if (node.matches('ec-question .state-chips')) { |
| 199 | window.TWPTExtraInfo.injectAtQuestionIfEnabled(node); |
Adrià Vilanova Martínez | 6e4a68d | 2022-01-24 21:44:32 +0100 | [diff] [blame] | 200 | } |
| 201 | if (node.matches('ec-thread ec-message')) { |
| 202 | window.TWPTExtraInfo.injectAtMessageIfEnabled(node); |
Adrià Vilanova Martínez | 74a2520 | 2022-01-23 23:36:58 +0100 | [diff] [blame] | 203 | } |
Adrià Vilanova Martínez | 4f56d56 | 2022-01-26 00:23:27 +0100 | [diff] [blame] | 204 | |
| 205 | // Inject per-forum stats section in the user profile |
| 206 | if (node.matches( |
| 207 | 'ec-unified-user .scTailwindUser_profileUserprofilesection ' + |
| 208 | 'sc-tailwind-shared-activity-chart')) { |
| 209 | window.TWPTExtraInfo.injectPerForumStatsIfEnabled(node); |
| 210 | } |
Adrià Vilanova Martínez | 5a8055b | 2022-09-29 13:05:19 +0200 | [diff] [blame] | 211 | |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 212 | // Inject old thread page design warning if applicable |
Adrià Vilanova Martínez | d2344c4 | 2022-09-30 13:14:29 +0200 | [diff] [blame] | 213 | if (node.matches( |
| 214 | 'ec-thread > .page > .material-content > div[role="list"]')) { |
| 215 | window.TWPTThreadPageDesignWarning.injectWarningIfApplicable(node); |
Adrià Vilanova Martínez | 5a8055b | 2022-09-29 13:05:19 +0200 | [diff] [blame] | 216 | } |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 217 | |
| 218 | // Inject thread toolbar |
| 219 | if (threadToolbar.shouldInject(node)) { |
| 220 | threadToolbar.injectIfApplicable(node); |
| 221 | } |
Adrià Vilanova Martínez | 412b758 | 2022-12-30 01:35:30 +0100 | [diff] [blame] | 222 | |
| 223 | // Inject parent reply quote |
Adrià Vilanova Martínez | 115e3d8 | 2023-01-10 21:50:06 +0100 | [diff] [blame] | 224 | if (flattenThreads.shouldInjectQuote(node)) { |
| 225 | flattenThreads.injectQuoteIfApplicable(node); |
| 226 | } |
| 227 | |
| 228 | // Inject reply button in non-nested view |
| 229 | if (flattenThreads.shouldInjectReplyBtn(node)) { |
| 230 | flattenThreads.injectReplyBtnIfApplicable(node); |
Adrià Vilanova Martínez | 412b758 | 2022-12-30 01:35:30 +0100 | [diff] [blame] | 231 | } |
Adrià Vilanova Martínez | 4d59925 | 2023-02-04 20:54:54 +0100 | [diff] [blame] | 232 | |
| 233 | // Delete additional info in the edit message box |
| 234 | if (flattenThreads.isAdditionalInfoElement(node)) { |
| 235 | flattenThreads.deleteAdditionalInfoElementIfApplicable(node); |
| 236 | } |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | |
Adrià Vilanova Martínez | 115e3d8 | 2023-01-10 21:50:06 +0100 | [diff] [blame] | 240 | function handleRemovedNode(mutation, node) { |
| 241 | if (!('tagName' in node)) return; |
| 242 | |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 243 | // Remove snackbar when exiting thread list view |
Adrià Vilanova Martínez | 115e3d8 | 2023-01-10 21:50:06 +0100 | [diff] [blame] | 244 | if (node.tagName == 'EC-THREAD-LIST') { |
avm99963 | fd22267 | 2021-08-12 23:23:01 +0200 | [diff] [blame] | 245 | window.TWPTAutoRefresh.hideUpdatePrompt(); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 246 | } |
Adrià Vilanova Martínez | 115e3d8 | 2023-01-10 21:50:06 +0100 | [diff] [blame] | 247 | |
| 248 | // Readd reply button when the Community Console removes it |
| 249 | if (node.tagName == 'TWPT-FLATTEN-THREAD-REPLY-BUTTON') { |
| 250 | flattenThreads.injectReplyBtn( |
| 251 | mutation.target, JSON.parse(node.getAttribute('extraInfo'))); |
| 252 | } |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 253 | } |
| 254 | |
Adrià Vilanova Martínez | 115e3d8 | 2023-01-10 21:50:06 +0100 | [diff] [blame] | 255 | function mutationCallback(mutationList) { |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 256 | mutationList.forEach((mutation) => { |
| 257 | if (mutation.type == 'childList') { |
| 258 | mutation.addedNodes.forEach(function(node) { |
| 259 | handleCandidateNode(node); |
| 260 | }); |
| 261 | |
| 262 | mutation.removedNodes.forEach(function(node) { |
Adrià Vilanova Martínez | 115e3d8 | 2023-01-10 21:50:06 +0100 | [diff] [blame] | 263 | handleRemovedNode(mutation, node); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 264 | }); |
| 265 | } |
| 266 | }); |
| 267 | } |
| 268 | |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 269 | var observerOptions = { |
| 270 | childList: true, |
| 271 | subtree: true, |
| 272 | }; |
| 273 | |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 274 | getOptions(null).then(items => { |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 275 | options = items; |
| 276 | |
Adrià Vilanova Martínez | 27c6996 | 2021-07-17 23:32:51 +0200 | [diff] [blame] | 277 | // Initialize classes needed by the mutation observer |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 278 | avatars = new AvatarsHandler(); |
Adrià Vilanova Martínez | a7ae3db | 2022-01-28 11:57:27 +0100 | [diff] [blame] | 279 | infiniteScroll = new InfiniteScroll(); |
Adrià Vilanova Martínez | 2788d12 | 2022-10-10 22:06:25 +0200 | [diff] [blame] | 280 | workflows = new Workflows(); |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 281 | threadToolbar = new ThreadToolbar(); |
Adrià Vilanova Martínez | 412b758 | 2022-12-30 01:35:30 +0100 | [diff] [blame] | 282 | flattenThreads = new FlattenThreads(); |
Adrià Vilanova Martínez | 27c6996 | 2021-07-17 23:32:51 +0200 | [diff] [blame] | 283 | |
Adrià Vilanova Martínez | e0d65f2 | 2022-11-06 18:49:35 +0100 | [diff] [blame] | 284 | // autoRefresh, extraInfo, threadPageDesignWarning and workflowsImport are |
| 285 | // initialized in start.js |
avm99963 | d3f4ac0 | 2021-08-12 18:36:58 +0200 | [diff] [blame] | 286 | |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 287 | // Before starting the mutation Observer, check whether we missed any |
| 288 | // mutations by manually checking whether some watched nodes already |
| 289 | // exist. |
| 290 | var cssSelectors = watchedNodesSelectors.join(','); |
| 291 | document.querySelectorAll(cssSelectors) |
| 292 | .forEach(node => handleCandidateNode(node)); |
| 293 | |
| 294 | mutationObserver = new MutationObserver(mutationCallback); |
| 295 | mutationObserver.observe(document.body, observerOptions); |
| 296 | |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 297 | // 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] | 298 | if (options.fixedtoolbar) { |
| 299 | injectStyles( |
| 300 | 'ec-bulk-actions{position: sticky; top: 0; background: var(--TWPT-primary-background, #fff); z-index: 96;}'); |
| 301 | } |
| 302 | |
| 303 | if (options.increasecontrast) { |
| 304 | injectStyles( |
| 305 | '.thread-summary.read:not(.checked){background: var(--TWPT-thread-read-background, #ecedee)!important;}'); |
| 306 | } |
| 307 | |
| 308 | if (options.stickysidebarheaders) { |
| 309 | injectStyles( |
| 310 | 'material-drawer .main-header{background: var(--TWPT-drawer-background, #fff)!important; position: sticky; top: 0; z-index: 1;}'); |
| 311 | } |
| 312 | |
| 313 | if (options.enhancedannouncementsdot) { |
| 314 | injectStylesheet( |
| 315 | chrome.runtime.getURL('css/enhanced_announcements_dot.css')); |
| 316 | } |
| 317 | |
| 318 | if (options.repositionexpandthread) { |
Adrià Vilanova Martínez | 27c6996 | 2021-07-17 23:32:51 +0200 | [diff] [blame] | 319 | injectStylesheet(chrome.runtime.getURL('css/reposition_expand_thread.css')); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 320 | } |
| 321 | |
Adrià Vilanova Martínez | 9d27c21 | 2021-12-05 13:54:10 +0100 | [diff] [blame] | 322 | if (options.imagemaxheight) { |
| 323 | injectStylesheet(chrome.runtime.getURL('css/image_max_height.css')); |
| 324 | } |
| 325 | |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 326 | if (options.ccforcehidedrawer) { |
| 327 | var drawer = document.querySelector('material-drawer'); |
| 328 | if (drawer !== null && drawer.classList.contains('mat-drawer-expanded')) { |
| 329 | document.querySelector('.material-drawer-button').click(); |
| 330 | } |
| 331 | } |
| 332 | |
Adrià Vilanova Martínez | d269c62 | 2021-09-04 18:35:55 +0200 | [diff] [blame] | 333 | // Batch lock |
| 334 | injectScript(chrome.runtime.getURL('batchLockInject.bundle.js')); |
| 335 | injectStylesheet(chrome.runtime.getURL('css/batchlock_inject.css')); |
| 336 | // Thread list avatars |
| 337 | injectStylesheet(chrome.runtime.getURL('css/thread_list_avatars.css')); |
| 338 | // Auto refresh list |
| 339 | injectStylesheet(chrome.runtime.getURL('css/autorefresh_list.css')); |
Adrià Vilanova Martínez | 7e8796c | 2022-01-23 21:46:46 +0100 | [diff] [blame] | 340 | // Extra info |
| 341 | injectStylesheet(chrome.runtime.getURL('css/extrainfo.css')); |
avm99963 | 37601bc | 2022-02-21 10:36:45 +0100 | [diff] [blame] | 342 | injectStylesheet(chrome.runtime.getURL('css/extrainfo_perforumstats.css')); |
Adrià Vilanova Martínez | 2d9be8d | 2022-12-28 00:50:14 +0100 | [diff] [blame] | 343 | // Workflows, Thread toolbar |
| 344 | injectScript(chrome.runtime.getURL('litComponentsInject.bundle.js')); |
| 345 | // Thread toolbar |
| 346 | injectStylesheet(chrome.runtime.getURL('css/thread_toolbar.css')); |
Adrià Vilanova Martínez | 115e3d8 | 2023-01-10 21:50:06 +0100 | [diff] [blame] | 347 | // Flatten threads |
| 348 | injectStylesheet(chrome.runtime.getURL('css/flatten_threads.css')); |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 349 | }); |
Adrià Vilanova Martínez | 825888a | 2023-07-23 01:53:48 +0200 | [diff] [blame] | 350 | |
| 351 | new XHRProxyKillSwitchHandler(); |