2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* Waiter to handle events related to the CyberChef options.
|
|
|
|
*
|
|
|
|
* @author n1474335 [n1474335@gmail.com]
|
|
|
|
* @copyright Crown Copyright 2016
|
|
|
|
* @license Apache-2.0
|
|
|
|
*
|
|
|
|
* @constructor
|
2017-03-23 17:52:20 +00:00
|
|
|
* @param {App} app - The main view object for CyberChef.
|
2016-11-28 10:42:58 +00:00
|
|
|
*/
|
2017-12-28 18:17:38 +00:00
|
|
|
const OptionsWaiter = function(app, manager) {
|
2016-11-28 10:42:58 +00:00
|
|
|
this.app = app;
|
2017-12-28 18:17:38 +00:00
|
|
|
this.manager = manager;
|
2016-11-28 10:42:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads options and sets values of switches and inputs to match them.
|
|
|
|
*
|
|
|
|
* @param {Object} options
|
|
|
|
*/
|
|
|
|
OptionsWaiter.prototype.load = function(options) {
|
2017-04-13 18:08:50 +01:00
|
|
|
for (const option in options) {
|
2016-11-28 10:42:58 +00:00
|
|
|
this.app.options[option] = options[option];
|
|
|
|
}
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2016-11-28 10:42:58 +00:00
|
|
|
// Set options to match object
|
2017-04-13 18:08:50 +01:00
|
|
|
const cboxes = document.querySelectorAll("#options-body input[type=checkbox]");
|
2017-04-13 18:31:26 +01:00
|
|
|
let i;
|
|
|
|
for (i = 0; i < cboxes.length; i++) {
|
2018-06-17 12:44:12 +01:00
|
|
|
cboxes[i].checked = this.app.options[cboxes[i].getAttribute("option")];
|
2016-11-28 10:42:58 +00:00
|
|
|
}
|
|
|
|
|
2017-04-13 18:08:50 +01:00
|
|
|
const nboxes = document.querySelectorAll("#options-body input[type=number]");
|
2016-11-28 10:42:58 +00:00
|
|
|
for (i = 0; i < nboxes.length; i++) {
|
|
|
|
nboxes[i].value = this.app.options[nboxes[i].getAttribute("option")];
|
|
|
|
nboxes[i].dispatchEvent(new CustomEvent("change", {bubbles: true}));
|
|
|
|
}
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2017-04-13 18:08:50 +01:00
|
|
|
const selects = document.querySelectorAll("#options-body select");
|
2016-11-28 10:42:58 +00:00
|
|
|
for (i = 0; i < selects.length; i++) {
|
2017-05-19 00:10:36 +01:00
|
|
|
const val = this.app.options[selects[i].getAttribute("option")];
|
|
|
|
if (val) {
|
|
|
|
selects[i].value = val;
|
|
|
|
selects[i].dispatchEvent(new CustomEvent("change", {bubbles: true}));
|
2017-05-24 10:29:47 +01:00
|
|
|
} else {
|
|
|
|
selects[i].selectedIndex = 0;
|
2017-05-19 00:10:36 +01:00
|
|
|
}
|
2016-11-28 10:42:58 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler for options click events.
|
|
|
|
* Dispays the options pane.
|
2017-06-16 15:36:42 +00:00
|
|
|
*
|
|
|
|
* @param {event} e
|
2016-11-28 10:42:58 +00:00
|
|
|
*/
|
2017-06-16 15:36:42 +00:00
|
|
|
OptionsWaiter.prototype.optionsClick = function(e) {
|
|
|
|
e.preventDefault();
|
2016-11-28 10:42:58 +00:00
|
|
|
$("#options-modal").modal();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler for reset options click events.
|
|
|
|
* Resets options back to their default values.
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
OptionsWaiter.prototype.resetOptionsClick = function() {
|
2016-11-28 10:42:58 +00:00
|
|
|
this.load(this.app.doptions);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler for switch change events.
|
|
|
|
* Modifies the option state and saves it to local storage.
|
|
|
|
*
|
|
|
|
* @param {event} e
|
|
|
|
*/
|
2018-06-17 12:44:12 +01:00
|
|
|
OptionsWaiter.prototype.switchChange = function(e) {
|
2017-05-03 00:40:39 +01:00
|
|
|
const el = e.target;
|
|
|
|
const option = el.getAttribute("option");
|
2018-06-17 12:44:12 +01:00
|
|
|
const state = el.checked;
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2017-12-28 18:17:38 +00:00
|
|
|
log.debug(`Setting ${option} to ${state}`);
|
2016-11-28 10:42:58 +00:00
|
|
|
this.app.options[option] = state;
|
2017-09-20 22:26:47 +01:00
|
|
|
|
|
|
|
if (this.app.isLocalStorageAvailable())
|
|
|
|
localStorage.setItem("options", JSON.stringify(this.app.options));
|
2016-11-28 10:42:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler for number change events.
|
|
|
|
* Modifies the option value and saves it to local storage.
|
|
|
|
*
|
|
|
|
* @param {event} e
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
OptionsWaiter.prototype.numberChange = function(e) {
|
2017-05-03 00:40:39 +01:00
|
|
|
const el = e.target;
|
|
|
|
const option = el.getAttribute("option");
|
2017-12-28 18:17:38 +00:00
|
|
|
const val = parseInt(el.value, 10);
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2017-12-28 18:17:38 +00:00
|
|
|
log.debug(`Setting ${option} to ${val}`);
|
|
|
|
this.app.options[option] = val;
|
2017-09-20 22:26:47 +01:00
|
|
|
|
|
|
|
if (this.app.isLocalStorageAvailable())
|
|
|
|
localStorage.setItem("options", JSON.stringify(this.app.options));
|
2016-11-28 10:42:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler for select change events.
|
|
|
|
* Modifies the option value and saves it to local storage.
|
|
|
|
*
|
|
|
|
* @param {event} e
|
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
OptionsWaiter.prototype.selectChange = function(e) {
|
2017-05-03 00:40:39 +01:00
|
|
|
const el = e.target;
|
|
|
|
const option = el.getAttribute("option");
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2017-12-28 18:17:38 +00:00
|
|
|
log.debug(`Setting ${option} to ${el.value}`);
|
2016-11-28 10:42:58 +00:00
|
|
|
this.app.options[option] = el.value;
|
2017-09-20 22:26:47 +01:00
|
|
|
|
|
|
|
if (this.app.isLocalStorageAvailable())
|
|
|
|
localStorage.setItem("options", JSON.stringify(this.app.options));
|
2016-11-28 10:42:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2017-01-31 18:24:56 +00:00
|
|
|
* Sets or unsets word wrap on the input and output depending on the wordWrap option value.
|
2016-11-28 10:42:58 +00:00
|
|
|
*/
|
2017-01-31 18:24:56 +00:00
|
|
|
OptionsWaiter.prototype.setWordWrap = function() {
|
2016-11-28 10:42:58 +00:00
|
|
|
document.getElementById("input-text").classList.remove("word-wrap");
|
|
|
|
document.getElementById("output-text").classList.remove("word-wrap");
|
|
|
|
document.getElementById("output-html").classList.remove("word-wrap");
|
|
|
|
document.getElementById("input-highlighter").classList.remove("word-wrap");
|
|
|
|
document.getElementById("output-highlighter").classList.remove("word-wrap");
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2017-01-31 18:24:56 +00:00
|
|
|
if (!this.app.options.wordWrap) {
|
2016-11-28 10:42:58 +00:00
|
|
|
document.getElementById("input-text").classList.add("word-wrap");
|
|
|
|
document.getElementById("output-text").classList.add("word-wrap");
|
|
|
|
document.getElementById("output-html").classList.add("word-wrap");
|
|
|
|
document.getElementById("input-highlighter").classList.add("word-wrap");
|
|
|
|
document.getElementById("output-highlighter").classList.add("word-wrap");
|
|
|
|
}
|
|
|
|
};
|
2017-03-23 17:52:20 +00:00
|
|
|
|
2017-04-25 00:21:38 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Changes the theme by setting the class of the <html> element.
|
2018-01-02 14:46:35 +00:00
|
|
|
*
|
2017-12-28 18:17:38 +00:00
|
|
|
* @param {Event} e
|
2017-04-25 00:21:38 +01:00
|
|
|
*/
|
|
|
|
OptionsWaiter.prototype.themeChange = function (e) {
|
2017-05-07 01:13:47 +01:00
|
|
|
const themeClass = e.target.value;
|
2017-04-25 00:21:38 +01:00
|
|
|
|
|
|
|
document.querySelector(":root").className = themeClass;
|
|
|
|
};
|
|
|
|
|
2017-12-28 18:17:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Changes the console logging level.
|
2018-01-02 14:46:35 +00:00
|
|
|
*
|
2017-12-28 18:17:38 +00:00
|
|
|
* @param {Event} e
|
|
|
|
*/
|
|
|
|
OptionsWaiter.prototype.logLevelChange = function (e) {
|
|
|
|
const level = e.target.value;
|
|
|
|
log.setLevel(level, false);
|
|
|
|
this.manager.worker.setLogLevel();
|
2019-04-25 16:32:48 +01:00
|
|
|
this.manager.input.setLogLevel();
|
2017-12-28 18:17:38 +00:00
|
|
|
};
|
|
|
|
|
2017-03-23 17:52:20 +00:00
|
|
|
export default OptionsWaiter;
|