HTMLOperation.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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='right'\
  45. data-content=\"" + this.description + "\" data-html='true' data-trigger='hover'\
  46. data-boundary='viewport'";
  47. }
  48. html += ">" + this.name;
  49. if (removeIcon) {
  50. html += "<img src='data:image/png;base64," + HTMLOperation.REMOVE_ICON +
  51. "' class='op-icon remove-icon'>";
  52. }
  53. if (this.description) {
  54. html += "<img src='data:image/png;base64," + HTMLOperation.INFO_ICON + "' class='op-icon'>";
  55. }
  56. html += "</li>";
  57. return html;
  58. };
  59. /**
  60. * Renders the operation in HTML as a full operation with ingredients.
  61. *
  62. * @returns {string}
  63. */
  64. HTMLOperation.prototype.toFullHtml = function() {
  65. let html = "<div class='arg-title'>" + this.name + "</div>";
  66. for (let i = 0; i < this.ingList.length; i++) {
  67. html += this.ingList[i].toHtml();
  68. }
  69. html += "<div class='recip-icons'>\
  70. <div class='breakpoint' title='Set breakpoint' break='false'></div>\
  71. <div class='disable-icon recip-icon' title='Disable operation'\
  72. disabled='false'></div>";
  73. html += "</div>\
  74. <div class='clearfix'>&nbsp;</div>";
  75. return html;
  76. };
  77. /**
  78. * Highlights the searched string in the name and description of the operation.
  79. *
  80. * @param {string} searchStr
  81. * @param {number} namePos - The position of the search string in the operation name
  82. * @param {number} descPos - The position of the search string in the operation description
  83. */
  84. HTMLOperation.prototype.highlightSearchString = function(searchStr, namePos, descPos) {
  85. if (namePos >= 0) {
  86. this.name = this.name.slice(0, namePos) + "<b><u>" +
  87. this.name.slice(namePos, namePos + searchStr.length) + "</u></b>" +
  88. this.name.slice(namePos + searchStr.length);
  89. }
  90. if (this.description && descPos >= 0) {
  91. // Find HTML tag offsets
  92. const re = /<[^>]+>/g;
  93. let match;
  94. while ((match = re.exec(this.description))) {
  95. // If the search string occurs within an HTML tag, return without highlighting it.
  96. if (descPos >= match.index && descPos <= (match.index + match[0].length))
  97. return;
  98. }
  99. this.description = this.description.slice(0, descPos) + "<b><u>" +
  100. this.description.slice(descPos, descPos + searchStr.length) + "</u></b>" +
  101. this.description.slice(descPos + searchStr.length);
  102. }
  103. };
  104. export default HTMLOperation;