|
@@ -1,3 +1,24 @@
|
|
|
|
+function loadScript(src) {
|
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
|
+ const s = document.createElement('script');
|
|
|
|
+ s.src = src;
|
|
|
|
+ s.onload = resolve;
|
|
|
|
+ s.onerror = reject;
|
|
|
|
+ document.head.appendChild(s);
|
|
|
|
+ });
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+async function createApi() {
|
|
|
|
+ await loadScript('qrc:///qtwebchannel/qwebchannel.js');
|
|
|
|
+ const channel = await new Promise((resolve) => {
|
|
|
|
+ /*global QWebChannel */
|
|
|
|
+ new QWebChannel(window.qt.webChannelTransport, resolve);
|
|
|
|
+ });
|
|
|
|
+ return channel.objects;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+window.apiPromise = createApi();
|
|
|
|
+
|
|
async function tryConnect(server) {
|
|
async function tryConnect(server) {
|
|
document.getElementById('connect-button').disabled = true;
|
|
document.getElementById('connect-button').disabled = true;
|
|
|
|
|
|
@@ -14,36 +35,47 @@ async function tryConnect(server) {
|
|
throw new Error("Status not ok");
|
|
throw new Error("Status not ok");
|
|
}
|
|
}
|
|
|
|
|
|
- // 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.
|
|
|
|
- const webUrl = htmlResponse.url.replace(/\/[^\/]*$/, "/");
|
|
|
|
- const realUrl = window.location.href;
|
|
|
|
|
|
+ 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 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);
|
|
|
|
- };
|
|
|
|
|
|
+ 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();
|
|
|
|
|
|
+ document.open();
|
|
|
|
+ document.write((new XMLSerializer()).serializeToString(doc));
|
|
|
|
+ document.close();
|
|
|
|
+ } else {
|
|
|
|
+ console.log("Using normal navigation");
|
|
|
|
+ window.location = server;
|
|
|
|
+ }
|
|
|
|
|
|
window.localStorage.setItem("saved-server", server);
|
|
window.localStorage.setItem("saved-server", server);
|
|
|
|
+
|
|
|
|
+ const api = await window.apiPromise;
|
|
|
|
+ await new Promise(resolve => {
|
|
|
|
+ api.settings.setValue('main', 'userWebClient', server, resolve);
|
|
|
|
+ });
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
} catch (e) {
|
|
} catch (e) {
|
|
@@ -66,10 +98,14 @@ document.getElementById('connect-fail-button').addEventListener('click', () => {
|
|
document.getElementById('backdrop').style.display = 'none';
|
|
document.getElementById('backdrop').style.display = 'none';
|
|
});
|
|
});
|
|
|
|
|
|
-const savedServer = window.localStorage.getItem("saved-server");
|
|
|
|
|
|
|
|
// load the server if we have one
|
|
// load the server if we have one
|
|
(async() => {
|
|
(async() => {
|
|
|
|
+ const api = await window.apiPromise;
|
|
|
|
+ const savedServer = await new Promise(resolve => {
|
|
|
|
+ api.settings.value('main', 'userWebClient', resolve);
|
|
|
|
+ });
|
|
|
|
+
|
|
if (!savedServer || !(await tryConnect(savedServer))) {
|
|
if (!savedServer || !(await tryConnect(savedServer))) {
|
|
document.getElementById('splash').style.display = 'none';
|
|
document.getElementById('splash').style.display = 'none';
|
|
document.getElementById('main').style.display = 'block';
|
|
document.getElementById('main').style.display = 'block';
|