bookmarklet: use dynamic host and scheme from request headers

This commit is contained in:
blankie 2026-06-14 07:43:26 +00:00
parent 96603da142
commit af6ad6d391
3 changed files with 13 additions and 9 deletions

View file

@ -125,6 +125,7 @@ class GatewayHandler(BaseHTTPRequestHandler):
"body": body, "body": body,
"cookies": cookies, "cookies": cookies,
"gateway_host": self.headers.get("Host", f"localhost:{GATEWAY_PORT}"), "gateway_host": self.headers.get("Host", f"localhost:{GATEWAY_PORT}"),
"scheme": self.headers.get("X-Forwarded-Proto", "http"),
} }
try: try:

View file

@ -48,6 +48,7 @@ def _dispatch_inner(data):
query = data.get("query", {}) query = data.get("query", {})
body = data.get("body", {}) body = data.get("body", {})
gateway_host = data.get("gateway_host", "") gateway_host = data.get("gateway_host", "")
scheme = data.get("scheme", "http")
def extract_id(prefix): def extract_id(prefix):
try: try:
@ -72,7 +73,7 @@ def _dispatch_inner(data):
elif path == "/bookmark": elif path == "/bookmark":
return handle_bookmark(query) return handle_bookmark(query)
elif path == "/style": elif path == "/style":
return handle_style_form() return handle_style_form(gateway_host=gateway_host, scheme=scheme)
elif path == "/share/preview": elif path == "/share/preview":
return handle_share_preview() return handle_share_preview()
elif path == "/about": elif path == "/about":
@ -121,14 +122,14 @@ def _dispatch_inner(data):
pid = extract_id("/delete/") pid = extract_id("/delete/")
return handle_delete(pid) if pid is not None else _error(400) return handle_delete(pid) if pid is not None else _error(400)
elif path == "/style": elif path == "/style":
return handle_style_submit(body) return handle_style_submit(body, gateway_host=gateway_host, scheme=scheme)
elif path == "/style/reset": elif path == "/style/reset":
set_setting("custom_template", "") set_setting("custom_template", "")
return handle_style_form("Template reset to default.") return handle_style_form("Template reset to default.", gateway_host=gateway_host, scheme=scheme)
elif path == "/style/vacuum": elif path == "/style/vacuum":
from db import vacuum_db from db import vacuum_db
vacuum_db() vacuum_db()
return handle_style_form("Database vacuumed.") return handle_style_form("Database vacuumed.", gateway_host=gateway_host, scheme=scheme)
elif path == "/import": elif path == "/import":
return handle_import_submit(body) return handle_import_submit(body)
elif path == "/reindex": elif path == "/reindex":

View file

@ -5,7 +5,7 @@ from ._helpers import _respond, _csrf_field, _get_bookmark_token
from .subscriptions import _count_shared_pages from .subscriptions import _count_shared_pages
def handle_style_form(msg=""): def handle_style_form(msg="", gateway_host="", scheme="http"):
template = get_setting("custom_template") or DEFAULT_TEMPLATE template = get_setting("custom_template") or DEFAULT_TEMPLATE
name = get_site_name() name = get_site_name()
sharing = get_setting("sharing_enabled", "0") sharing = get_setting("sharing_enabled", "0")
@ -135,7 +135,7 @@ def handle_style_form(msg=""):
f"</form>" f"</form>"
f"<h2>bookmarklet</h2>" f"<h2>bookmarklet</h2>"
f"<p>Drag this link to your bookmarks bar. Click it on any page to index it instantly.</p>" f"<p>Drag this link to your bookmarks bar. Click it on any page to index it instantly.</p>"
f'<p><a href="javascript:void(fetch(\'http://localhost:8080/bookmark?url=\'+encodeURIComponent(location.href)+\'&token={_get_bookmark_token()}\').then(r=>r.text()).then(t=>alert(t)).catch(()=>alert(\'tinyweb not running\')))">+ save to {esc(name)}</a></p>' f'<p><a href="javascript:void(fetch(\'{esc(scheme)}://{esc(gateway_host or "localhost:8080")}/bookmark?url=\'+encodeURIComponent(location.href)+\'&token={_get_bookmark_token()}\').then(r=>r.text()).then(t=>alert(t)).catch(()=>alert(\'tinyweb not running\')))">+ save to {esc(name)}</a></p>'
f"<h2>reset</h2>" f"<h2>reset</h2>"
f'<form method="post" action="/style/reset" ' f'<form method="post" action="/style/reset" '
f'onsubmit="return confirm(\'Reset the template to default? Your custom template will be lost.\')">' f'onsubmit="return confirm(\'Reset the template to default? Your custom template will be lost.\')">'
@ -153,7 +153,7 @@ def handle_style_form(msg=""):
) )
def handle_style_submit(body): def handle_style_submit(body, gateway_host="", scheme="http"):
template = body.get("template", [""])[0].replace("\r\n", "\n").replace("\r", "\n") template = body.get("template", [""])[0].replace("\r\n", "\n").replace("\r", "\n")
name = body.get("site_name", ["tinyweb"])[0].strip() name = body.get("site_name", ["tinyweb"])[0].strip()
sharing = "1" if body.get("sharing_enabled") else "0" sharing = "1" if body.get("sharing_enabled") else "0"
@ -192,7 +192,8 @@ def handle_style_submit(body):
from handlers import forum_plugin from handlers import forum_plugin
if forum_enabled == "1" and forum_plugin is None: if forum_enabled == "1" and forum_plugin is None:
return handle_style_form( return handle_style_form(
"Forum plugin not installed. Run: pip install tinyweb-forum" "Forum plugin not installed. Run: pip install tinyweb-forum",
gateway_host=gateway_host, scheme=scheme,
) )
if forum_enabled == "1": if forum_enabled == "1":
forum_plugin.enable() forum_plugin.enable()
@ -208,7 +209,8 @@ def handle_style_submit(body):
pass pass
set_setting("forum_enabled", forum_enabled) set_setting("forum_enabled", forum_enabled)
templates_mod.FORUM_ENABLED = (forum_enabled == "1") templates_mod.FORUM_ENABLED = (forum_enabled == "1")
return handle_style_form("Saved. Restart TinyWeb for mesh network changes to take effect.") return handle_style_form("Saved. Restart TinyWeb for mesh network changes to take effect.",
gateway_host=gateway_host, scheme=scheme)
def handle_about(): def handle_about():