summaryrefslogtreecommitdiff
path: root/source/features/infinite-scroll.tsx
blob: a25465cd2c88e3ead80ff6c260c4462d9a5ec80f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import select from 'select-dom';
import onetime from 'onetime';
import debounce from 'debounce-fn';
import {observe} from 'selector-observer';
import * as pageDetect from 'github-url-detection';

import features from '.';

const loadMore = debounce(() => {
	const button = select<HTMLButtonElement>('.ajax-pagination-btn')!;
	button.click();
	button.textContent = 'Loading…';

	// If GH hasn't loaded the JS, the click will not load anything.
	// We can detect if it worked by looking at the button's state,
	// and then trying again (auto-debounced)
	if (!button.disabled) {
		loadMore();
	}
}, {wait: 200});

const inView = new IntersectionObserver(([{isIntersecting}]) => {
	if (isIntersecting) {
		loadMore();
	}
}, {
	rootMargin: '500px' // https://github.com/sindresorhus/refined-github/pull/505#issuecomment-309273098
});

function init(): void {
	observe('.ajax-pagination-btn', {
		add(button) {
			inView.observe(button);
		}
	});
}

void features.add(__filebasename, {
	include: [
		pageDetect.isDashboard
	],
	init: onetime(init)
});