MojoFrontend/scripts/console.js

81 lines
2.4 KiB
JavaScript
Raw Normal View History

2022-05-09 04:20:23 +00:00
var DEBUG = true;
2022-05-04 23:38:03 +00:00
async function sendCommand(payload, method="invoke", background=false, persistent="auto") {
2022-05-03 09:40:07 +00:00
let key = new window.URLSearchParams(window.location.search).get("k");
let url = '/mojoplus/api';
2022-05-04 20:12:44 +00:00
let data = JSON.stringify({ "k": key, "request": method, "payload": payload });
2022-05-09 04:20:23 +00:00
if (DEBUG) console.log(payload);
2022-05-03 11:19:25 +00:00
let response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: data,
})
2022-05-05 01:16:56 +00:00
try {
let json = await response.json();
try{
if (method == 'invoke' && !background) {
if (json.code != 200) throw Error('');
var m = json.payload.trim().replace(/\r/g, "").replace(/\n\n/g, "\n");
if(persistent == "auto") {
var m2 = m.replace(/\n/g, "");
persistent = (m.length - m2.length) > 3; // true more than 3 lines
}
message(`${m}`, null, persistent);
}
return json
} catch (e) {
var messages = `Request failed.`
if (json.code) {
messages += ` Code: ${json.message}`
}
message(messages, "fail", false);
2022-05-04 23:38:03 +00:00
}
2022-05-05 01:16:56 +00:00
} catch (err) {
message("Connection issue.", "fail", false);
2022-05-04 20:12:44 +00:00
}
2022-05-03 10:12:30 +00:00
}
2022-05-04 20:12:44 +00:00
function dismissMessage(){
document.getElementById("message").style.opacity = 1;
setTimeout(() => {
document.getElementById("message").classList.add("hide");
}, 500);
document.getElementById("message").style.opacity = 0;
}
2022-05-03 10:12:30 +00:00
function switchPage(page) {
const iframe = document.getElementById("content");
iframe.src = `pages/${page}.html`;
2022-05-04 20:12:44 +00:00
}
2022-05-04 23:38:03 +00:00
function message(message, className, persistent=false) {
var m = document.createElement("div");
m.classList.add("messageBox");
m.classList.add("initial");
if (className) m.classList.add(className);
var mc = document.createElement("div");
var dismissButton = document.createElement("button");
mc.classList.add("messageContent");
mc.innerText = message;
dismissButton.innerText = "Dissmiss";
m.appendChild(mc);
m.appendChild(dismissButton);
document.getElementById("message").appendChild(m);
dismissButton.onclick = (e) => {
dismissMessageInternal(e.target.parentNode);
}
// m.classList.remove("initial");
setTimeout(function() {
m.classList.remove("initial");
}, 100);
if (!persistent) setTimeout(() => dismissMessageInternal(m), 4000);
}
function dismissMessageInternal(target) {
if (target){
target.classList.add("finish");
setTimeout(()=>{target.remove();}, 400);
}
}