|
@@ -55,16 +55,6 @@
|
|
|
<script src="https://cdn.jsdelivr.net/npm/bs-custom-file-input/dist/bs-custom-file-input.js"></script>
|
|
|
|
|
|
<script>
|
|
|
- // https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript
|
|
|
- function getParameterByName(name, url) {
|
|
|
- if (!url) url = window.location.href;
|
|
|
- name = name.replace(/[\[\]]/g, '\\$&');
|
|
|
- var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),
|
|
|
- results = regex.exec(url);
|
|
|
- if (!results) return null;
|
|
|
- if (!results[2]) return '';
|
|
|
- return decodeURIComponent(results[2].replace(/\+/g, ' '));
|
|
|
- }
|
|
|
|
|
|
/*
|
|
|
* Check given `backend_addr` is a http backend
|
|
@@ -73,6 +63,11 @@
|
|
|
* is_http_backend('http://localhost:8080/io').then(function(http_backend){ });
|
|
|
* */
|
|
|
function is_http_backend(backend_addr) {
|
|
|
+ var url = new URL(backend_addr);
|
|
|
+ var protocol = url.protocol || window.location.protocol;
|
|
|
+ url.protocol = protocol.replace('wss', 'https').replace('ws', 'http');
|
|
|
+ backend_addr = url.href;
|
|
|
+
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
$.get(backend_addr, {test: 1}, 'html').done(function (data) {
|
|
|
resolve(data === 'ok');
|
|
@@ -87,29 +82,25 @@
|
|
|
bsCustomFileInput.init()
|
|
|
});
|
|
|
|
|
|
- function get_ws_addr() {
|
|
|
- if (getParameterByName('_pywebio_ws_url')) {
|
|
|
- return getParameterByName('_pywebio_ws_url');
|
|
|
- } else {
|
|
|
- var p = window.location.pathname.lastIndexOf('/');
|
|
|
- var pathname = '';
|
|
|
- if (p !== -1)
|
|
|
- pathname = window.location.pathname.substring(0, p);
|
|
|
- return "ws://" + window.location.host + pathname + "/io";
|
|
|
- }
|
|
|
+ // 获取后端API地址
|
|
|
+ function get_backend_addr() {
|
|
|
+ const url = new URLSearchParams(window.location.href);
|
|
|
+ var uri = url.get('_pywebio_addr') || './io';
|
|
|
+ return new URL(uri, window.location.href).href;
|
|
|
}
|
|
|
|
|
|
CodeMirror.modeURL = "https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.52.2/mode/%N/%N.min.js";
|
|
|
|
|
|
var md_body = $('#markdown-body');
|
|
|
|
|
|
- const debug = getParameterByName('_pywebio_debug');
|
|
|
- is_http_backend('./io').then(function (http_backend) {
|
|
|
+ const debug = new URLSearchParams(window.location.href).get('_pywebio_debug');
|
|
|
+ const backend_addr = get_backend_addr();
|
|
|
+ is_http_backend(backend_addr).then(function (http_backend) {
|
|
|
var session;
|
|
|
if (http_backend)
|
|
|
- session = new WebIO.HttpWebIOSession('./io');
|
|
|
+ session = new WebIO.HttpWebIOSession(backend_addr);
|
|
|
else
|
|
|
- session = new WebIO.WebSocketWebIOSession(get_ws_addr());
|
|
|
+ session = new WebIO.WebSocketWebIOSession(backend_addr);
|
|
|
var ctrl = new WebIO.WebIOController(session, md_body, $('#input-container'));
|
|
|
session.start_session(debug);
|
|
|
});
|