summaryrefslogtreecommitdiff
path: root/source/features/unfinished-comments.tsx
blob: c8dd51275785720c3f142504cde3b4a46ad9a4e8 (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
import select from 'select-dom';
import * as pageDetect from 'github-url-detection';

import features from '.';

let documentTitle: string | undefined;

function hasDraftComments(): boolean {
	return select.all('textarea').some(textarea => textarea.value.length > 0 && textarea.offsetWidth > 0);
}

async function updateDocumentTitle(): Promise<void> {
	if (documentTitle) {
		document.title = documentTitle;
		documentTitle = undefined;
	} else if (document.visibilityState === 'hidden' && hasDraftComments()) {
		documentTitle = document.title;
		document.title = '(Draft comment) ' + document.title;
	}
}

function init(): void {
	document.addEventListener('visibilitychange', updateDocumentTitle);
}

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