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