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