blob: 4d98bee53b3f792c73e7d95ced798165334b71d7 [file] [log] [blame]
Adrià Vilanova Martínezc0a0abc2022-01-28 11:06:02 +01001import {injectStylesheet} from '../common/contentScriptsUtils.js';
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +02002import {getOptions} from '../common/optionsUtils.js';
Adrià Vilanova Martínez46773172024-01-10 21:44:22 +01003import {redirectIfApplicable} from '../redirect/index.js';
avm99963dda2b042021-03-16 23:59:08 +01004
Adrià Vilanova Martínezf93656e2022-01-28 10:54:28 +01005const kLoadMoreButtons = [
6 {
7 feature: 'thread',
8 buttonSelectors: [
9 '.thread-all-replies__load-more-button',
10 '.scTailwindThreadMorebuttonload-more .scTailwindThreadMorebuttonbutton',
Adrià Vilanova Martínez88854972022-08-28 11:57:12 +020011 '.scTailwindThreadMessagegapload-more .scTailwindThreadMessagegapbutton',
Adrià Vilanova Martínezf93656e2022-01-28 10:54:28 +010012 ],
13 },
14 {
15 feature: 'threadall',
16 buttonSelectors: [
17 '.thread-all-replies__load-all-button',
18 '.scTailwindThreadMorebuttonload-all .scTailwindThreadMorebuttonbutton',
Adrià Vilanova Martínez88854972022-08-28 11:57:12 +020019 '.scTailwindThreadMessagegapload-all .scTailwindThreadMessagegapbutton',
Adrià Vilanova Martínezf93656e2022-01-28 10:54:28 +010020 ],
21 }
22];
Adrià Vilanova Martínezc8007802022-10-01 19:42:19 +020023const kMsgidDelay = 3500;
Adrià Vilanova Martínezf93656e2022-01-28 10:54:28 +010024
Adrià Vilanova Martínez46773172024-01-10 21:44:22 +010025function 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
avm9996336b8dbc2020-09-01 21:16:19 +020037var intersectionObserver;
avm99963cbea3142019-03-28 00:48:15 +010038
Adrià Vilanova Martínez46773172024-01-10 21:44:22 +010039function intersectionCallback(entries) {
avm9996336b8dbc2020-09-01 21:16:19 +020040 entries.forEach(entry => {
41 if (entry.isIntersecting) {
42 entry.target.click();
avm99963cbea3142019-03-28 00:48:15 +010043 }
44 });
Adrià Vilanova Martínez46773172024-01-10 21:44:22 +010045}
avm9996336b8dbc2020-09-01 21:16:19 +020046
47var intersectionOptions = {
48 threshold: 1.0,
49};
50
Adrià Vilanova Martínezc8007802022-10-01 19:42:19 +020051function 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
66function 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ínez46773172024-01-10 21:44:22 +010079main();