~cytrogen/fluent-reader-mobile

ref: 34469ca0231cd9ea216f42aab16cb9e3714bb4e7 fluent-reader-mobile/assets/article/article.js -rw-r--r-- 2.5 KiB
34469ca0 — HallowDem feat: enhance custom font upload with WebView rendering, WOFF/WOFF2 support, preview & deletion 4 days ago
                                                                                
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
67
68
69
70
71
72
73
function get(name) {
    if (name = (new RegExp('[?&]' + encodeURIComponent(name) + '=([^&]*)')).exec(location.search))
        return decodeURIComponent(name[1])
    return null
}
async function getArticle(url) {
    let article = get("a")
    if (get("m") === "1") {
        return (await Mercury.parse(url, {html: article})).content || ""
    } else {
        return article
    }
}
document.documentElement.style.fontSize = get("s") + "px"
let fontFamily = get("f")
if (fontFamily && fontFamily !== "System") {
    let fontPath = get("fp")
    if (!fontPath) {
        // Built-in fonts served from bundled assets
        var builtInFonts = {
            'OpenSans': 'fonts/OpenSans-Regular.ttf',
            'Roboto': 'fonts/Roboto-Regular.ttf',
            'SourceSerif': 'fonts/SourceSerif-Regular.ttf'
        }
        if (builtInFonts[fontFamily]) {
            fontPath = builtInFonts[fontFamily]
        }
    }
    if (fontPath) {
        var style = document.createElement('style')
        style.textContent = '@font-face { font-family: "' + fontFamily + '"; src: url("' + fontPath + '"); }'
        document.head.appendChild(style)
    }
    document.documentElement.style.fontFamily = '"' + fontFamily + '", sans-serif'
}
let theme = get("t")
if (theme !== null) document.documentElement.classList.add(theme === "1" ? "light" : "dark")
let url = get("u")
getArticle(url).then(article => {
    let domParser = new DOMParser()
    let dom = domParser.parseFromString(get("h"), "text/html")
    dom.getElementsByTagName("article")[0].innerHTML = article
    let baseUrl = url.split("/").slice(0, 3).join("/")
    for (let s of dom.getElementsByTagName("script")) {
        s.parentNode.removeChild(s)
    }
    for (let e of dom.querySelectorAll("*[src]")) {
        if (e.src && !e.src.startsWith("http")) {
            if (e.src.startsWith("/")) {
                e.src = baseUrl + e.src
            } else if (e.src.startsWith(":")) {
                e.src = "http" + e.src
            } else {
                e.src = baseUrl + "/" + e.src
            }
        }
    }
    for (let e of dom.querySelectorAll("*[href]")) {
        if (e.href && !e.href.startsWith("http")) {
            if (e.href.startsWith("/")) {
                e.href = baseUrl + e.href
            } else if (e.href.startsWith(":")) {
                e.href = "http" + e.href
            } else {
                e.href = baseUrl + "/" + e.href
            }
        }
    }
    let main = document.getElementById("main")
    main.innerHTML = dom.body.innerHTML
    main.classList.add("show")
})