blob: dd2088a9ca9f5f7c17bcc0bbd4cef3568a00da95 [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';
3
avm99963dda2b042021-03-16 23:59:08 +01004var CCThreadWithoutMessage = /forum\/[0-9]*\/thread\/[0-9]*$/;
5
Adrià Vilanova Martínezf93656e2022-01-28 10:54:28 +01006const kLoadMoreButtons = [
7 {
8 feature: 'thread',
9 buttonSelectors: [
10 '.thread-all-replies__load-more-button',
11 '.scTailwindThreadMorebuttonload-more .scTailwindThreadMorebuttonbutton',
Adrià Vilanova Martínez88854972022-08-28 11:57:12 +020012 '.scTailwindThreadMessagegapload-more .scTailwindThreadMessagegapbutton',
Adrià Vilanova Martínezf93656e2022-01-28 10:54:28 +010013 ],
14 },
15 {
16 feature: 'threadall',
17 buttonSelectors: [
18 '.thread-all-replies__load-all-button',
19 '.scTailwindThreadMorebuttonload-all .scTailwindThreadMorebuttonbutton',
Adrià Vilanova Martínez88854972022-08-28 11:57:12 +020020 '.scTailwindThreadMessagegapload-all .scTailwindThreadMessagegapbutton',
Adrià Vilanova Martínezf93656e2022-01-28 10:54:28 +010021 ],
22 }
23];
Adrià Vilanova Martínezc8007802022-10-01 19:42:19 +020024const kMsgidDelay = 3500;
Adrià Vilanova Martínezf93656e2022-01-28 10:54:28 +010025
avm9996336b8dbc2020-09-01 21:16:19 +020026var intersectionObserver;
avm99963cbea3142019-03-28 00:48:15 +010027
avm9996336b8dbc2020-09-01 21:16:19 +020028function intersectionCallback(entries, observer) {
29 entries.forEach(entry => {
30 if (entry.isIntersecting) {
31 entry.target.click();
avm99963cbea3142019-03-28 00:48:15 +010032 }
33 });
avm9996336b8dbc2020-09-01 21:16:19 +020034};
35
36var intersectionOptions = {
37 threshold: 1.0,
38};
39
Adrià Vilanova Martínezc8007802022-10-01 19:42:19 +020040function 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
55function 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ínezd269c622021-09-04 18:35:55 +020068getOptions(null).then(options => {
avm9996336b8dbc2020-09-01 21:16:19 +020069 var redirectLink = document.querySelector('.community-console');
Adrià Vilanova Martínezd269c622021-09-04 18:35:55 +020070 if (options.redirect && redirectLink !== null) {
avm99963dda2b042021-03-16 23:59:08 +010071 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;
avm9996336b8dbc2020-09-01 21:16:19 +020080 } else {
Adrià Vilanova Martínezc8007802022-10-01 19:42:19 +020081 setUpInfiniteScrollWithPotentialDelay(options);
Adrià Vilanova Martínezc0a0abc2022-01-28 11:06:02 +010082
83 if (options.imagemaxheight)
84 injectStylesheet(chrome.runtime.getURL('css/image_max_height.css'));
avm9996336b8dbc2020-09-01 21:16:19 +020085 }
86});