Set base inside of iframes (#1766)

* Set base inside of iframes

* Add `system.getURLPrefix` syscall
This commit is contained in:
MrMugame
2026-01-09 15:30:34 +01:00
committed by GitHub
parent 77801a9a6a
commit 4cd12c8ae7
6 changed files with 32 additions and 3 deletions
+6 -2
View File
@@ -1,4 +1,4 @@
import { useEffect, useRef } from "preact/hooks";
import { useEffect, useMemo, useRef } from "preact/hooks";
import type { Client } from "../client.ts";
import type { PanelConfig } from "../types/ui.ts";
import { panelHtml } from "./panel_html.ts";
@@ -12,6 +12,10 @@ export function Panel({
}) {
const iFrameRef = useRef<HTMLIFrameElement>(null);
const html = useMemo(() => {
return panelHtml.replace("{{.HostPrefix}}", document.baseURI);
}, []);
function updateContent() {
if (!iFrameRef.current?.contentWindow) {
return;
@@ -87,7 +91,7 @@ export function Panel({
return (
<div className="sb-panel" style={{ flex: config.mode }}>
<iframe
srcDoc={panelHtml}
srcDoc={html}
ref={iFrameRef}
style={{ visibility: "hidden" }}
onLoad={() => iFrameRef.current!.style.visibility = "visible"}
+1 -1
View File
@@ -2,7 +2,7 @@ export const panelHtml = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<base target="_top">
<base target="_top" href="{{.HostPrefix}}">
<meta name='color-scheme' content='dark light'>
<script>
const pendingRequests = new Map();
+4
View File
@@ -218,6 +218,10 @@ export class DocumentEditor {
script.text = documentEditorJS;
doc.head.prepend(script);
const base = doc.createElement("base");
base.href = document.baseURI;
doc.head.prepend(base);
// This isn't great, but the only way to get the up-to-date theme without
// using media queries
const theme = document.querySelector("html")?.getAttribute("data-theme");
+5
View File
@@ -141,6 +141,11 @@ export function systemSyscalls(
"system.getMode": () => {
return readOnlyMode ? "ro" : "rw";
},
"system.getURLPrefix": () => {
const url = new URL(document.baseURI);
return url.pathname;
},
"system.getVersion": () => {
return publicVersion;
},
+7
View File
@@ -61,6 +61,13 @@ export function getMode(): Promise<"ro" | "rw"> {
return syscall("system.getMode");
}
/**
* Returns the prefix set by SB_URL_PREFIX or "/" if the variable isn't set
*/
export function getURLPrefix(): Promise<string> {
return syscall("system.getURLPrefix");
}
/**
* Returns the SilverBullet version
*/
+9
View File
@@ -55,6 +55,15 @@ local mode = system.getMode()
print("System mode: " .. mode)
```
### system.getURLPrefix()
Returns the prefix set by [[Install/Configuration|SB_URL_PREFIX]] or "/" if the variable isn't set
Example:
```lua
local prefix = system.getURLPrefix()
print("Prefix: " .. prefix)
```
### system.getVersion()
Returns the SilverBullet version.