HTMLOperation.mjs 5.2 KB

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