HTMLOperation.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import HTMLIngredient from "./HTMLIngredient.js";
  2. /**
  3. * Object to handle the creation of operations.
  4. *
  5. * @author n1474335 [n1474335@gmail.com]
  6. * @copyright Crown Copyright 2016
  7. * @license Apache-2.0
  8. *
  9. * @constructor
  10. * @param {string} name - The name of the operation.
  11. * @param {Object} config - The configuration object for this operation.
  12. * @param {App} app - The main view object for CyberChef.
  13. * @param {Manager} manager - The CyberChef event manager.
  14. */
  15. const HTMLOperation = function(name, config, app, manager) {
  16. this.app = app;
  17. this.manager = manager;
  18. this.name = name;
  19. this.description = config.description;
  20. this.manualBake = config.manualBake || false;
  21. this.config = config;
  22. this.ingList = [];
  23. for (let i = 0; i < config.args.length; i++) {
  24. const ing = new HTMLIngredient(config.args[i], this.app, this.manager);
  25. this.ingList.push(ing);
  26. }
  27. };
  28. /**
  29. * @constant
  30. */
  31. HTMLOperation.INFO_ICON = "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAByElEQVR4XqVTzWoaYRQ9KZJmoVaS1J1QiYTIuOgqi9lEugguQhYhdGs3hTyAi0CWJTvJIks30ZBNsimUtlqkVLoQCuJsphRriyFjabWtEyf/Rv3iWcwwymTlgQuH851z5hu43wRGkEwmXwCIA4hiGAUAmUQikQbhEHwyGCWVSglVVUW73RYmyKnxjB56ncJ6NpsVxHGrI/ZLuniVb3DIqQmCHnrNkgcggNeSJPlisRgyJR2b737j/TcDsQUPwv6H5NR4BnroZcb6Z16N2PvyX6yna9Z8qp6JQ0Uf0ughmGHWBSAuyzJqrQ7eqKewY/dzE363C71e39LoWQq5wUwul4uzIBoIBHD01RgyrkZ8eDbvwUWnj623v2DHx4qB51IAzLIAXq8XP/7W0bUVVJtXWIk8wvlN364TA+/1IDMLwmWK/Hq3axmhaBdoGLeklm73ElaBYRgIzkyifHIOO4QQJKM3oJcZq6CgaVp0OTyHw9K/kQI4FiyHfdC0n2CWe5ApFosIPZ7C2tNpXpcDOehGyD/FIbd0euhlhllzFxRzC3fydbG4XRYbB9/tQ41n9m1U7l3lyp9LkfygiZeZCoecmtMqj/+Yxn7Od3v0j50qCO3zAAAAAElFTkSuQmCC";
  32. /**
  33. * @constant
  34. */
  35. HTMLOperation.REMOVE_ICON = "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAABwklEQVR42qRTPU8CQRB9K2CCMRJ6NTQajOUaqfxIbLCRghhjQixosLAgFNBQ3l8wsabxLxBJbCyVUBiMCVQEQkOEKBbCnefM3p4eohWXzM3uvHlv52b2hG3bmOWZw4yPn1/XQkCQ9wFxcgZZ0QLKpifpN8Z1n1L13griBBjHhYK0nMT4b+wom53ClAAFQacZJ/m8rNfrSOZy0vxJjPP6IJ2WzWYTO6mUwiwtILiJJSHUKVSWkchkZK1WQzQaxU2pVGUglkjIbreLUCiEx0qlStlFCpfPiPstYDtVKJH9ZFI2Gw1FGA6H6LTbCAaDeGu1FJl6UuYjpwTGzucokZW1NfnS66kyfT4fXns9RaZmlgNcuhZQU+jowLzuOK/HgwEW3E5ZlhLXVWKk11P3wNYNWw+HZdA0sUgx1zjGmD05nckx0ilGjBJdUq3fr7K5e8bGf43RdL7fOPSQb4lI8SLbrUfkUIuY32VTI1bJn5BqDnh4Dodt9ryPUDzyD7aquWoKQohl2i9sAbubwPkTcHkP3FHsg+yT+7sN7G0AF3Xg6sHB3onbdgWWKBDQg/BcTuVt51dQA/JrnIcyIu6rmPV3/hJgACPc0BMEYTg+AAAAAElFTkSuQmCC";
  36. /**
  37. * Renders the operation in HTML as a stub operation with no ingredients.
  38. *
  39. * @returns {string}
  40. */
  41. HTMLOperation.prototype.toStubHtml = function(removeIcon) {
  42. let html = "<li class='operation'";
  43. if (this.description) {
  44. html += " data-container='body' data-toggle='popover' data-placement='auto right'\
  45. data-content=\"" + this.description + "\" data-html='true' data-trigger='hover'";
  46. }
  47. html += ">" + this.name;
  48. if (removeIcon) {
  49. html += "<img src='data:image/png;base64," + HTMLOperation.REMOVE_ICON +
  50. "' class='op-icon remove-icon'>";
  51. }
  52. if (this.description) {
  53. html += "<img src='data:image/png;base64," + HTMLOperation.INFO_ICON + "' class='op-icon'>";
  54. }
  55. html += "</li>";
  56. return html;
  57. };
  58. /**
  59. * Renders the operation in HTML as a full operation with ingredients.
  60. *
  61. * @returns {string}
  62. */
  63. HTMLOperation.prototype.toFullHtml = function() {
  64. let html = "<div class='arg-title'>" + this.name + "</div>";
  65. for (let i = 0; i < this.ingList.length; i++) {
  66. html += this.ingList[i].toHtml();
  67. }
  68. html += "<div class='recip-icons'>\
  69. <div class='breakpoint' title='Set breakpoint' break='false'></div>\
  70. <div class='disable-icon recip-icon' title='Disable operation'\
  71. disabled='false'></div>";
  72. html += "</div>\
  73. <div class='clearfix'>&nbsp;</div>";
  74. return html;
  75. };
  76. /**
  77. * Highlights the searched string in the name and description of the operation.
  78. *
  79. * @param {string} searchStr
  80. * @param {number} namePos - The position of the search string in the operation name
  81. * @param {number} descPos - The position of the search string in the operation description
  82. */
  83. HTMLOperation.prototype.highlightSearchString = function(searchStr, namePos, descPos) {
  84. if (namePos >= 0) {
  85. this.name = this.name.slice(0, namePos) + "<b><u>" +
  86. this.name.slice(namePos, namePos + searchStr.length) + "</u></b>" +
  87. this.name.slice(namePos + searchStr.length);
  88. }
  89. if (this.description && descPos >= 0) {
  90. // Find HTML tag offsets
  91. const re = /<[^>]+>/g;
  92. let match;
  93. while ((match = re.exec(this.description))) {
  94. // If the search string occurs within an HTML tag, return without highlighting it.
  95. if (descPos >= match.index && descPos <= (match.index + match[0].length))
  96. return;
  97. }
  98. this.description = this.description.slice(0, descPos) + "<b><u>" +
  99. this.description.slice(descPos, descPos + searchStr.length) + "</u></b>" +
  100. this.description.slice(descPos + searchStr.length);
  101. }
  102. };
  103. export default HTMLOperation;