/*
This software is licensed under the MIT License. See the license file for details.
Source: https://github.com/spacehuhntech/WiFiDuck
*/
// ========== Global Variables ========== //
// ! List of files returned by "ls" command
var file_list = "";
// ! Variable to save interval for updating status continously
var status_interval = undefined;
// ! Unsaved content in the editor
var unsaved_changed = false;
// ! Flag if editor has loaded a file yet
var file_opened = false;
// ========== Global Functions ========== //
// ===== Value Getters ===== //
function get_new_filename() {
return E("newFile").value;
}
function get_editor_filename() {
return E("editorFile").value;
}
function set_editor_filename(filename) {
return E("editorFile").value = filename;
}
function get_editor_content() {
var content = E("editor").value;
if (!content.endsWith("\n"))
content = content + "\n";
return content;
}
// ! Update status until it's no longer "running"
function check_status() {
if (current_status.includes("running") || current_status.includes("saving"))
ws_update_status();
else
stop_status_interval();
}
// ! Start interval that checks and updates the status continously
function start_status_interval() {
if (status_interval) return; // !< Only continue if status_interval not set
ws_update_status(); // !< Get current status
status_interval = setInterval(check_status, 500); // !< Start interval
}
// ! Stop interval that checks and updates the status continously
function stop_status_interval() {
if (!status_interval) return; // !< Only continue if status_interval was set
// ! Stop interval and unset variable
clearInterval(status_interval);
status_interval = undefined;
}
// ! Append string to script content
function append(str) {
E("editor").value += str;
}
// ! Updates file list and memory usage
function update_file_list() {
ws_send("mem", function(msg) {
var lines = msg.split(/\n/);
if(lines.length == 1) {
console.error("Malformed response:");
console.error(msg);
return;
}
var byte = lines[0].split(" ")[0];
var used = lines[1].split(" ")[0];
var free = lines[2].split(" ")[0];
var percent = Math.floor(byte / 100);
var freepercent = Math.floor(free / percent);
E("freeMemory").innerHTML = used + " byte used (" + freepercent + "% free)";
file_list = "";
ws_send("ls", function(csv) {
file_list += csv;
var lines = file_list.split(/\n/);
var tableHTML = "\n";
tableHTML += "
\n";
tableHTML += "
File
\n";
tableHTML += "
Byte
\n";
tableHTML += "
Actions
\n";
tableHTML += "
\n";
tableHTML += "\n";
tableHTML += "
\n";
for (var i = 0; i < lines.length; i++) {
var data = lines[i].split(" ");
var fileName = data[0];
var fileSize = data[1];
if (fileName.length > 0) {
if (i == 0 && !file_opened) {
read(fileName);
}
tableHTML += "