summaryrefslogtreecommitdiff
path: root/app/src/main/java/org/terst/feedreader/ui/ReaderWebView.kt
blob: 503f71e81e69ecf8abf7f8f6f9170684e36b93f7 (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
package org.terst.feedreader.ui

import android.content.Context
import android.view.View
import android.webkit.WebResourceRequest
import android.webkit.WebView
import android.webkit.WebViewClient

/**
 * Shared factory for the read-only, dark-themed article WebView used by both the
 * per-article ReaderScreen and LinkReaderScreen (for links tapped inside an article).
 * JS is always off -- extracted/feed HTML never needs it, and this keeps articles
 * from running arbitrary scripts. Link taps are intercepted rather than left to
 * WebView's default behavior (handing off to the system browser) so following a
 * link stays inside the app's own reader/extraction pipeline.
 */
fun newReaderWebView(context: Context, onOpenLink: (String) -> Unit): WebView =
    WebView(context).apply {
        settings.javaScriptEnabled = false
        overScrollMode = View.OVER_SCROLL_NEVER
        isVerticalScrollBarEnabled = false
        isHorizontalScrollBarEnabled = false
        webViewClient = object : WebViewClient() {
            override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest): Boolean {
                onOpenLink(request.url.toString())
                return true
            }
        }
    }

// Hardcoded dark palette, not @media (prefers-color-scheme) -- the app itself always
// forces dark colorScheme() regardless of system setting (see MainActivity), so reader
// content should match rather than following a system light/dark the app itself ignores.
fun wrapReaderHtml(bodyContent: String): String = """
    <html><head><meta name="viewport" content="width=device-width, initial-scale=1">
    <style>
    body { font-family: sans-serif; font-size: 18px; line-height: 1.5; padding: 16px;
           background-color: #121212; color: #e1e1e1; }
    a { color: #8ab4f8; }
    img { max-width: 100%; height: auto; }
    svg { max-width: 100%; }
    /* A decorative icon (section badge, share glyph, etc.) that slipped through
       extraction relies on the source site's own stylesheet for its real display
       size (commonly Tailwind classes like "h-5 w-5") -- which we don't load here.
       An <svg> with no explicit width/height falls back to the browser's default
       (much larger than any inline icon is meant to be, confirmed live: Ars
       Technica's per-article section-badge icon rendering "giant"). One WITH
       explicit width/height was deliberately sized by the page itself, so leave
       those alone -- just cap the ones that would otherwise size themselves. */
    svg:not([width]):not([height]) { max-width: 32px; max-height: 32px; }
    </style></head>
    <body>$bodyContent</body></html>
""".trimIndent()