aboutsummaryrefslogtreecommitdiff
path: root/static/scripts/auto_refresh.js
blob: 5e7223ea1f3fca37c6c48123f747bd400ab962e1 (plain)
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
(() => {
    const onFrontPage = () => {
        const searchParams = new URLSearchParams(window.location.search);
        const pageNum = searchParams.get("page_num");
        return (pageNum === "1") || (pageNum === null);
    };

    const refreshCheckboxLabel = document.querySelector("label#refresh_checkbox_label");
    if (onFrontPage()) {
        refreshCheckboxLabel.setAttribute("style", "");
    }

    const UPDATE_INTERVAL_MILLISECONDS = 1 * 60 * 1000; // 1 minute
    setInterval(async () => {
        if (!onFrontPage()) {
            return;
        }

        const refreshCheckbox = document.querySelector("label#refresh_checkbox_label > input");
        if (!refreshCheckbox.checked) {
            return;
        }

        console.log("Refreshing entries...");

        const response = await fetch(window.location);
        if (!response.ok) {
            return;
        }
        const responseText = await response.text();

        const parser = new DOMParser();
        const newDoc = parser.parseFromString(responseText, "text/html");
        document.querySelector("table").innerHTML = newDoc.querySelector("table").innerHTML;

        console.log("Refreshed entries.");
    }, UPDATE_INTERVAL_MILLISECONDS);
})();