CyberChef/src/web/OperationsWaiter.js

314 lines
9.2 KiB
JavaScript
Raw Normal View History

import HTMLOperation from "./HTMLOperation.js";
import Sortable from "sortablejs";
2016-11-28 11:42:58 +01:00
/**
* Waiter to handle events related to the operations.
*
* @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 OperationsWaiter = 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
this.options = {};
this.removeIntent = false;
2016-11-28 11:42:58 +01:00
};
/**
* Handler for search events.
* Finds operations which match the given search term and displays them under the search box.
*
* @param {event} e
*/
OperationsWaiter.prototype.searchOperations = function(e) {
2017-04-13 19:08:50 +02:00
let ops, selected;
2017-02-09 16:09:33 +01:00
2016-12-14 17:39:17 +01:00
if (e.type === "search") { // Search
2016-11-28 11:42:58 +01:00
e.preventDefault();
ops = document.querySelectorAll("#search-results li");
if (ops.length) {
selected = this.getSelectedOp(ops);
2016-11-28 11:42:58 +01:00
if (selected > -1) {
this.manager.recipe.addOperation(ops[selected].innerHTML);
2016-11-28 11:42:58 +01:00
}
}
}
2017-02-09 16:09:33 +01:00
2016-12-14 17:39:17 +01:00
if (e.keyCode === 13) { // Return
2016-11-28 11:42:58 +01:00
e.preventDefault();
2016-12-14 17:39:17 +01:00
} else if (e.keyCode === 40) { // Down
2016-11-28 11:42:58 +01:00
e.preventDefault();
ops = document.querySelectorAll("#search-results li");
if (ops.length) {
selected = this.getSelectedOp(ops);
2016-11-28 11:42:58 +01:00
if (selected > -1) {
ops[selected].classList.remove("selected-op");
}
2016-12-14 17:39:17 +01:00
if (selected === ops.length-1) selected = -1;
2016-11-28 11:42:58 +01:00
ops[selected+1].classList.add("selected-op");
}
2016-12-14 17:39:17 +01:00
} else if (e.keyCode === 38) { // Up
2016-11-28 11:42:58 +01:00
e.preventDefault();
ops = document.querySelectorAll("#search-results li");
if (ops.length) {
selected = this.getSelectedOp(ops);
2016-11-28 11:42:58 +01:00
if (selected > -1) {
ops[selected].classList.remove("selected-op");
}
if (selected === 0) selected = ops.length;
ops[selected-1].classList.add("selected-op");
}
} else {
2017-05-03 01:40:39 +02:00
const searchResultsEl = document.getElementById("search-results");
const el = e.target;
const str = el.value;
2017-02-09 16:09:33 +01:00
while (searchResultsEl.firstChild) {
try {
$(searchResultsEl.firstChild).popover("destroy");
} catch (err) {}
searchResultsEl.removeChild(searchResultsEl.firstChild);
2016-11-28 11:42:58 +01:00
}
2017-02-09 16:09:33 +01:00
2016-11-28 11:42:58 +01:00
$("#categories .in").collapse("hide");
if (str) {
2017-05-03 01:40:39 +02:00
const matchedOps = this.filterOperations(str, true);
const matchedOpsHtml = matchedOps
.map(v => v.toStubHtml())
.join("");
2017-02-09 16:09:33 +01:00
searchResultsEl.innerHTML = matchedOpsHtml;
searchResultsEl.dispatchEvent(this.manager.oplistcreate);
2016-11-28 11:42:58 +01:00
}
}
};
/**
* Filters operations based on the search string and returns the matching ones.
*
* @param {string} searchStr
2016-11-28 11:42:58 +01:00
* @param {boolean} highlight - Whether or not to highlight the matching string in the operation
* name and description
* @returns {string[]}
*/
2017-05-03 01:40:39 +02:00
OperationsWaiter.prototype.filterOperations = function(inStr, highlight) {
const matchedOps = [];
const matchedDescs = [];
2017-02-09 16:09:33 +01:00
2017-05-03 01:40:39 +02:00
const searchStr = inStr.toLowerCase();
2017-02-09 16:09:33 +01:00
2017-04-13 19:08:50 +02:00
for (const opName in this.app.operations) {
2017-05-03 01:40:39 +02:00
const op = this.app.operations[opName];
const namePos = opName.toLowerCase().indexOf(searchStr);
const descPos = op.description.toLowerCase().indexOf(searchStr);
2017-02-09 16:09:33 +01:00
if (namePos >= 0 || descPos >= 0) {
2017-04-13 19:08:50 +02:00
const operation = new HTMLOperation(opName, this.app.operations[opName], this.app, this.manager);
2016-11-28 11:42:58 +01:00
if (highlight) {
operation.highlightSearchString(searchStr, namePos, descPos);
2016-11-28 11:42:58 +01:00
}
2017-02-09 16:09:33 +01:00
if (namePos < 0) {
matchedOps.push(operation);
2016-11-28 11:42:58 +01:00
} else {
matchedDescs.push(operation);
2016-11-28 11:42:58 +01:00
}
}
}
2017-02-09 16:09:33 +01:00
return matchedDescs.concat(matchedOps);
2016-11-28 11:42:58 +01:00
};
/**
* Finds the operation which has been selected using keyboard shortcuts. This will have the class
* 'selected-op' set. Returns the index of the operation within the given list.
*
* @param {element[]} ops
* @returns {number}
*/
OperationsWaiter.prototype.getSelectedOp = function(ops) {
2017-04-13 19:08:50 +02:00
for (let i = 0; i < ops.length; i++) {
2016-11-28 11:42:58 +01:00
if (ops[i].classList.contains("selected-op")) {
return i;
}
}
return -1;
};
/**
* Handler for oplistcreate events.
*
* @listens Manager#oplistcreate
* @param {event} e
*/
OperationsWaiter.prototype.opListCreate = function(e) {
this.manager.recipe.createSortableSeedList(e.target);
this.enableOpsListPopovers(e.target);
};
/**
* Sets up popovers, allowing the popover itself to gain focus which enables scrolling
* and other interactions.
*
* @param {Element} el - The element to start selecting from
*/
OperationsWaiter.prototype.enableOpsListPopovers = function(el) {
$(el).find("[data-toggle=popover]").addBack("[data-toggle=popover]")
.popover({trigger: "manual"})
.on("mouseenter", function(e) {
if (e.buttons > 0) return; // Mouse button held down - likely dragging an opertion
const _this = this;
$(this).popover("show");
$(".popover").on("mouseleave", function () {
$(_this).popover("hide");
});
}).on("mouseleave", function () {
const _this = this;
setTimeout(function() {
// Determine if the popover associated with this element is being hovered over
if ($(_this).data("bs.popover") &&
($(_this).data("bs.popover").$tip && !$(_this).data("bs.popover").$tip.is(":hover"))) {
$(_this).popover("hide");
}
}, 50);
});
2016-11-28 11:42:58 +01:00
};
/**
* Handler for operation doubleclick events.
* Adds the operation to the recipe and auto bakes.
*
* @param {event} e
*/
OperationsWaiter.prototype.operationDblclick = function(e) {
2017-04-13 19:08:50 +02:00
const li = e.target;
2017-02-09 16:09:33 +01:00
this.manager.recipe.addOperation(li.textContent);
2016-11-28 11:42:58 +01:00
};
/**
* Handler for edit favourites click events.
* Sets up the 'Edit favourites' pane and displays it.
*
* @param {event} e
*/
OperationsWaiter.prototype.editFavouritesClick = function(e) {
2016-11-28 11:42:58 +01:00
e.preventDefault();
e.stopPropagation();
2017-02-09 16:09:33 +01:00
2016-11-28 11:42:58 +01:00
// Add favourites to modal
2017-04-13 19:08:50 +02:00
const favCat = this.app.categories.filter(function(c) {
2016-12-14 17:39:17 +01:00
return c.name === "Favourites";
2016-11-28 11:42:58 +01:00
})[0];
2017-02-09 16:09:33 +01:00
2017-04-13 19:08:50 +02:00
let html = "";
for (let i = 0; i < favCat.ops.length; i++) {
const opName = favCat.ops[i];
const operation = new HTMLOperation(opName, this.app.operations[opName], this.app, this.manager);
html += operation.toStubHtml(true);
2016-11-28 11:42:58 +01:00
}
2017-02-09 16:09:33 +01:00
2017-04-13 19:08:50 +02:00
const editFavouritesList = document.getElementById("edit-favourites-list");
editFavouritesList.innerHTML = html;
this.removeIntent = false;
2017-02-09 16:09:33 +01:00
2017-04-13 19:31:26 +02:00
const editableList = Sortable.create(editFavouritesList, {
2016-12-14 17:39:17 +01:00
filter: ".remove-icon",
2016-11-28 11:42:58 +01:00
onFilter: function (evt) {
2017-04-13 19:08:50 +02:00
const el = editableList.closest(evt.item);
if (el && el.parentNode) {
2016-11-28 11:42:58 +01:00
$(el).popover("destroy");
el.parentNode.removeChild(el);
}
},
onEnd: function(evt) {
if (this.removeIntent) {
$(evt.item).popover("destroy");
evt.item.remove();
}
2016-11-28 11:42:58 +01:00
}.bind(this),
});
2017-02-09 16:09:33 +01:00
Sortable.utils.on(editFavouritesList, "dragleave", function() {
this.removeIntent = true;
2016-11-28 11:42:58 +01:00
}.bind(this));
2017-02-09 16:09:33 +01:00
Sortable.utils.on(editFavouritesList, "dragover", function() {
this.removeIntent = false;
2016-11-28 11:42:58 +01:00
}.bind(this));
2017-02-09 16:09:33 +01:00
2016-11-28 11:42:58 +01:00
$("#edit-favourites-list [data-toggle=popover]").popover();
$("#favourites-modal").modal();
};
/**
* Handler for save favourites click events.
* Saves the selected favourites and reloads them.
*/
OperationsWaiter.prototype.saveFavouritesClick = function() {
2017-05-03 01:40:39 +02:00
const favs = document.querySelectorAll("#edit-favourites-list li");
const favouritesList = Array.from(favs, e => e.textContent);
2016-11-28 11:42:58 +01:00
this.app.saveFavourites(favouritesList);
this.app.loadFavourites();
this.app.populateOperationsList();
this.manager.recipe.initialiseOperationDragNDrop();
2016-11-28 11:42:58 +01:00
};
/**
* Handler for reset favourites click events.
* Resets favourites to their defaults.
*/
OperationsWaiter.prototype.resetFavouritesClick = function() {
this.app.resetFavourites();
2016-11-28 11:42:58 +01:00
};
/**
* Handler for opIcon mouseover events.
2016-11-28 11:42:58 +01:00
* Hides any popovers already showing on the operation so that there aren't two at once.
*
* @param {event} e
*/
OperationsWaiter.prototype.opIconMouseover = function(e) {
2017-04-13 19:08:50 +02:00
const opEl = e.target.parentNode;
2016-12-14 17:39:17 +01:00
if (e.target.getAttribute("data-toggle") === "popover") {
$(opEl).popover("hide");
2016-11-28 11:42:58 +01:00
}
};
/**
* Handler for opIcon mouseleave events.
2016-11-28 11:42:58 +01:00
* If this icon created a popover and we're moving back to the operation element, display the
* operation popover again.
*
* @param {event} e
*/
OperationsWaiter.prototype.opIconMouseleave = function(e) {
2017-05-03 01:40:39 +02:00
const opEl = e.target.parentNode;
const toEl = e.toElement || e.relatedElement;
2017-02-09 16:09:33 +01:00
if (e.target.getAttribute("data-toggle") === "popover" && toEl === opEl) {
$(opEl).popover("show");
2016-11-28 11:42:58 +01:00
}
};
export default OperationsWaiter;