CyberChef/src/web/InputWaiter.js

301 lines
8.3 KiB
JavaScript
Raw Normal View History

import LoaderWorker from "worker-loader?inline&fallback=false!./LoaderWorker.js";
2016-11-28 11:42:58 +01:00
/**
* Waiter to handle events related to the input.
*
* @author n1474335 [n1474335@gmail.com]
* @copyright Crown Copyright 2016
* @license Apache-2.0
*
* @constructor
* @param {App} app - The main view object for CyberChef.
2016-11-28 11:42:58 +01:00
* @param {Manager} manager - The CyberChef event manager.
*/
2017-04-13 19:08:50 +02:00
const InputWaiter = function(app, manager) {
2016-11-28 11:42:58 +01:00
this.app = app;
this.manager = manager;
2017-02-09 16:09:33 +01:00
2016-11-28 11:42:58 +01:00
// Define keys that don't change the input so we don't have to autobake when they are pressed
this.badKeys = [
2016-11-28 11:42:58 +01:00
16, //Shift
17, //Ctrl
18, //Alt
19, //Pause
20, //Caps
27, //Esc
2016-12-14 17:39:17 +01:00
33, 34, 35, 36, //PgUp, PgDn, End, Home
37, 38, 39, 40, //Directional
2016-11-28 11:42:58 +01:00
44, //PrntScrn
2016-12-14 17:39:17 +01:00
91, 92, //Win
2016-11-28 11:42:58 +01:00
93, //Context
2016-12-14 17:39:17 +01:00
112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, //F1-12
2016-11-28 11:42:58 +01:00
144, //Num
145, //Scroll
];
this.loaderWorker = null;
this.fileBuffer = null;
2016-11-28 11:42:58 +01:00
};
/**
* Gets the user's input from the input textarea.
*
* @returns {string}
*/
InputWaiter.prototype.get = function() {
return this.fileBuffer || document.getElementById("input-text").value;
2016-11-28 11:42:58 +01:00
};
/**
* Sets the input in the input area.
2016-11-28 11:42:58 +01:00
*
* @param {string|File} input
2016-11-28 11:42:58 +01:00
*
* @fires Manager#statechange
*/
InputWaiter.prototype.set = function(input) {
const inputText = document.getElementById("input-text");
if (input instanceof File) {
this.setFile(input);
inputText.value = "";
this.setInputInfo(input.size, null);
} else {
inputText.value = input;
this.closeFile();
window.dispatchEvent(this.manager.statechange);
const lines = input.length < (this.app.options.ioDisplayThreshold * 1024) ?
input.count("\n") + 1 : null;
this.setInputInfo(input.length, lines);
}
2016-11-28 11:42:58 +01:00
};
/**
* Shows file details.
*
* @param {File} file
*/
InputWaiter.prototype.setFile = function(file) {
// Display file overlay in input area with details
const fileOverlay = document.getElementById("input-file"),
fileName = document.getElementById("input-file-name"),
fileSize = document.getElementById("input-file-size"),
fileType = document.getElementById("input-file-type"),
fileLoaded = document.getElementById("input-file-loaded");
this.fileBuffer = new ArrayBuffer();
fileOverlay.style.display = "block";
fileName.textContent = file.name;
fileSize.textContent = file.size.toLocaleString() + " bytes";
fileType.textContent = file.type || "unknown";
fileLoaded.textContent = "0%";
};
2016-11-28 11:42:58 +01:00
/**
* Displays information about the input.
*
* @param {number} length - The length of the current input string
* @param {number} lines - The number of the lines in the current input string
*/
InputWaiter.prototype.setInputInfo = function(length, lines) {
2017-04-13 19:08:50 +02:00
let width = length.toString().length;
2016-11-28 11:42:58 +01:00
width = width < 2 ? 2 : width;
2017-02-09 16:09:33 +01:00
const lengthStr = length.toString().padStart(width, " ").replace(/ /g, "&nbsp;");
let msg = "length: " + lengthStr;
2017-02-09 16:09:33 +01:00
if (typeof lines === "number") {
const linesStr = lines.toString().padStart(width, " ").replace(/ /g, "&nbsp;");
msg += "<br>lines: " + linesStr;
}
document.getElementById("input-info").innerHTML = msg;
2016-11-28 11:42:58 +01:00
};
/**
* Handler for input change events.
2016-11-28 11:42:58 +01:00
*
* @param {event} e
*
* @fires Manager#statechange
*/
InputWaiter.prototype.inputChange = function(e) {
// Ignore this function if the input is a File
if (this.fileBuffer) return;
2016-11-28 11:42:58 +01:00
// Remove highlighting from input and output panes as the offsets might be different now
this.manager.highlighter.removeHighlights();
2017-02-09 16:09:33 +01:00
2016-11-28 11:42:58 +01:00
// Reset recipe progress as any previous processing will be redundant now
this.app.progress = 0;
2017-02-09 16:09:33 +01:00
2016-11-28 11:42:58 +01:00
// Update the input metadata info
2017-05-03 01:40:39 +02:00
const inputText = this.get();
const lines = inputText.length < (this.app.options.ioDisplayThreshold * 1024) ?
inputText.count("\n") + 1 : null;
2017-02-09 16:09:33 +01:00
this.setInputInfo(inputText.length, lines);
2017-02-09 16:09:33 +01:00
if (e && this.badKeys.indexOf(e.keyCode) < 0) {
2016-11-28 11:42:58 +01:00
// Fire the statechange event as the input has been modified
window.dispatchEvent(this.manager.statechange);
}
};
/**
* Handler for input paste events.
* Checks that the size of the input is below the display limit, otherwise treats it as a file/blob.
*
* @param {event} e
*/
InputWaiter.prototype.inputPaste = function(e) {
const pastedData = e.clipboardData.getData("Text");
if (pastedData.length < (this.app.options.ioDisplayThreshold * 1024)) {
this.inputChange(e);
} else {
e.preventDefault();
e.stopPropagation();
const file = new File([pastedData], "PastedData", {
type: "text/plain",
lastModified: Date.now()
});
this.loaderWorker = new LoaderWorker();
this.loaderWorker.addEventListener("message", this.handleLoaderMessage.bind(this));
this.loaderWorker.postMessage({"file": file});
this.set(file);
return false;
}
};
2016-11-28 11:42:58 +01:00
/**
* Handler for input dragover events.
* Gives the user a visual cue to show that items can be dropped here.
*
* @param {event} e
*/
InputWaiter.prototype.inputDragover = function(e) {
2016-11-28 11:42:58 +01:00
// This will be set if we're dragging an operation
if (e.dataTransfer.effectAllowed === "move")
return false;
2017-02-09 16:09:33 +01:00
2016-11-28 11:42:58 +01:00
e.stopPropagation();
e.preventDefault();
e.target.closest("#input-text,#input-file").classList.add("dropping-file");
2016-11-28 11:42:58 +01:00
};
/**
* Handler for input dragleave events.
* Removes the visual cue.
*
* @param {event} e
*/
InputWaiter.prototype.inputDragleave = function(e) {
2016-11-28 11:42:58 +01:00
e.stopPropagation();
e.preventDefault();
document.getElementById("input-text").classList.remove("dropping-file");
document.getElementById("input-file").classList.remove("dropping-file");
2016-11-28 11:42:58 +01:00
};
/**
* Handler for input drop events.
* Loads the dragged data into the input textarea.
*
* @param {event} e
*/
InputWaiter.prototype.inputDrop = function(e) {
2016-11-28 11:42:58 +01:00
// This will be set if we're dragging an operation
if (e.dataTransfer.effectAllowed === "move")
return false;
2017-02-09 16:09:33 +01:00
2016-11-28 11:42:58 +01:00
e.stopPropagation();
e.preventDefault();
2017-02-09 16:09:33 +01:00
2017-05-03 01:40:39 +02:00
const file = e.dataTransfer.files[0];
const text = e.dataTransfer.getData("Text");
2017-02-09 16:09:33 +01:00
document.getElementById("input-text").classList.remove("dropping-file");
document.getElementById("input-file").classList.remove("dropping-file");
if (text) {
this.closeFile();
2016-11-28 11:42:58 +01:00
this.set(text);
return;
2016-11-28 11:42:58 +01:00
}
if (file) {
this.closeFile();
this.loaderWorker = new LoaderWorker();
this.loaderWorker.addEventListener("message", this.handleLoaderMessage.bind(this));
this.loaderWorker.postMessage({"file": file});
this.set(file);
}
};
/**
* Handler for messages sent back by the LoaderWorker.
*
* @param {MessageEvent} e
*/
InputWaiter.prototype.handleLoaderMessage = function(e) {
const r = e.data;
if (r.hasOwnProperty("progress")) {
const fileLoaded = document.getElementById("input-file-loaded");
fileLoaded.textContent = r.progress + "%";
}
if (r.hasOwnProperty("error")) {
this.app.alert(r.error, "danger", 10000);
}
if (r.hasOwnProperty("fileBuffer")) {
log.debug("Input file loaded");
this.fileBuffer = r.fileBuffer;
window.dispatchEvent(this.manager.statechange);
}
};
/**
* Handler for file close events.
*/
InputWaiter.prototype.closeFile = function() {
if (this.loaderWorker) this.loaderWorker.terminate();
this.fileBuffer = null;
document.getElementById("input-file").style.display = "none";
2016-11-28 11:42:58 +01:00
};
/**
* Handler for clear IO events.
* Resets the input, output and info areas.
*
* @fires Manager#statechange
*/
InputWaiter.prototype.clearIoClick = function() {
this.closeFile();
this.manager.output.closeFile();
this.manager.highlighter.removeHighlights();
2016-11-28 11:42:58 +01:00
document.getElementById("input-text").value = "";
document.getElementById("output-text").value = "";
document.getElementById("input-info").innerHTML = "";
document.getElementById("output-info").innerHTML = "";
document.getElementById("input-selection-info").innerHTML = "";
document.getElementById("output-selection-info").innerHTML = "";
window.dispatchEvent(this.manager.statechange);
};
export default InputWaiter;