diff options
Diffstat (limited to 'webui/src')
| -rw-r--r-- | webui/src/index.tsx | 3 | ||||
| -rw-r--r-- | webui/src/web.ts | 90 |
2 files changed, 93 insertions, 0 deletions
diff --git a/webui/src/index.tsx b/webui/src/index.tsx new file mode 100644 index 000000000..0a80fbdf0 --- /dev/null +++ b/webui/src/index.tsx @@ -0,0 +1,3 @@ +const rootEl = document.createElement("div") +rootEl.textContent = "hello dev" +document.body.append(rootEl) diff --git a/webui/src/web.ts b/webui/src/web.ts new file mode 100644 index 000000000..97f1908aa --- /dev/null +++ b/webui/src/web.ts @@ -0,0 +1,90 @@ +export function run() { + +const advertiseExitNode = {{ .AdvertiseExitNode }}; +const isUnraid = {{ .IsUnraid }}; +const unraidCsrfToken = "{{ .UnraidToken }}"; +let fetchingUrl = false; +var data = { +AdvertiseRoutes: "{{ .AdvertiseRoutes }}", +AdvertiseExitNode: advertiseExitNode, +Reauthenticate: false, +ForceLogout: false +}; + +function postData(e) { +e.preventDefault(); + +if (fetchingUrl) { + return; +} + +fetchingUrl = true; +const urlParams = new URLSearchParams(window.location.search); +const token = urlParams.get("SynoToken"); +const nextParams = new URLSearchParams({ up: true }); +if (token) { + nextParams.set("SynoToken", token) +} +const nextUrl = new URL(window.location); +nextUrl.search = nextParams.toString() + +let body = JSON.stringify(data); +let contentType = "application/json"; + +if (isUnraid) { + const params = new URLSearchParams(); + params.append("csrf_token", unraidCsrfToken); + params.append("ts_data", JSON.stringify(data)); + + body = params.toString(); + contentType = "application/x-www-form-urlencoded;charset=UTF-8"; +} + +const url = nextUrl.toString(); +fetch(url, { + method: "POST", + headers: { + "Accept": "application/json", + "Content-Type": contentType, + }, + body: body +}).then(res => res.json()).then(res => { + fetchingUrl = false; + const err = res["error"]; + if (err) { + throw new Error(err); + } + const url = res["url"]; + if (url) { + if(isUnraid) { + window.open(url, "_blank"); + } else { + document.location.href = url; + } + } else { + location.reload(); + } +}).catch(err => { + alert("Failed operation: " + err.message); +}); +} + +document.querySelectorAll(".js-loginButton").forEach(function (el){ +el.addEventListener("click", function(e) { + data.Reauthenticate = true; + postData(e); +}); +}) +document.querySelectorAll(".js-logoutButton").forEach(function(el) { +el.addEventListener("click", function (e) { + data.ForceLogout = true; + postData(e); +}); +}) +document.querySelectorAll(".js-advertiseExitNode").forEach(function (el) { +el.addEventListener("click", function(e) { + data.AdvertiseExitNode = !advertiseExitNode; + postData(e); +}); +}) +}
\ No newline at end of file |
