-
-
Notifications
You must be signed in to change notification settings - Fork 346
/
Copy pathfind-webclient.js
86 lines (72 loc) · 3.32 KB
/
find-webclient.js
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
async function tryConnect(server) {
document.getElementById('connect-button').disabled = true;
try {
if (!server.startsWith("http")) {
server = "http://" + server;
}
const url = new URL("/System/Info/Public", server);
const response = await fetch(url);
if (response.ok && (await response.json()).Id) {
const htmlResponse = await fetch(server);
if (!htmlResponse.ok) {
throw new Error("Status not ok");
}
if (response.headers.get("content-security-policy")) {
// Sigh... If we just navigate to the URL, the server's CSP will block us loading other resources.
// So we have to parse the HTML, set a new base href, and then write it back to the page.
// We also have to override the history functions to make sure they use the correct URL.
console.log("Using CSP workaround");
const webUrl = htmlResponse.url.replace(/\/[^\/]*$/, "/");
const realUrl = window.location.href;
const html = await htmlResponse.text();
const parser = new DOMParser();
const doc = parser.parseFromString(html, "text/html");
const base = doc.createElement("base");
base.href = webUrl
doc.head.insertBefore(base, doc.head.firstChild);
const oldPushState = window.history.pushState;
window.history.pushState = function(state, title, url) {
url = (new URL(url, realUrl)).toString();
return oldPushState.call(window.history, state, title, url);
};
const oldReplaceState = window.history.replaceState;
window.history.replaceState = function(state, title, url) {
url = (new URL(url, realUrl)).toString();
return oldReplaceState.call(window.history, state, title, url);
};
document.open();
document.write((new XMLSerializer()).serializeToString(doc));
document.close();
} else {
console.log("Using normal navigation");
window.location = server;
}
await window.initCompleted;
window.jmpInfo.settings.main.userWebClient = server;
return true;
}
} catch (e) {
console.error(e);
document.getElementById('connect-button').disabled = false;
return false;
}
}
document.getElementById('connect-form').addEventListener('submit', async (e) => {
e.preventDefault();
const server = document.getElementById('address').value;
const result = await tryConnect(server);
if (!result) {
document.getElementById('backdrop').style.display = 'block';
}
});
document.getElementById('connect-fail-button').addEventListener('click', () => {
document.getElementById('backdrop').style.display = 'none';
});
// load the server if we have one
(async() => {
const savedServer = window.jmpInfo.settings.main.userWebClient;
if (!savedServer || !(await tryConnect(savedServer))) {
document.getElementById('splash').style.display = 'none';
document.getElementById('main').style.display = 'block';
}
})();