From a6bde0aa87d843d884d0295537a327a9108fbb4e Mon Sep 17 00:00:00 2001 From: blankie Date: Sun, 14 Jun 2026 07:43:26 +0000 Subject: [PATCH] bookmarklet: use dynamic host and scheme from request headers --- gateway.py | 1 + handlers/__init__.py | 9 +++++---- handlers/customize.py | 12 +++++++----- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/gateway.py b/gateway.py index 7d3fd53..02a2d95 100644 --- a/gateway.py +++ b/gateway.py @@ -125,6 +125,7 @@ class GatewayHandler(BaseHTTPRequestHandler): "body": body, "cookies": cookies, "gateway_host": self.headers.get("Host", f"localhost:{GATEWAY_PORT}"), + "scheme": self.headers.get("X-Forwarded-Proto", "http"), } try: diff --git a/handlers/__init__.py b/handlers/__init__.py index f05dabb..ba91d9a 100644 --- a/handlers/__init__.py +++ b/handlers/__init__.py @@ -48,6 +48,7 @@ def _dispatch_inner(data): query = data.get("query", {}) body = data.get("body", {}) gateway_host = data.get("gateway_host", "") + scheme = data.get("scheme", "http") def extract_id(prefix): try: @@ -72,7 +73,7 @@ def _dispatch_inner(data): elif path == "/bookmark": return handle_bookmark(query) elif path == "/style": - return handle_style_form() + return handle_style_form(gateway_host=gateway_host, scheme=scheme) elif path == "/share/preview": return handle_share_preview() elif path == "/about": @@ -121,14 +122,14 @@ def _dispatch_inner(data): pid = extract_id("/delete/") return handle_delete(pid) if pid is not None else _error(400) elif path == "/style": - return handle_style_submit(body) + return handle_style_submit(body, gateway_host=gateway_host, scheme=scheme) elif path == "/style/reset": 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": from db import 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": return handle_import_submit(body) elif path == "/reindex": diff --git a/handlers/customize.py b/handlers/customize.py index 1f2568b..6e158ed 100644 --- a/handlers/customize.py +++ b/handlers/customize.py @@ -5,7 +5,7 @@ from ._helpers import _respond, _csrf_field, _get_bookmark_token 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 name = get_site_name() sharing = get_setting("sharing_enabled", "0") @@ -135,7 +135,7 @@ def handle_style_form(msg=""): f"" f"

bookmarklet

" f"

Drag this link to your bookmarks bar. Click it on any page to index it instantly.

" - f'

+ save to {esc(name)}

' + f'

r.text()).then(t=>alert(t)).catch(()=>alert(\'tinyweb not running\')))">+ save to {esc(name)}

' f"

reset

" f'
' @@ -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") name = body.get("site_name", ["tinyweb"])[0].strip() sharing = "1" if body.get("sharing_enabled") else "0" @@ -192,7 +192,8 @@ def handle_style_submit(body): from handlers import forum_plugin if forum_enabled == "1" and forum_plugin is None: 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": forum_plugin.enable() @@ -208,7 +209,8 @@ def handle_style_submit(body): pass set_setting("forum_enabled", forum_enabled) 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():