2016-11-28 10:42:58 +00:00
|
|
|
/**
|
|
|
|
* @author n1474335 [n1474335@gmail.com]
|
|
|
|
* @copyright Crown Copyright 2016
|
|
|
|
* @license Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2019-08-22 11:53:41 +01:00
|
|
|
* Waiter to handle events related to the CyberChef options.
|
2016-11-28 10:42:58 +00:00
|
|
|
*/
|
2019-08-22 11:53:41 +01:00
|
|
|
class OptionsWaiter {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* OptionsWaiter constructor.
|
|
|
|
*
|
|
|
|
* @param {App} app - The main view object for CyberChef.
|
|
|
|
* @param {Manager} manager - The CyberChef event manager.
|
|
|
|
*/
|
|
|
|
constructor(app, manager) {
|
|
|
|
this.app = app;
|
|
|
|
this.manager = manager;
|
2016-11-28 10:42:58 +00:00
|
|
|
}
|
|
|
|
|
2019-08-22 11:53:41 +01:00
|
|
|
/**
|
|
|
|
* Loads options and sets values of switches and inputs to match them.
|
|
|
|
*
|
|
|
|
* @param {Object} options
|
|
|
|
*/
|
|
|
|
load(options) {
|
2023-01-19 17:14:24 +00:00
|
|
|
Object.assign(this.app.options, options);
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2019-08-22 11:53:41 +01:00
|
|
|
// Set options to match object
|
2023-01-19 17:14:24 +00:00
|
|
|
document.querySelectorAll("#options-body input[type=checkbox]").forEach(cbox => {
|
|
|
|
cbox.checked = this.app.options[cbox.getAttribute("option")];
|
|
|
|
});
|
|
|
|
|
|
|
|
document.querySelectorAll("#options-body input[type=number]").forEach(nbox => {
|
|
|
|
nbox.value = this.app.options[nbox.getAttribute("option")];
|
|
|
|
nbox.dispatchEvent(new CustomEvent("change", {bubbles: true}));
|
|
|
|
});
|
|
|
|
|
|
|
|
document.querySelectorAll("#options-body select").forEach(select => {
|
|
|
|
const val = this.app.options[select.getAttribute("option")];
|
2019-08-22 11:53:41 +01:00
|
|
|
if (val) {
|
2023-01-19 17:14:24 +00:00
|
|
|
select.value = val;
|
|
|
|
select.dispatchEvent(new CustomEvent("change", {bubbles: true}));
|
2019-08-22 11:53:41 +01:00
|
|
|
} else {
|
2023-01-19 17:14:24 +00:00
|
|
|
select.selectedIndex = 0;
|
2019-08-22 11:53:41 +01:00
|
|
|
}
|
2023-01-19 17:14:24 +00:00
|
|
|
});
|
2022-06-29 18:02:49 +01:00
|
|
|
|
|
|
|
// Initialise options
|
|
|
|
this.setWordWrap();
|
2024-05-13 17:48:09 +01:00
|
|
|
this.manager.ops.setCatCount();
|
2019-08-22 11:53:41 +01:00
|
|
|
}
|
2016-11-28 10:42:58 +00:00
|
|
|
|
|
|
|
|
2019-08-22 11:53:41 +01:00
|
|
|
/**
|
|
|
|
* Handler for options click events.
|
2019-10-16 15:38:20 +01:00
|
|
|
* Displays the options pane.
|
2019-08-22 11:53:41 +01:00
|
|
|
*
|
|
|
|
* @param {event} e
|
|
|
|
*/
|
|
|
|
optionsClick(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
$("#options-modal").modal();
|
|
|
|
}
|
2016-11-28 10:42:58 +00:00
|
|
|
|
|
|
|
|
2019-08-22 11:53:41 +01:00
|
|
|
/**
|
|
|
|
* Handler for reset options click events.
|
|
|
|
* Resets options back to their default values.
|
|
|
|
*/
|
|
|
|
resetOptionsClick() {
|
|
|
|
this.load(this.app.doptions);
|
|
|
|
}
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2017-09-20 22:26:47 +01:00
|
|
|
|
2019-08-22 11:53:41 +01:00
|
|
|
/**
|
|
|
|
* Handler for switch change events.
|
|
|
|
*
|
|
|
|
* @param {event} e
|
|
|
|
*/
|
|
|
|
switchChange(e) {
|
|
|
|
const el = e.target;
|
|
|
|
const option = el.getAttribute("option");
|
|
|
|
const state = el.checked;
|
2016-11-28 10:42:58 +00:00
|
|
|
|
2019-08-22 11:53:41 +01:00
|
|
|
this.updateOption(option, state);
|
|
|
|
}
|
2016-11-28 10:42:58 +00:00
|
|
|
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2019-08-22 11:53:41 +01:00
|
|
|
/**
|
|
|
|
* Handler for number change events.
|
|
|
|
*
|
|
|
|
* @param {event} e
|
|
|
|
*/
|
|
|
|
numberChange(e) {
|
|
|
|
const el = e.target;
|
|
|
|
const option = el.getAttribute("option");
|
|
|
|
const val = parseInt(el.value, 10);
|
2017-09-20 22:26:47 +01:00
|
|
|
|
2019-08-22 11:53:41 +01:00
|
|
|
this.updateOption(option, val);
|
|
|
|
}
|
2016-11-28 10:42:58 +00:00
|
|
|
|
|
|
|
|
2019-08-22 11:53:41 +01:00
|
|
|
/**
|
|
|
|
* Handler for select change events.
|
|
|
|
*
|
|
|
|
* @param {event} e
|
|
|
|
*/
|
|
|
|
selectChange(e) {
|
|
|
|
const el = e.target;
|
|
|
|
const option = el.getAttribute("option");
|
2017-02-09 15:09:33 +00:00
|
|
|
|
2019-08-22 11:53:41 +01:00
|
|
|
this.updateOption(option, el.value);
|
|
|
|
}
|
2017-09-20 22:26:47 +01:00
|
|
|
|
2019-08-22 11:53:41 +01:00
|
|
|
/**
|
|
|
|
* Modifies an option value and saves it to local storage.
|
|
|
|
*
|
|
|
|
* @param {string} option - The option to be updated
|
|
|
|
* @param {string|number|boolean} value - The new value of the option
|
|
|
|
*/
|
|
|
|
updateOption(option, value) {
|
|
|
|
log.debug(`Setting ${option} to ${value}`);
|
|
|
|
this.app.options[option] = value;
|
|
|
|
|
|
|
|
if (this.app.isLocalStorageAvailable())
|
|
|
|
localStorage.setItem("options", JSON.stringify(this.app.options));
|
|
|
|
}
|
2016-11-28 10:42:58 +00:00
|
|
|
|
|
|
|
|
2019-08-22 11:53:41 +01:00
|
|
|
/**
|
|
|
|
* Sets or unsets word wrap on the input and output depending on the wordWrap option value.
|
|
|
|
*/
|
|
|
|
setWordWrap() {
|
2022-06-29 18:02:49 +01:00
|
|
|
this.manager.input.setWordWrap(this.app.options.wordWrap);
|
2022-07-02 19:23:03 +01:00
|
|
|
this.manager.output.setWordWrap(this.app.options.wordWrap);
|
2016-11-28 10:42:58 +00:00
|
|
|
}
|
2017-03-23 17:52:20 +00:00
|
|
|
|
2017-04-25 00:21:38 +01:00
|
|
|
|
2019-08-22 11:53:41 +01:00
|
|
|
/**
|
2019-10-27 15:17:06 +00:00
|
|
|
* Theme change event listener
|
2019-08-22 11:53:41 +01:00
|
|
|
*
|
|
|
|
* @param {Event} e
|
|
|
|
*/
|
|
|
|
themeChange(e) {
|
|
|
|
const themeClass = e.target.value;
|
2019-10-27 15:17:06 +00:00
|
|
|
this.changeTheme(themeClass);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Changes the theme by setting the class of the <html> element.
|
|
|
|
*
|
|
|
|
* @param (string} theme
|
|
|
|
*/
|
|
|
|
changeTheme(theme) {
|
|
|
|
document.querySelector(":root").className = theme;
|
|
|
|
|
|
|
|
// Update theme selection
|
|
|
|
const themeSelect = document.getElementById("theme");
|
|
|
|
themeSelect.selectedIndex = themeSelect.querySelector(`option[value="${theme}"`).index;
|
2019-08-22 11:53:41 +01:00
|
|
|
}
|
2017-04-25 00:21:38 +01:00
|
|
|
|
2017-12-28 18:17:38 +00:00
|
|
|
|
2019-08-22 11:53:41 +01:00
|
|
|
/**
|
|
|
|
* Changes the console logging level.
|
|
|
|
*
|
|
|
|
* @param {Event} e
|
|
|
|
*/
|
|
|
|
logLevelChange(e) {
|
|
|
|
const level = e.target.value;
|
|
|
|
log.setLevel(level, false);
|
|
|
|
this.manager.worker.setLogLevel();
|
|
|
|
this.manager.input.setLogLevel();
|
2023-01-18 18:07:06 +00:00
|
|
|
this.manager.output.setLogLevel();
|
|
|
|
this.manager.background.setLogLevel();
|
2019-08-22 11:53:41 +01:00
|
|
|
}
|
|
|
|
}
|
2017-12-28 18:17:38 +00:00
|
|
|
|
2017-03-23 17:52:20 +00:00
|
|
|
export default OptionsWaiter;
|