mirror of
https://github.com/mofeng-git/One-KVM.git
synced 2026-01-31 18:11:54 +08:00
refactoring
This commit is contained in:
@@ -51,7 +51,7 @@ function __setAppText() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function __loadKvmdInfo() {
|
function __loadKvmdInfo() {
|
||||||
tools.httpGet("/api/info?fields=auth,meta,extras", function(http) {
|
tools.httpGet("/api/info", {"fields": "auth,meta,extras"}, function(http) {
|
||||||
if (http.status === 200) {
|
if (http.status === 200) {
|
||||||
let info = JSON.parse(http.responseText).result;
|
let info = JSON.parse(http.responseText).result;
|
||||||
|
|
||||||
@@ -121,7 +121,7 @@ function __makeApp(id, path, icon, name) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function __logout() {
|
function __logout() {
|
||||||
tools.httpPost("/api/auth/logout", function(http) {
|
tools.httpPost("/api/auth/logout", null, function(http) {
|
||||||
if (http.status === 200 || http.status === 401 || http.status === 403) {
|
if (http.status === 200 || http.status === 401 || http.status === 403) {
|
||||||
document.location.href = "/login";
|
document.location.href = "/login";
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ export function main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function __loadKvmdInfo() {
|
function __loadKvmdInfo() {
|
||||||
tools.httpGet("/api/info", function(http) {
|
tools.httpGet("/api/info", null, function(http) {
|
||||||
if (http.status === 200) {
|
if (http.status === 200) {
|
||||||
let ipmi_port = JSON.parse(http.responseText).result.extras.ipmi.port;
|
let ipmi_port = JSON.parse(http.responseText).result.extras.ipmi.port;
|
||||||
let make_item = (comment, ipmi, api) => `
|
let make_item = (comment, ipmi, api) => `
|
||||||
|
|||||||
@@ -73,7 +73,7 @@ export function Atx(__recorder) {
|
|||||||
|
|
||||||
var __clickButton = function(button, confirm_msg) {
|
var __clickButton = function(button, confirm_msg) {
|
||||||
let click_button = function() {
|
let click_button = function() {
|
||||||
tools.httpPost(`/api/atx/click?button=${button}`, function(http) {
|
tools.httpPost("/api/atx/click", {"button": button}, function(http) {
|
||||||
if (http.status === 409) {
|
if (http.status === 409) {
|
||||||
wm.error("Performing another ATX operation for other client.<br>Please try again later");
|
wm.error("Performing another ATX operation for other client.<br>Please try again later");
|
||||||
} else if (http.status !== 200) {
|
} else if (http.status !== 200) {
|
||||||
|
|||||||
@@ -181,7 +181,7 @@ export function Gpio(__recorder) {
|
|||||||
confirm = el.getAttribute("data-confirm-off");
|
confirm = el.getAttribute("data-confirm-off");
|
||||||
}
|
}
|
||||||
let act = () => {
|
let act = () => {
|
||||||
__sendPost(`/api/gpio/switch?channel=${channel}&state=${to}`);
|
__sendPost("/api/gpio/switch", {"channel": channel, "state": to});
|
||||||
__recorder.recordGpioSwitchEvent(channel, to);
|
__recorder.recordGpioSwitchEvent(channel, to);
|
||||||
};
|
};
|
||||||
if (confirm) {
|
if (confirm) {
|
||||||
@@ -201,7 +201,7 @@ export function Gpio(__recorder) {
|
|||||||
let channel = el.getAttribute("data-channel");
|
let channel = el.getAttribute("data-channel");
|
||||||
let confirm = el.getAttribute("data-confirm");
|
let confirm = el.getAttribute("data-confirm");
|
||||||
let act = () => {
|
let act = () => {
|
||||||
__sendPost(`/api/gpio/pulse?channel=${channel}`);
|
__sendPost("/api/gpio/pulse", {"channel": channel});
|
||||||
__recorder.recordGpioPulseEvent(channel);
|
__recorder.recordGpioPulseEvent(channel);
|
||||||
};
|
};
|
||||||
if (confirm) {
|
if (confirm) {
|
||||||
|
|||||||
@@ -253,7 +253,7 @@ export function Hid(__getGeometry, __recorder) {
|
|||||||
|
|
||||||
tools.debug(`HID: paste-as-keys ${keymap}: ${text}`);
|
tools.debug(`HID: paste-as-keys ${keymap}: ${text}`);
|
||||||
|
|
||||||
tools.httpPost(`/api/hid/print?limit=0&keymap=${keymap}`, function(http) {
|
tools.httpPost("/api/hid/print", {"limit": 0, "keymap": keymap}, function(http) {
|
||||||
tools.el.setEnabled($("hid-pak-text"), true);
|
tools.el.setEnabled($("hid-pak-text"), true);
|
||||||
tools.el.setEnabled($("hid-pak-button"), true);
|
tools.el.setEnabled($("hid-pak-button"), true);
|
||||||
tools.el.setEnabled($("hid-pak-keymap-selector"), true);
|
tools.el.setEnabled($("hid-pak-keymap-selector"), true);
|
||||||
@@ -286,7 +286,7 @@ export function Hid(__getGeometry, __recorder) {
|
|||||||
|
|
||||||
var __clickOutputsRadio = function(hid) {
|
var __clickOutputsRadio = function(hid) {
|
||||||
let output = tools.radio.getValue(`hid-outputs-${hid}-radio`);
|
let output = tools.radio.getValue(`hid-outputs-${hid}-radio`);
|
||||||
tools.httpPost(`/api/hid/set_params?${hid}_output=${output}`, function(http) {
|
tools.httpPost("/api/hid/set_params", {[`${hid}_output`]: output}, function(http) {
|
||||||
if (http.status !== 200) {
|
if (http.status !== 200) {
|
||||||
wm.error("Can't configure HID:<br>", http.responseText);
|
wm.error("Can't configure HID:<br>", http.responseText);
|
||||||
}
|
}
|
||||||
@@ -295,7 +295,7 @@ export function Hid(__getGeometry, __recorder) {
|
|||||||
|
|
||||||
var __clickJigglerSwitch = function() {
|
var __clickJigglerSwitch = function() {
|
||||||
let enabled = $("hid-jiggler-switch").checked;
|
let enabled = $("hid-jiggler-switch").checked;
|
||||||
tools.httpPost(`/api/hid/set_params?jiggler=${enabled}`, function(http) {
|
tools.httpPost("/api/hid/set_params", {"jiggler": enabled}, function(http) {
|
||||||
if (http.status !== 200) {
|
if (http.status !== 200) {
|
||||||
wm.error(`Can't ${enabled ? "enabled" : "disable"} mouse juggler:<br>`, http.responseText);
|
wm.error(`Can't ${enabled ? "enabled" : "disable"} mouse juggler:<br>`, http.responseText);
|
||||||
}
|
}
|
||||||
@@ -304,7 +304,7 @@ export function Hid(__getGeometry, __recorder) {
|
|||||||
|
|
||||||
var __clickConnectSwitch = function() {
|
var __clickConnectSwitch = function() {
|
||||||
let connected = $("hid-connect-switch").checked;
|
let connected = $("hid-connect-switch").checked;
|
||||||
tools.httpPost(`/api/hid/set_connected?connected=${connected}`, function(http) {
|
tools.httpPost("/api/hid/set_connected", {"connected": connected}, function(http) {
|
||||||
if (http.status !== 200) {
|
if (http.status !== 200) {
|
||||||
wm.error(`Can't ${connected ? "connect" : "disconnect"} HID:<br>`, http.responseText);
|
wm.error(`Can't ${connected ? "connect" : "disconnect"} HID:<br>`, http.responseText);
|
||||||
}
|
}
|
||||||
@@ -314,7 +314,7 @@ export function Hid(__getGeometry, __recorder) {
|
|||||||
var __clickResetButton = function() {
|
var __clickResetButton = function() {
|
||||||
wm.confirm("Are you sure you want to reset HID (keyboard & mouse)?").then(function(ok) {
|
wm.confirm("Are you sure you want to reset HID (keyboard & mouse)?").then(function(ok) {
|
||||||
if (ok) {
|
if (ok) {
|
||||||
tools.httpPost("/api/hid/reset", function(http) {
|
tools.httpPost("/api/hid/reset", null, function(http) {
|
||||||
if (http.status !== 200) {
|
if (http.status !== 200) {
|
||||||
wm.error("HID reset error:<br>", http.responseText);
|
wm.error("HID reset error:<br>", http.responseText);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -86,9 +86,9 @@ export function Msd() {
|
|||||||
|
|
||||||
var __clickRemoveButton = function() {
|
var __clickRemoveButton = function() {
|
||||||
let name = $("msd-image-selector").value;
|
let name = $("msd-image-selector").value;
|
||||||
wm.confirm(`Are you sure you want to remove the image<br><b>${name}</b> from PiKVM?`).then(function(ok) {
|
wm.confirm(`Are you sure you want to remove the image<br><b>${tools.escape(name)}</b> from PiKVM?`).then(function(ok) {
|
||||||
if (ok) {
|
if (ok) {
|
||||||
tools.httpPost(`/api/msd/remove?image=${name}`, function(http) {
|
tools.httpPost("/api/msd/remove", {"image": name}, function(http) {
|
||||||
if (http.status !== 200) {
|
if (http.status !== 200) {
|
||||||
wm.error("Can't remove image:<br>", http.responseText);
|
wm.error("Can't remove image:<br>", http.responseText);
|
||||||
}
|
}
|
||||||
@@ -98,7 +98,7 @@ export function Msd() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
var __sendParam = function(name, value) {
|
var __sendParam = function(name, value) {
|
||||||
tools.httpPost(`/api/msd/set_params?${name}=${encodeURIComponent(value)}`, function(http) {
|
tools.httpPost("/api/msd/set_params", {[name]: value}, function(http) {
|
||||||
if (http.status !== 200) {
|
if (http.status !== 200) {
|
||||||
wm.error("Can't configure MSD:<br>", http.responseText);
|
wm.error("Can't configure MSD:<br>", http.responseText);
|
||||||
}
|
}
|
||||||
@@ -164,7 +164,7 @@ export function Msd() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
var __clickConnectButton = function(connected) {
|
var __clickConnectButton = function(connected) {
|
||||||
tools.httpPost(`/api/msd/set_connected?connected=${connected}`, function(http) {
|
tools.httpPost("/api/msd/set_connected", {"connected": connected}, function(http) {
|
||||||
if (http.status !== 200) {
|
if (http.status !== 200) {
|
||||||
wm.error("Switch error:<br>", http.responseText);
|
wm.error("Switch error:<br>", http.responseText);
|
||||||
}
|
}
|
||||||
@@ -177,7 +177,7 @@ export function Msd() {
|
|||||||
var __clickResetButton = function() {
|
var __clickResetButton = function() {
|
||||||
wm.confirm("Are you sure you want to reset Mass Storage Drive?").then(function(ok) {
|
wm.confirm("Are you sure you want to reset Mass Storage Drive?").then(function(ok) {
|
||||||
if (ok) {
|
if (ok) {
|
||||||
tools.httpPost("/api/msd/reset", function(http) {
|
tools.httpPost("/api/msd/reset", null, function(http) {
|
||||||
if (http.status !== 200) {
|
if (http.status !== 200) {
|
||||||
wm.error("MSD reset error:<br>", http.responseText);
|
wm.error("MSD reset error:<br>", http.responseText);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -161,13 +161,15 @@ export function Ocr(__getGeometry) {
|
|||||||
tools.el.setEnabled($("stream-ocr-button"), false);
|
tools.el.setEnabled($("stream-ocr-button"), false);
|
||||||
tools.el.setEnabled($("stream-ocr-lang-selector"), false);
|
tools.el.setEnabled($("stream-ocr-lang-selector"), false);
|
||||||
$("stream-ocr-led").className = "led-yellow-rotating-fast";
|
$("stream-ocr-led").className = "led-yellow-rotating-fast";
|
||||||
|
let params = {
|
||||||
let lang = $("stream-ocr-lang-selector").value;
|
"ocr": 1,
|
||||||
let url = `/api/streamer/snapshot?ocr=1&ocr_langs=${lang}`;
|
"ocr_langs": $("stream-ocr-lang-selector").value,
|
||||||
url += `&ocr_left=${__selection.left}&ocr_top=${__selection.top}`;
|
"ocr_left": __selection.left,
|
||||||
url += `&ocr_right=${__selection.right}&ocr_bottom=${__selection.bottom}`;
|
"ocr_top": __selection.top,
|
||||||
|
"ocr_right": __selection.right,
|
||||||
tools.httpGet(url, function(http) {
|
"orc_bottom": __selection.bottom,
|
||||||
|
};
|
||||||
|
tools.httpGet("/api/streamer/snapshot", params, function(http) {
|
||||||
if (http.status === 200) {
|
if (http.status === 200) {
|
||||||
wm.copyTextToClipboard(http.responseText);
|
wm.copyTextToClipboard(http.responseText);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -280,7 +280,7 @@ export function Recorder() {
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
} else if (event.event_type === "print") {
|
} else if (event.event_type === "print") {
|
||||||
tools.httpPost("/api/hid/print?limit=0", function(http) {
|
tools.httpPost("/api/hid/print", {"limit": 0}, function(http) {
|
||||||
if (http.status === 413) {
|
if (http.status === 413) {
|
||||||
wm.error("Too many text for paste!");
|
wm.error("Too many text for paste!");
|
||||||
__stopProcess();
|
__stopProcess();
|
||||||
@@ -294,7 +294,7 @@ export function Recorder() {
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
} else if (event.event_type === "atx_button") {
|
} else if (event.event_type === "atx_button") {
|
||||||
tools.httpPost(`/api/atx/click?button=${event.event.button}`, function(http) {
|
tools.httpPost("/api/atx/click", {"button": event.event.button}, function(http) {
|
||||||
if (http.status !== 200) {
|
if (http.status !== 200) {
|
||||||
wm.error("ATX error:<br>", http.responseText);
|
wm.error("ATX error:<br>", http.responseText);
|
||||||
__stopProcess();
|
__stopProcess();
|
||||||
@@ -306,12 +306,14 @@ export function Recorder() {
|
|||||||
|
|
||||||
} else if (["gpio_switch", "gpio_pulse"].includes(event.event_type)) {
|
} else if (["gpio_switch", "gpio_pulse"].includes(event.event_type)) {
|
||||||
let path = "/api/gpio";
|
let path = "/api/gpio";
|
||||||
|
let params = {"channel": event.event.channel};
|
||||||
if (event.event_type === "gpio_switch") {
|
if (event.event_type === "gpio_switch") {
|
||||||
path += `/switch?channel=${event.event.channel}&state=${event.event.to}`;
|
path += "/switch";
|
||||||
|
params["state"] = event.event.to;
|
||||||
} else { // gpio_pulse
|
} else { // gpio_pulse
|
||||||
path += `/pulse?channel=${event.event.channel}`;
|
path += "/pulse";
|
||||||
}
|
}
|
||||||
tools.httpPost(path, function(http) {
|
tools.httpPost(path, params, function(http) {
|
||||||
if (http.status !== 200) {
|
if (http.status !== 200) {
|
||||||
wm.error("GPIO error:<br>", http.responseText);
|
wm.error("GPIO error:<br>", http.responseText);
|
||||||
__stopProcess();
|
__stopProcess();
|
||||||
|
|||||||
@@ -280,7 +280,7 @@ export function Session() {
|
|||||||
$("link-led").className = "led-yellow";
|
$("link-led").className = "led-yellow";
|
||||||
$("link-led").title = "Connecting...";
|
$("link-led").title = "Connecting...";
|
||||||
|
|
||||||
tools.httpGet("/api/auth/check", function(http) {
|
tools.httpGet("/api/auth/check", null, function(http) {
|
||||||
if (http.status === 200) {
|
if (http.status === 200) {
|
||||||
__ws = new WebSocket(`${tools.is_https ? "wss" : "ws"}://${location.host}/api/ws`);
|
__ws = new WebSocket(`${tools.is_https ? "wss" : "ws"}://${location.host}/api/ws`);
|
||||||
__ws.sendHidEvent = (event) => __sendHidEvent(__ws, event.event_type, event.event);
|
__ws.sendHidEvent = (event) => __sendHidEvent(__ws, event.event_type, event.event);
|
||||||
|
|||||||
@@ -295,7 +295,7 @@ export function Streamer() {
|
|||||||
wm.confirm("Are you sure you want to reset stream?").then(function (ok) {
|
wm.confirm("Are you sure you want to reset stream?").then(function (ok) {
|
||||||
if (ok) {
|
if (ok) {
|
||||||
__resetStream();
|
__resetStream();
|
||||||
tools.httpPost("/api/streamer/reset", function(http) {
|
tools.httpPost("/api/streamer/reset", null, function(http) {
|
||||||
if (http.status !== 200) {
|
if (http.status !== 200) {
|
||||||
wm.error("Can't reset stream:<br>", http.responseText);
|
wm.error("Can't reset stream:<br>", http.responseText);
|
||||||
}
|
}
|
||||||
@@ -305,7 +305,7 @@ export function Streamer() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
var __sendParam = function(name, value) {
|
var __sendParam = function(name, value) {
|
||||||
tools.httpPost(`/api/streamer/set_params?${name}=${value}`, function(http) {
|
tools.httpPost("/api/streamer/set_params", {[name]: value}, function(http) {
|
||||||
if (http.status !== 200) {
|
if (http.status !== 200) {
|
||||||
wm.error("Can't configure stream:<br>", http.responseText);
|
wm.error("Can't configure stream:<br>", http.responseText);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ function __login() {
|
|||||||
} else {
|
} else {
|
||||||
let passwd = $("passwd-input").value + $("code-input").value;
|
let passwd = $("passwd-input").value + $("code-input").value;
|
||||||
let body = `user=${encodeURIComponent(user)}&passwd=${encodeURIComponent(passwd)}`;
|
let body = `user=${encodeURIComponent(user)}&passwd=${encodeURIComponent(passwd)}`;
|
||||||
tools.httpPost("/api/auth/login", function(http) {
|
tools.httpPost("/api/auth/login", null, function(http) {
|
||||||
if (http.status === 200) {
|
if (http.status === 200) {
|
||||||
document.location.href = "/";
|
document.location.href = "/";
|
||||||
} else if (http.status === 403) {
|
} else if (http.status === 403) {
|
||||||
|
|||||||
@@ -39,7 +39,13 @@ export var tools = new function() {
|
|||||||
|
|
||||||
/************************************************************************/
|
/************************************************************************/
|
||||||
|
|
||||||
self.httpRequest = function(method, url, callback, body=null, content_type=null, timeout=15000) {
|
self.httpRequest = function(method, url, params, callback, body=null, content_type=null, timeout=15000) {
|
||||||
|
if (params) {
|
||||||
|
params = new URLSearchParams(params);
|
||||||
|
if (params) {
|
||||||
|
url += "?" + params;
|
||||||
|
}
|
||||||
|
}
|
||||||
let http = new XMLHttpRequest();
|
let http = new XMLHttpRequest();
|
||||||
http.open(method, url, true);
|
http.open(method, url, true);
|
||||||
if (content_type) {
|
if (content_type) {
|
||||||
@@ -54,12 +60,12 @@ export var tools = new function() {
|
|||||||
http.send(body);
|
http.send(body);
|
||||||
};
|
};
|
||||||
|
|
||||||
self.httpGet = function(url, callback, body=null, content_type=null, timeout=15000) {
|
self.httpGet = function(url, params, callback, body=null, content_type=null, timeout=15000) {
|
||||||
self.httpRequest("GET", url, callback, body, content_type, timeout);
|
self.httpRequest("GET", url, params, callback, body, content_type, timeout);
|
||||||
};
|
};
|
||||||
|
|
||||||
self.httpPost = function(url, callback, body=null, content_type=null, timeout=15000) {
|
self.httpPost = function(url, params, callback, body=null, content_type=null, timeout=15000) {
|
||||||
self.httpRequest("POST", url, callback, body, content_type, timeout);
|
self.httpRequest("POST", url, params, callback, body, content_type, timeout);
|
||||||
};
|
};
|
||||||
|
|
||||||
/************************************************************************/
|
/************************************************************************/
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ export function main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function __loadKvmdInfo() {
|
function __loadKvmdInfo() {
|
||||||
tools.httpGet("/api/info", function(http) {
|
tools.httpGet("/api/info", null, function(http) {
|
||||||
if (http.status === 200) {
|
if (http.status === 200) {
|
||||||
let vnc_port = JSON.parse(http.responseText).result.extras.vnc.port;
|
let vnc_port = JSON.parse(http.responseText).result.extras.vnc.port;
|
||||||
$("vnc-text").innerHTML = `
|
$("vnc-text").innerHTML = `
|
||||||
|
|||||||
Reference in New Issue
Block a user