aboutsummaryrefslogtreecommitdiff
path: root/content/vatican-news-site.js
blob: 2e68151a2fc14a9b55af8ac382ea9c44e8736db0 (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
 * SPDX-License-Identifier: CC0-1.0
 *
 * Copyright (C) 2025 Woj. Kosior <koszko@koszko.org>
 */

/*
  #+begin_src manifest-jq
  .matches = ["https://www.vaticannews.va/*"]
  #+end_src
*/

if (false) { /* #+begin_src background-js */
    blockJsOnUrl("https://www.vaticannews.va/*");
} /* #+end_src */

/* Make some dynamically-loaded images work. */

const dynamicImages = Object.assign(
    {},
    [...document.querySelectorAll("img[data-original]")].filter(
        img => img.parentElement.tagName === "A"
    )
);

const eps = 1e-10;

let loadingTimeout = null;

function imageLoaded(ev) {
    ev.target.classList.add("loaded");
    ev.target.removeEventListener("load", imageLoaded);
}

function loadVisibleImages() {
    loadingTimeout = null;

    const min = -eps, max = window.innerHeight + eps;
    let allLoaded = true;

    for (const [idx, img] of Object.entries(dynamicImages)) {
        const boundingRect = img.getBoundingClientRect();
        if (boundingRect.top > min && boundingRect.bottom < max) {
            delete dynamicImages[idx];
            img.src = img.getAttribute("data-original");
            img.addEventListener("load", imageLoaded);
        } else {
            allLoaded = false;
        }
    }

    if (allLoaded)
        window.removeEventListener("resize", setLoadingTimeout);
}

function setLoadingTimeout() {
    if (loadingTimeout !== null)
        clearTimeout(loadingTimeout);

    loadingTimeout = setTimeout(loadVisibleImages, 500);
}

window.addEventListener("scroll", setLoadingTimeout);
window.addEventListener("resize", setLoadingTimeout);

loadVisibleImages();