mirror of
https://github.com/esphome/esphome.git
synced 2026-10-05 02:21:30 +00:00
[web_server] Shorten the offline hint
This commit is contained in:
@@ -2,7 +2,6 @@ from __future__ import annotations
|
||||
|
||||
import base64
|
||||
import gzip
|
||||
from html import escape as html_escape
|
||||
import logging
|
||||
import re
|
||||
|
||||
@@ -349,17 +348,13 @@ def build_index_html(config: ConfigType, offline_hint: bool = True) -> str:
|
||||
if js_url and offline_hint:
|
||||
# The interface is downloaded from the internet. Show a hint instead of a blank
|
||||
# page when the browser cannot reach it, which is common in WiFi AP mode.
|
||||
# Kept short: this string lives in flash on every build without captive_portal.
|
||||
hint = (
|
||||
f"Could not load the web interface from {js_url}. This browser needs internet "
|
||||
"access to download it. To serve it from the device instead (for example in "
|
||||
"WiFi access point mode), set local: true under web_server: in the YAML "
|
||||
"configuration and install the firmware again."
|
||||
"Could not download the web interface. This browser needs internet access, "
|
||||
"or set local: true under web_server: in the YAML and reinstall so the "
|
||||
"device serves it."
|
||||
)
|
||||
# Escape for a JS single quoted string inside a double quoted HTML attribute.
|
||||
hint_js = html_escape(
|
||||
hint.replace("\\", "\\\\").replace("'", "\\'"), quote=True
|
||||
)
|
||||
html += f'<script src="{js_url}" onerror="document.body.innerText=\'{hint_js}\'"></script>'
|
||||
html += f'<script src="{js_url}" onerror="document.body.innerText=\'{hint}\'"></script>'
|
||||
elif js_url:
|
||||
html += f'<script src="{js_url}"></script>'
|
||||
html += "</body></html>"
|
||||
|
||||
@@ -10,15 +10,8 @@ def test_build_index_html_has_offline_hint() -> None:
|
||||
{CONF_JS_URL: "https://oi.esphome.io/v2/www.js", CONF_CSS_URL: ""}
|
||||
)
|
||||
assert '<script src="https://oi.esphome.io/v2/www.js" onerror="' in html
|
||||
assert "document.body.innerText='Could not download the web interface." in html
|
||||
assert "local: true" in html
|
||||
assert "https://oi.esphome.io/v2/www.js. This browser needs internet" in html
|
||||
|
||||
|
||||
def test_build_index_html_escapes_hint() -> None:
|
||||
"""Quotes in the script URL cannot break out of the onerror attribute."""
|
||||
html = build_index_html({CONF_JS_URL: "http://x/a'b\"c.js", CONF_CSS_URL: ""})
|
||||
assert "onerror=\"document.body.innerText='" in html
|
||||
assert "a\\'b"c.js" in html
|
||||
|
||||
|
||||
def test_build_index_html_hint_disabled() -> None:
|
||||
|
||||
Reference in New Issue
Block a user