CyberChef/src/web/waiters/OperationsWaiter.mjs

349 lines
11 KiB
JavaScript
Raw Normal View History

2018-05-15 17:36:45 +00:00
/**
* @author n1474335 [n1474335@gmail.com]
* @copyright Crown Copyright 2016
* @license Apache-2.0
*/
2021-02-12 13:51:51 +00:00
import {fuzzyMatch, calcMatchRanges} from "../../core/lib/FuzzyMatch.mjs";
import {COperationList} from "../components/c-operation-list.mjs";
2023-08-03 16:57:08 +12:00
import {COperationLi} from "../components/c-operation-li.mjs";
2018-05-15 17:36:45 +00:00
/**
* Waiter to handle events related to the operations.
*/
class OperationsWaiter {
/**
* OperationsWaiter 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;
this.options = {};
}
2018-05-15 17:36:45 +00:00
/**
* Handler for search events.
* Finds operations which match the given search term and displays them under the search box.
*
* @param {Event} e
2018-05-15 17:36:45 +00:00
*/
searchOperations(e) {
let ops, focused;
2023-05-10 21:53:29 +12:00
if (e.type === "keyup") {
const searchResults = document.getElementById("search-results");
this.openOpsDropdown();
2023-05-10 21:53:29 +12:00
if (e.target.value.length !== 0) {
this.app.setElementVisibility(searchResults, true);
}
}
2018-05-15 17:36:45 +00:00
if (e.key === "Enter") { // Search or Return ( enter )
2018-05-15 17:36:45 +00:00
e.preventDefault();
2023-08-03 16:57:08 +12:00
ops = document.querySelectorAll("#search-results c-operation-list c-operation-li li");
2018-05-15 17:36:45 +00:00
if (ops.length) {
focused = this.getFocusedOp(ops);
if (focused > -1) {
this.manager.recipe.addOperation(ops[focused].getAttribute("data-name"));
2018-05-15 17:36:45 +00:00
}
}
}
2023-05-10 21:53:29 +12:00
if (e.type === "click" && !e.target.value.length) {
this.openOpsDropdown();
2023-08-03 16:57:08 +12:00
} else if (e.key === "Escape") { // Escape
this.closeOpsDropdown();
2023-08-03 16:57:08 +12:00
} else if (e.key === "ArrowDown") { // Down
2018-05-15 17:36:45 +00:00
e.preventDefault();
2023-08-03 16:57:08 +12:00
ops = document.querySelectorAll("#search-results c-operation-list c-operation-li li");
2018-05-15 17:36:45 +00:00
if (ops.length) {
focused = this.getFocusedOp(ops);
if (focused > -1) {
ops[focused].classList.remove("focused-op");
2018-05-15 17:36:45 +00:00
}
if (focused === ops.length-1) focused = -1;
ops[focused+1].classList.add("focused-op");
2018-05-15 17:36:45 +00:00
}
2023-08-03 16:57:08 +12:00
} else if (e.key === "ArrowUp") { // Up
2018-05-15 17:36:45 +00:00
e.preventDefault();
2023-08-03 16:57:08 +12:00
ops = document.querySelectorAll("#search-results c-operation-list c-operation-li li");
2018-05-15 17:36:45 +00:00
if (ops.length) {
focused = this.getFocusedOp(ops);
if (focused > -1) {
ops[focused].classList.remove("focused-op");
2018-05-15 17:36:45 +00:00
}
if (focused === 0) focused = ops.length;
ops[focused-1].classList.add("focused-op");
2018-05-15 17:36:45 +00:00
}
} else {
const searchResultsEl = document.getElementById("search-results");
const el = e.target;
const str = el.value;
while (searchResultsEl.firstChild) {
try {
$(searchResultsEl.firstChild).popover("dispose");
2018-05-15 17:36:45 +00:00
} catch (err) {}
searchResultsEl.removeChild(searchResultsEl.firstChild);
}
$("#categories .show").collapse("hide");
2023-08-03 16:57:08 +12:00
2018-05-15 17:36:45 +00:00
if (str) {
const matchedOps = this.filterOperations(str, true);
2023-08-03 16:57:08 +12:00
const cOpList = new COperationList(
this.app,
matchedOps,
2023-08-03 16:57:08 +12:00
true,
false,
true,
{
class: "check-icon",
innerText: "check"
}
);
cOpList.build();
searchResultsEl.append(cOpList);
2018-05-15 17:36:45 +00:00
}
this.manager.ops.updateListItemsClasses("#rec-list", "selected");
2018-05-15 17:36:45 +00:00
}
}
/**
* Filters operations based on the search string and returns the matching ones.
*
* @param {string} searchStr
* @param {boolean} highlight - Whether to highlight the matching string in the operation
2018-05-15 17:36:45 +00:00
* name and description
* @returns {[[string, number[]]]}
2018-05-15 17:36:45 +00:00
*/
filterOperations(searchStr, highlight) {
2018-05-15 17:36:45 +00:00
const matchedOps = [];
const matchedDescs = [];
2022-05-30 18:06:15 +01:00
// Create version with no whitespace for the fuzzy match
// Helps avoid missing matches e.g. query "TCP " would not find "Parse TCP"
const inStrNWS = searchStr.replace(/\s/g, "");
2022-05-30 18:06:15 +01:00
2018-05-15 17:36:45 +00:00
for (const opName in this.app.operations) {
const op = this.app.operations[opName];
2021-02-05 17:54:57 +00:00
// Match op name using fuzzy match
2022-05-30 18:06:15 +01:00
const [nameMatch, score, idxs] = fuzzyMatch(inStrNWS, opName);
2021-02-05 17:54:57 +00:00
// Match description based on exact match
const descPos = op.description.toLowerCase().indexOf(searchStr.toLowerCase());
2018-05-15 17:36:45 +00:00
2021-02-05 17:54:57 +00:00
if (nameMatch || descPos >= 0) {
if (nameMatch) {
matchedOps.push([[opName, calcMatchRanges(idxs)], score]);
2018-05-15 17:36:45 +00:00
} else {
matchedDescs.push([opName]);
2018-05-15 17:36:45 +00:00
}
}
}
2021-02-05 17:54:57 +00:00
// Sort matched operations based on fuzzy score
matchedOps.sort((a, b) => b[1] - a[1]);
return matchedOps.map(a => a[0]).concat(matchedDescs);
2018-05-15 17:36:45 +00:00
}
/**
* Finds the operation which has been focused on using keyboard shortcuts. This will have the class
* 'focused-op' set. Returns the index of the operation within the given list.
2018-05-15 17:36:45 +00:00
*
* @param {element[]} ops
* @returns {number}
*/
getFocusedOp(ops) {
2018-05-15 17:36:45 +00:00
for (let i = 0; i < ops.length; i++) {
if (ops[i].classList.contains("focused-op")) {
2018-05-15 17:36:45 +00:00
return i;
}
}
return -1;
}
/**
* Handler for edit favourites click events.
* Sets up the 'Edit favourites' pane and displays it.
2023-07-24 15:01:38 +12:00
*
* @param {Event} e
2018-05-15 17:36:45 +00:00
*/
2023-07-24 15:01:38 +12:00
editFavouritesClick(e) {
const div = document.getElementById("editable-favourites");
// Remove c-operation-list if there already was one
2023-07-24 15:01:38 +12:00
if (div.querySelector("c-operation-list")) {
div.removeChild(div.querySelector("c-operation-list"));
}
// Get list of Favourite operation names
const favCatConfig = this.app.categories.find(catConfig => catConfig.name === "Favourites");
if(favCatConfig !== undefined) {
const opList = new COperationList(
this.app,
favCatConfig.ops.map( op => [op]),
false,
true,
false,
{
class: "remove-icon",
innerText: "delete"
2023-07-24 15:01:38 +12:00
},
)
2018-05-15 17:36:45 +00:00
opList.build();
div.appendChild(opList);
}
2023-07-24 15:01:38 +12:00
if (!this.app.isMobileView()) {
$("#editable-favourites [data-toggle=popover]").popover();
}
2018-05-15 17:36:45 +00:00
$("#favourites-modal").modal();
}
/**
* Open operations dropdown
*/
openOpsDropdown() {
2023-05-09 22:14:36 +12:00
// the 'close' ( dropdown ) icon in Operations component mobile UI
const closeOpsDropdownIcon = document.getElementById("close-ops-dropdown-icon");
const categories = document.getElementById("categories");
this.app.setElementVisibility(categories, true);
this.app.setElementVisibility(closeOpsDropdownIcon, true);
}
/**
* Hide any operation lists ( #categories or #search-results ) and the close-operations-dropdown
* icon itself, clear any search input
*/
closeOpsDropdown() {
const search = document.getElementById("search");
// if any input remains in #search, clear it
if (search.value.length) {
2023-05-10 21:53:29 +12:00
search.value = "";
}
this.app.setElementVisibility(document.getElementById("categories"), false);
this.app.setElementVisibility(document.getElementById("search-results"), false);
this.app.setElementVisibility(document.getElementById("close-ops-dropdown-icon"), false);
}
2018-05-15 17:36:45 +00:00
/**
* Handler for save favourites click events.
* Saves the selected favourites and reloads them.
*/
saveFavouritesClick() {
const listItems = document.querySelectorAll("#editable-favourites c-operation-li > li");
const favourites = Array.from(listItems, li => li.getAttribute("data-name"));
2018-05-15 17:36:45 +00:00
2023-07-24 15:01:38 +12:00
this.app.updateFavourites(favourites, true);
2018-05-15 17:36:45 +00:00
}
/**
* Handler for reset favourites click events.
* Resets favourites to their defaults.
*/
resetFavouritesClick() {
this.app.resetFavourites();
2023-05-09 22:14:36 +12:00
}
/**
* Update classes in the #dropdown-operations op-lists based on the
* list items of a srcListSelector.
*
2023-07-25 13:25:55 +12:00
* e.g: update all list items to add or remove the 'selected' class based on the operations
* in the current recipe-list, or 'favourite' classes on the current list of favourites.
*
* @param {string} srcListSelector - the UL element list to compare to
* @param {string} className - the className to update
*/
updateListItemsClasses(srcListSelector, className) {
2023-07-24 12:14:59 +12:00
const listItems = document.querySelectorAll(`${srcListSelector} li`);
2023-07-25 13:25:55 +12:00
const ops = document.querySelectorAll("c-operation-li > li.operation");
this.removeClassFromOps(className);
if (listItems.length !== 0) {
listItems.forEach((item => {
const targetDataName = item.getAttribute("data-name");
ops.forEach((op) => {
if (targetDataName === op.getAttribute("data-name")) {
this.addClassToOp(targetDataName, className);
}
});
}));
}
}
/**
* Generic function to remove a class from > ALL < operation list items
*
* @param {string} className - the class to remove
*/
removeClassFromOps(className) {
2023-07-25 13:25:55 +12:00
const ops = document.querySelectorAll("c-operation-li > li.operation");
ops.forEach((op => {
this.removeClassFromOp(op.getAttribute("data-name"), className);
}));
}
/**
2023-07-25 13:25:55 +12:00
* Generic function to remove a class from target operation list item. This operation li may occur twice ( when the op is in
* 'favourites' and in the category for instance )
*
* @param {string} opName - operation name through data-name attribute of the target operation
* @param {string} className - the class to remove
*/
removeClassFromOp(opName, className) {
2023-07-25 13:25:55 +12:00
const ops = document.querySelectorAll(`c-operation-li > li.operation[data-name="${opName}"].${className}`);
ops.forEach((op) => {
op.classList.remove(`${className}`);
});
}
/**
2023-07-25 13:25:55 +12:00
* Generic function to add a class to a specific operation. This operation li may occur twice ( when the op is in
* 'favourites' and in the category for instance )
*
* @param {string} opName - operation name through data-name attribute of the target operation
* @param {string} className - the class to add to the operation list item
*/
addClassToOp(opName, className) {
2023-07-25 13:25:55 +12:00
const ops = document.querySelectorAll(`c-operation-li > li.operation[data-name="${opName}"]`);
ops.forEach((op => {
op.classList.add(`${className}`);
}));
2018-05-15 17:36:45 +00:00
}
}
export default OperationsWaiter;