Adrià Vilanova Martínez | d7951b2 | 2024-05-25 22:13:32 +0200 | [diff] [blame] | 1 | import {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 | 4677317 | 2024-01-10 21:44:22 +0100 | [diff] [blame] | 3 | import {redirectIfApplicable} from '../redirect/index.js'; |
avm99963 | dda2b04 | 2021-03-16 23:59:08 +0100 | [diff] [blame] | 4 | |
Adrià Vilanova Martínez | f93656e | 2022-01-28 10:54:28 +0100 | [diff] [blame] | 5 | const kLoadMoreButtons = [ |
| 6 | { |
| 7 | feature: 'thread', |
| 8 | buttonSelectors: [ |
| 9 | '.thread-all-replies__load-more-button', |
| 10 | '.scTailwindThreadMorebuttonload-more .scTailwindThreadMorebuttonbutton', |
Adrià Vilanova Martínez | 8885497 | 2022-08-28 11:57:12 +0200 | [diff] [blame] | 11 | '.scTailwindThreadMessagegapload-more .scTailwindThreadMessagegapbutton', |
Adrià Vilanova Martínez | f93656e | 2022-01-28 10:54:28 +0100 | [diff] [blame] | 12 | ], |
| 13 | }, |
| 14 | { |
| 15 | feature: 'threadall', |
| 16 | buttonSelectors: [ |
| 17 | '.thread-all-replies__load-all-button', |
| 18 | '.scTailwindThreadMorebuttonload-all .scTailwindThreadMorebuttonbutton', |
Adrià Vilanova Martínez | 8885497 | 2022-08-28 11:57:12 +0200 | [diff] [blame] | 19 | '.scTailwindThreadMessagegapload-all .scTailwindThreadMessagegapbutton', |
Adrià Vilanova Martínez | f93656e | 2022-01-28 10:54:28 +0100 | [diff] [blame] | 20 | ], |
| 21 | } |
| 22 | ]; |
Adrià Vilanova Martínez | c800780 | 2022-10-01 19:42:19 +0200 | [diff] [blame] | 23 | const kMsgidDelay = 3500; |
Adrià Vilanova Martínez | f93656e | 2022-01-28 10:54:28 +0100 | [diff] [blame] | 24 | |
Adrià Vilanova Martínez | 4677317 | 2024-01-10 21:44:22 +0100 | [diff] [blame] | 25 | function main() { |
| 26 | const ok = redirectIfApplicable(); |
| 27 | if (ok) return; |
| 28 | |
| 29 | getOptions(null).then(options => { |
| 30 | setUpInfiniteScrollWithPotentialDelay(options); |
| 31 | |
| 32 | if (options.imagemaxheight) |
| 33 | injectStylesheet(chrome.runtime.getURL('css/image_max_height.css')); |
| 34 | }); |
| 35 | } |
| 36 | |
avm99963 | 36b8dbc | 2020-09-01 21:16:19 +0200 | [diff] [blame] | 37 | var intersectionObserver; |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 38 | |
Adrià Vilanova Martínez | 4677317 | 2024-01-10 21:44:22 +0100 | [diff] [blame] | 39 | function intersectionCallback(entries) { |
avm99963 | 36b8dbc | 2020-09-01 21:16:19 +0200 | [diff] [blame] | 40 | entries.forEach(entry => { |
| 41 | if (entry.isIntersecting) { |
| 42 | entry.target.click(); |
avm99963 | cbea314 | 2019-03-28 00:48:15 +0100 | [diff] [blame] | 43 | } |
| 44 | }); |
Adrià Vilanova Martínez | 4677317 | 2024-01-10 21:44:22 +0100 | [diff] [blame] | 45 | } |
avm99963 | 36b8dbc | 2020-09-01 21:16:19 +0200 | [diff] [blame] | 46 | |
| 47 | var intersectionOptions = { |
| 48 | threshold: 1.0, |
| 49 | }; |
| 50 | |
Adrià Vilanova Martínez | c800780 | 2022-10-01 19:42:19 +0200 | [diff] [blame] | 51 | function setUpInfiniteScroll(options) { |
| 52 | for (const entry of kLoadMoreButtons) { |
| 53 | if (options[entry.feature]) { |
| 54 | for (const selector of entry.buttonSelectors) { |
| 55 | let buttons = document.querySelectorAll(selector); |
| 56 | buttons.forEach(button => { |
| 57 | intersectionObserver = new IntersectionObserver( |
| 58 | intersectionCallback, intersectionOptions); |
| 59 | intersectionObserver.observe(button); |
| 60 | }); |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | function setUpInfiniteScrollWithPotentialDelay(options) { |
| 67 | // If the msgid query parameter is set, the page will scroll to that message, |
| 68 | // which will show the "load more" button. |
| 69 | const params = new URLSearchParams(window.location.search); |
| 70 | if (params.has('msgid')) { |
| 71 | window.setTimeout(() => { |
| 72 | setUpInfiniteScroll(options); |
| 73 | }, kMsgidDelay); |
| 74 | } else { |
| 75 | setUpInfiniteScroll(options); |
| 76 | } |
| 77 | } |
| 78 | |
Adrià Vilanova Martínez | 4677317 | 2024-01-10 21:44:22 +0100 | [diff] [blame] | 79 | main(); |