summaryrefslogtreecommitdiffhomepage
path: root/webui/src/web.ts
blob: 97f1908aab626ed82bd31c995ccfbcfabefc944c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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);
});
})
}