InspectorClient.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. /*
  2. * Copyright (c) 2023-2024, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Base64.h>
  7. #include <AK/JsonArray.h>
  8. #include <AK/JsonObject.h>
  9. #include <AK/StringBuilder.h>
  10. #include <LibJS/MarkupGenerator.h>
  11. #include <LibWeb/Infra/Strings.h>
  12. #include <LibWebView/InspectorClient.h>
  13. #include <LibWebView/SourceHighlighter.h>
  14. namespace WebView {
  15. static ErrorOr<JsonValue> parse_json_tree(StringView json)
  16. {
  17. auto parsed_tree = TRY(JsonValue::from_string(json));
  18. if (!parsed_tree.is_object())
  19. return Error::from_string_literal("Expected tree to be a JSON object");
  20. return parsed_tree;
  21. }
  22. InspectorClient::InspectorClient(ViewImplementation& content_web_view, ViewImplementation& inspector_web_view)
  23. : m_content_web_view(content_web_view)
  24. , m_inspector_web_view(inspector_web_view)
  25. {
  26. m_content_web_view.on_received_dom_tree = [this](auto const& dom_tree) {
  27. auto result = parse_json_tree(dom_tree);
  28. if (result.is_error()) {
  29. dbgln("Failed to load DOM tree: {}", result.error());
  30. return;
  31. }
  32. auto dom_tree_html = generate_dom_tree(result.value().as_object());
  33. auto dom_tree_base64 = MUST(encode_base64(dom_tree_html.bytes()));
  34. auto script = MUST(String::formatted("inspector.loadDOMTree(\"{}\");", dom_tree_base64));
  35. m_inspector_web_view.run_javascript(script);
  36. m_dom_tree_loaded = true;
  37. if (m_pending_selection.has_value())
  38. select_node(m_pending_selection.release_value());
  39. else
  40. select_default_node();
  41. };
  42. m_content_web_view.on_received_dom_node_properties = [this](auto const& inspected_node_properties) {
  43. StringBuilder builder;
  44. // FIXME: Support box model metrics and ARIA properties.
  45. auto generate_property_script = [&](auto const& computed_style, auto const& resolved_style, auto const& custom_properties) {
  46. builder.append("inspector.createPropertyTables(\""sv);
  47. builder.append_escaped_for_json(computed_style);
  48. builder.append("\", \""sv);
  49. builder.append_escaped_for_json(resolved_style);
  50. builder.append("\", \""sv);
  51. builder.append_escaped_for_json(custom_properties);
  52. builder.append("\");"sv);
  53. };
  54. if (inspected_node_properties.has_value()) {
  55. generate_property_script(
  56. inspected_node_properties->computed_style_json,
  57. inspected_node_properties->resolved_style_json,
  58. inspected_node_properties->custom_properties_json);
  59. } else {
  60. generate_property_script("{}"sv, "{}"sv, "{}"sv);
  61. }
  62. m_inspector_web_view.run_javascript(builder.string_view());
  63. };
  64. m_content_web_view.on_received_accessibility_tree = [this](auto const& accessibility_tree) {
  65. auto result = parse_json_tree(accessibility_tree);
  66. if (result.is_error()) {
  67. dbgln("Failed to load accessibility tree: {}", result.error());
  68. return;
  69. }
  70. auto accessibility_tree_html = generate_accessibility_tree(result.value().as_object());
  71. auto accessibility_tree_base64 = MUST(encode_base64(accessibility_tree_html.bytes()));
  72. auto script = MUST(String::formatted("inspector.loadAccessibilityTree(\"{}\");", accessibility_tree_base64));
  73. m_inspector_web_view.run_javascript(script);
  74. };
  75. m_content_web_view.on_received_hovered_node_id = [this](auto node_id) {
  76. select_node(node_id);
  77. };
  78. m_content_web_view.on_finshed_editing_dom_node = [this](auto const& node_id) {
  79. m_pending_selection = node_id;
  80. m_dom_tree_loaded = false;
  81. inspect();
  82. };
  83. m_content_web_view.on_received_dom_node_html = [this](auto const& html) {
  84. if (m_content_web_view.on_insert_clipboard_entry)
  85. m_content_web_view.on_insert_clipboard_entry(html, "unspecified"_string, "text/plain"_string);
  86. };
  87. m_content_web_view.on_received_console_message = [this](auto message_index) {
  88. handle_console_message(message_index);
  89. };
  90. m_content_web_view.on_received_console_messages = [this](auto start_index, auto const& message_types, auto const& messages) {
  91. handle_console_messages(start_index, message_types, messages);
  92. };
  93. m_inspector_web_view.enable_inspector_prototype();
  94. m_inspector_web_view.use_native_user_style_sheet();
  95. m_inspector_web_view.on_inspector_loaded = [this]() {
  96. inspect();
  97. m_content_web_view.js_console_request_messages(0);
  98. };
  99. m_inspector_web_view.on_inspector_requested_dom_tree_context_menu = [this](auto node_id, auto position, auto const& type, auto const& tag, auto const& attribute) {
  100. m_context_menu_data = ContextMenuData { node_id, tag, attribute };
  101. if (type.is_one_of("text"sv, "comment"sv)) {
  102. if (on_requested_dom_node_text_context_menu)
  103. on_requested_dom_node_text_context_menu(position);
  104. } else if (type == "tag"sv) {
  105. VERIFY(tag.has_value());
  106. if (on_requested_dom_node_tag_context_menu)
  107. on_requested_dom_node_tag_context_menu(position, *tag);
  108. } else if (type == "attribute"sv) {
  109. VERIFY(tag.has_value());
  110. VERIFY(attribute.has_value());
  111. if (on_requested_dom_node_attribute_context_menu)
  112. on_requested_dom_node_attribute_context_menu(position, *tag, *attribute);
  113. }
  114. };
  115. m_inspector_web_view.on_inspector_selected_dom_node = [this](auto node_id, auto const& pseudo_element) {
  116. m_content_web_view.inspect_dom_node(node_id, pseudo_element);
  117. };
  118. m_inspector_web_view.on_inspector_set_dom_node_text = [this](auto node_id, auto const& text) {
  119. m_content_web_view.set_dom_node_text(node_id, text);
  120. };
  121. m_inspector_web_view.on_inspector_set_dom_node_tag = [this](auto node_id, auto const& tag) {
  122. m_content_web_view.set_dom_node_tag(node_id, tag);
  123. };
  124. m_inspector_web_view.on_inspector_added_dom_node_attributes = [this](auto node_id, auto const& attributes) {
  125. m_content_web_view.add_dom_node_attributes(node_id, attributes);
  126. };
  127. m_inspector_web_view.on_inspector_replaced_dom_node_attribute = [this](auto node_id, auto const& name, auto const& replacement_attributes) {
  128. m_content_web_view.replace_dom_node_attribute(node_id, name, replacement_attributes);
  129. };
  130. m_inspector_web_view.on_inspector_executed_console_script = [this](auto const& script) {
  131. append_console_source(script);
  132. m_content_web_view.js_console_input(script.to_byte_string());
  133. };
  134. load_inspector();
  135. }
  136. InspectorClient::~InspectorClient()
  137. {
  138. m_content_web_view.on_finshed_editing_dom_node = nullptr;
  139. m_content_web_view.on_received_accessibility_tree = nullptr;
  140. m_content_web_view.on_received_console_message = nullptr;
  141. m_content_web_view.on_received_console_messages = nullptr;
  142. m_content_web_view.on_received_dom_node_html = nullptr;
  143. m_content_web_view.on_received_dom_node_properties = nullptr;
  144. m_content_web_view.on_received_dom_tree = nullptr;
  145. m_content_web_view.on_received_hovered_node_id = nullptr;
  146. }
  147. void InspectorClient::inspect()
  148. {
  149. if (m_dom_tree_loaded)
  150. return;
  151. m_content_web_view.inspect_dom_tree();
  152. m_content_web_view.inspect_accessibility_tree();
  153. }
  154. void InspectorClient::reset()
  155. {
  156. static constexpr auto script = "inspector.reset();"sv;
  157. m_inspector_web_view.run_javascript(script);
  158. m_body_node_id.clear();
  159. m_pending_selection.clear();
  160. m_dom_tree_loaded = false;
  161. m_highest_notified_message_index = -1;
  162. m_highest_received_message_index = -1;
  163. m_waiting_for_messages = false;
  164. }
  165. void InspectorClient::select_hovered_node()
  166. {
  167. m_content_web_view.get_hovered_node_id();
  168. }
  169. void InspectorClient::select_default_node()
  170. {
  171. if (m_body_node_id.has_value())
  172. select_node(*m_body_node_id);
  173. }
  174. void InspectorClient::clear_selection()
  175. {
  176. m_content_web_view.clear_inspected_dom_node();
  177. static constexpr auto script = "inspector.clearInspectedDOMNode();"sv;
  178. m_inspector_web_view.run_javascript(script);
  179. }
  180. void InspectorClient::select_node(i32 node_id)
  181. {
  182. if (!m_dom_tree_loaded) {
  183. m_pending_selection = node_id;
  184. return;
  185. }
  186. auto script = MUST(String::formatted("inspector.inspectDOMNodeID({});", node_id));
  187. m_inspector_web_view.run_javascript(script);
  188. }
  189. void InspectorClient::context_menu_edit_dom_node()
  190. {
  191. VERIFY(m_context_menu_data.has_value());
  192. auto script = MUST(String::formatted("inspector.editDOMNodeID({});", m_context_menu_data->dom_node_id));
  193. m_inspector_web_view.run_javascript(script);
  194. m_context_menu_data.clear();
  195. }
  196. void InspectorClient::context_menu_copy_dom_node()
  197. {
  198. VERIFY(m_context_menu_data.has_value());
  199. m_content_web_view.get_dom_node_html(m_context_menu_data->dom_node_id);
  200. m_context_menu_data.clear();
  201. }
  202. void InspectorClient::context_menu_screenshot_dom_node()
  203. {
  204. VERIFY(m_context_menu_data.has_value());
  205. m_content_web_view.take_dom_node_screenshot(m_context_menu_data->dom_node_id)
  206. ->when_resolved([this](auto const& path) {
  207. append_console_message(MUST(String::formatted("Screenshot saved to: {}", path)));
  208. })
  209. .when_rejected([this](auto const& error) {
  210. append_console_warning(MUST(String::formatted("Warning: {}", error)));
  211. });
  212. m_context_menu_data.clear();
  213. }
  214. void InspectorClient::context_menu_create_child_element()
  215. {
  216. VERIFY(m_context_menu_data.has_value());
  217. m_content_web_view.create_child_element(m_context_menu_data->dom_node_id);
  218. m_context_menu_data.clear();
  219. }
  220. void InspectorClient::context_menu_create_child_text_node()
  221. {
  222. VERIFY(m_context_menu_data.has_value());
  223. m_content_web_view.create_child_text_node(m_context_menu_data->dom_node_id);
  224. m_context_menu_data.clear();
  225. }
  226. void InspectorClient::context_menu_clone_dom_node()
  227. {
  228. VERIFY(m_context_menu_data.has_value());
  229. m_content_web_view.clone_dom_node(m_context_menu_data->dom_node_id);
  230. m_context_menu_data.clear();
  231. }
  232. void InspectorClient::context_menu_remove_dom_node()
  233. {
  234. VERIFY(m_context_menu_data.has_value());
  235. m_content_web_view.remove_dom_node(m_context_menu_data->dom_node_id);
  236. m_context_menu_data.clear();
  237. }
  238. void InspectorClient::context_menu_add_dom_node_attribute()
  239. {
  240. VERIFY(m_context_menu_data.has_value());
  241. auto script = MUST(String::formatted("inspector.addAttributeToDOMNodeID({});", m_context_menu_data->dom_node_id));
  242. m_inspector_web_view.run_javascript(script);
  243. m_context_menu_data.clear();
  244. }
  245. void InspectorClient::context_menu_remove_dom_node_attribute()
  246. {
  247. VERIFY(m_context_menu_data.has_value());
  248. VERIFY(m_context_menu_data->attribute.has_value());
  249. m_content_web_view.replace_dom_node_attribute(m_context_menu_data->dom_node_id, m_context_menu_data->attribute->name, {});
  250. m_context_menu_data.clear();
  251. }
  252. void InspectorClient::context_menu_copy_dom_node_attribute_value()
  253. {
  254. VERIFY(m_context_menu_data.has_value());
  255. VERIFY(m_context_menu_data->attribute.has_value());
  256. if (m_content_web_view.on_insert_clipboard_entry)
  257. m_content_web_view.on_insert_clipboard_entry(m_context_menu_data->attribute->value, "unspecified"_string, "text/plain"_string);
  258. m_context_menu_data.clear();
  259. }
  260. void InspectorClient::load_inspector()
  261. {
  262. StringBuilder builder;
  263. builder.append(R"~~~(
  264. <!DOCTYPE html>
  265. <html>
  266. <head>
  267. <meta name="color-scheme" content="dark light">
  268. <style type="text/css">
  269. )~~~"sv);
  270. builder.append(HTML_HIGHLIGHTER_STYLE);
  271. builder.append(R"~~~(
  272. </style>
  273. <link href="resource://ladybird/inspector.css" rel="stylesheet" />
  274. </head>
  275. <body>
  276. <div class="split-view">
  277. <div id="inspector-top" class="split-view-container" style="height: 60%">
  278. <div class="tab-controls-container">
  279. <div class="tab-controls">
  280. <button id="dom-tree-button" onclick="selectTopTab(this, 'dom-tree')">DOM Tree</button>
  281. <button id="accessibility-tree-button" onclick="selectTopTab(this, 'accessibility-tree')">Accessibility Tree</button>
  282. </div>
  283. </div>
  284. <div id="dom-tree" class="tab-content html"></div>
  285. <div id="accessibility-tree" class="tab-content"></div>
  286. </div>
  287. <div id="inspector-separator" class="split-view-separator">
  288. <svg viewBox="0 0 16 5" xmlns="http://www.w3.org/2000/svg">
  289. <circle cx="2" cy="2.5" r="2" />
  290. <circle cx="8" cy="2.5" r="2" />
  291. <circle cx="14" cy="2.5" r="2" />
  292. </svg>
  293. </div>
  294. <div id="inspector-bottom" class="split-view-container" style="height: calc(40% - 5px)">
  295. <div class="tab-controls-container">
  296. <div class="tab-controls">
  297. <button id="console-button" onclick="selectBottomTab(this, 'console')">Console</button>
  298. <button id="computed-style-button" onclick="selectBottomTab(this, 'computed-style')">Computed Style</button>
  299. <button id="resolved-style-button" onclick="selectBottomTab(this, 'resolved-style')">Resolved Style</button>
  300. <button id="custom-properties-button" onclick="selectBottomTab(this, 'custom-properties')">Custom Properties</button>
  301. </div>
  302. </div>
  303. <div id="console" class="tab-content">
  304. <div class="console">
  305. <div id="console-output" class="console-output"></div>
  306. <div class="console-input">
  307. <label for="console-input" class="console-prompt">&gt;&gt;</label>
  308. <input id="console-input" type="text" placeholder="Enter statement to execute">
  309. <button id="console-clear" title="Clear the console output" onclick="inspector.clearConsoleOutput()">X</button>
  310. </div>
  311. </div>
  312. </div>
  313. )~~~"sv);
  314. auto generate_property_table = [&](auto name) {
  315. builder.appendff(R"~~~(
  316. <div id="{0}" class="tab-content">
  317. <table class="property-table">
  318. <thead>
  319. <tr>
  320. <th>Name</th>
  321. <th>Value</th>
  322. </tr>
  323. </thead>
  324. <tbody id="{0}-table">
  325. </tbody>
  326. </table>
  327. </div>
  328. )~~~",
  329. name);
  330. };
  331. generate_property_table("computed-style"sv);
  332. generate_property_table("resolved-style"sv);
  333. generate_property_table("custom-properties"sv);
  334. builder.append(R"~~~(
  335. </div>
  336. </div>
  337. <script type="text/javascript" src="resource://ladybird/inspector.js"></script>
  338. </body>
  339. </html>
  340. )~~~"sv);
  341. m_inspector_web_view.load_html(builder.string_view());
  342. }
  343. template<typename Generator>
  344. static void generate_tree(StringBuilder& builder, JsonObject const& node, Generator&& generator)
  345. {
  346. if (auto children = node.get_array("children"sv); children.has_value() && !children->is_empty()) {
  347. auto name = node.get_byte_string("name"sv).value_or({});
  348. builder.append("<details>"sv);
  349. builder.append("<summary>"sv);
  350. generator(node);
  351. builder.append("</summary>"sv);
  352. children->for_each([&](auto const& child) {
  353. builder.append("<div>"sv);
  354. generate_tree(builder, child.as_object(), generator);
  355. builder.append("</div>"sv);
  356. });
  357. builder.append("</details>"sv);
  358. } else {
  359. generator(node);
  360. }
  361. }
  362. String InspectorClient::generate_dom_tree(JsonObject const& dom_tree)
  363. {
  364. StringBuilder builder;
  365. generate_tree(builder, dom_tree, [&](JsonObject const& node) {
  366. auto type = node.get_byte_string("type"sv).value_or("unknown"sv);
  367. auto name = node.get_byte_string("name"sv).value_or({});
  368. StringBuilder data_attributes;
  369. auto append_data_attribute = [&](auto name, auto value) {
  370. if (!data_attributes.is_empty())
  371. data_attributes.append(' ');
  372. data_attributes.appendff("data-{}=\"{}\"", name, value);
  373. };
  374. if (auto pseudo_element = node.get_integer<i32>("pseudo-element"sv); pseudo_element.has_value()) {
  375. append_data_attribute("id"sv, node.get_integer<i32>("parent-id"sv).value());
  376. append_data_attribute("pseudo-element"sv, *pseudo_element);
  377. } else {
  378. append_data_attribute("id"sv, node.get_integer<i32>("id"sv).value());
  379. }
  380. if (type == "text"sv) {
  381. auto deprecated_text = node.get_byte_string("text"sv).release_value();
  382. deprecated_text = escape_html_entities(deprecated_text);
  383. auto text = MUST(Web::Infra::strip_and_collapse_whitespace(deprecated_text));
  384. builder.appendff("<span data-node-type=\"text\" data-text=\"{}\" class=\"hoverable editable\" {}>", text, data_attributes.string_view());
  385. if (text.is_empty())
  386. builder.appendff("<span class=\"internal\">{}</span>", name);
  387. else
  388. builder.append(text);
  389. builder.append("</span>"sv);
  390. return;
  391. }
  392. if (type == "comment"sv) {
  393. auto comment = node.get_byte_string("data"sv).release_value();
  394. comment = escape_html_entities(comment);
  395. builder.appendff("<span class=\"hoverable comment\" {}>", data_attributes.string_view());
  396. builder.append("<span>&lt;!--</span>"sv);
  397. builder.appendff("<span data-node-type=\"comment\" class=\"editable\">{}</span>", comment);
  398. builder.append("<span>--&gt;</span>"sv);
  399. builder.append("</span>"sv);
  400. return;
  401. }
  402. if (type == "shadow-root"sv) {
  403. auto mode = node.get_byte_string("mode"sv).release_value();
  404. builder.appendff("<span class=\"hoverable internal\" {}>", data_attributes.string_view());
  405. builder.appendff("{} ({})", name, mode);
  406. builder.append("</span>"sv);
  407. return;
  408. }
  409. if (type != "element"sv) {
  410. builder.appendff("<span class=\"hoverable internal\" {}>", data_attributes.string_view());
  411. builder.appendff(name);
  412. builder.append("</span>"sv);
  413. return;
  414. }
  415. if (name.equals_ignoring_ascii_case("BODY"sv))
  416. m_body_node_id = node.get_integer<i32>("id"sv).value();
  417. auto tag = name.to_lowercase();
  418. builder.appendff("<span class=\"hoverable\" {}>", data_attributes.string_view());
  419. builder.append("<span>&lt;</span>"sv);
  420. builder.appendff("<span data-node-type=\"tag\" data-tag=\"{0}\" class=\"editable tag\">{0}</span>", tag);
  421. if (auto attributes = node.get_object("attributes"sv); attributes.has_value()) {
  422. attributes->for_each_member([&](auto const& name, auto const& value) {
  423. auto value_string = value.as_string();
  424. builder.append("&nbsp;"sv);
  425. builder.appendff("<span data-node-type=\"attribute\" data-tag=\"{}\" data-attribute-name=\"{}\" data-attribute-value=\"{}\" class=\"editable\">", tag, name, value_string);
  426. builder.appendff("<span class=\"attribute-name\">{}</span>", name);
  427. builder.append('=');
  428. builder.appendff("<span class=\"attribute-value\">\"{}\"</span>", value_string);
  429. builder.append("</span>"sv);
  430. });
  431. }
  432. builder.append("<span>&gt;</span>"sv);
  433. builder.append("</span>"sv);
  434. });
  435. return MUST(builder.to_string());
  436. }
  437. String InspectorClient::generate_accessibility_tree(JsonObject const& accessibility_tree)
  438. {
  439. StringBuilder builder;
  440. generate_tree(builder, accessibility_tree, [&](JsonObject const& node) {
  441. auto type = node.get_byte_string("type"sv).value_or("unknown"sv);
  442. auto role = node.get_byte_string("role"sv).value_or({});
  443. if (type == "text"sv) {
  444. auto text = node.get_byte_string("text"sv).release_value();
  445. text = escape_html_entities(text);
  446. builder.appendff("<span class=\"hoverable\">");
  447. builder.append(MUST(Web::Infra::strip_and_collapse_whitespace(text)));
  448. builder.append("</span>"sv);
  449. return;
  450. }
  451. if (type != "element"sv) {
  452. builder.appendff("<span class=\"hoverable internal\">");
  453. builder.appendff(role.to_lowercase());
  454. builder.append("</span>"sv);
  455. return;
  456. }
  457. auto name = node.get_byte_string("name"sv).value_or({});
  458. auto description = node.get_byte_string("description"sv).value_or({});
  459. builder.appendff("<span class=\"hoverable\">");
  460. builder.append(role.to_lowercase());
  461. builder.appendff(" name: \"{}\", description: \"{}\"", name, description);
  462. builder.append("</span>"sv);
  463. });
  464. return MUST(builder.to_string());
  465. }
  466. void InspectorClient::request_console_messages()
  467. {
  468. VERIFY(!m_waiting_for_messages);
  469. m_content_web_view.js_console_request_messages(m_highest_received_message_index + 1);
  470. m_waiting_for_messages = true;
  471. }
  472. void InspectorClient::handle_console_message(i32 message_index)
  473. {
  474. if (message_index <= m_highest_received_message_index) {
  475. dbgln("Notified about console message we already have");
  476. return;
  477. }
  478. if (message_index <= m_highest_notified_message_index) {
  479. dbgln("Notified about console message we're already aware of");
  480. return;
  481. }
  482. m_highest_notified_message_index = message_index;
  483. if (!m_waiting_for_messages)
  484. request_console_messages();
  485. }
  486. void InspectorClient::handle_console_messages(i32 start_index, ReadonlySpan<ByteString> message_types, ReadonlySpan<ByteString> messages)
  487. {
  488. auto end_index = start_index + static_cast<i32>(message_types.size()) - 1;
  489. if (end_index <= m_highest_received_message_index) {
  490. dbgln("Received old console messages");
  491. return;
  492. }
  493. for (size_t i = 0; i < message_types.size(); ++i) {
  494. auto const& type = message_types[i];
  495. auto const& message = messages[i];
  496. if (type == "html"sv)
  497. append_console_output(message);
  498. else if (type == "clear"sv)
  499. clear_console_output();
  500. else if (type == "group"sv)
  501. begin_console_group(message, true);
  502. else if (type == "groupCollapsed"sv)
  503. begin_console_group(message, false);
  504. else if (type == "groupEnd"sv)
  505. end_console_group();
  506. else
  507. VERIFY_NOT_REACHED();
  508. }
  509. m_highest_received_message_index = end_index;
  510. m_waiting_for_messages = false;
  511. if (m_highest_received_message_index < m_highest_notified_message_index)
  512. request_console_messages();
  513. }
  514. void InspectorClient::append_console_source(StringView source)
  515. {
  516. StringBuilder builder;
  517. builder.append("<span class=\"console-prompt\">&gt;&nbsp;</span>"sv);
  518. builder.append(MUST(JS::MarkupGenerator::html_from_source(source)));
  519. append_console_output(builder.string_view());
  520. }
  521. void InspectorClient::append_console_message(StringView message)
  522. {
  523. StringBuilder builder;
  524. builder.append("<span class=\"console-prompt\">#&nbsp;</span>"sv);
  525. builder.appendff("<span class=\"console-message\">{}</span>", message);
  526. append_console_output(builder.string_view());
  527. }
  528. void InspectorClient::append_console_warning(StringView warning)
  529. {
  530. StringBuilder builder;
  531. builder.append("<span class=\"console-prompt\">#&nbsp;</span>"sv);
  532. builder.appendff("<span class=\"console-warning\">{}</span>", warning);
  533. append_console_output(builder.string_view());
  534. }
  535. void InspectorClient::append_console_output(StringView html)
  536. {
  537. auto html_base64 = MUST(encode_base64(html.bytes()));
  538. auto script = MUST(String::formatted("inspector.appendConsoleOutput(\"{}\");", html_base64));
  539. m_inspector_web_view.run_javascript(script);
  540. }
  541. void InspectorClient::clear_console_output()
  542. {
  543. static constexpr auto script = "inspector.clearConsoleOutput();"sv;
  544. m_inspector_web_view.run_javascript(script);
  545. }
  546. void InspectorClient::begin_console_group(StringView label, bool start_expanded)
  547. {
  548. auto label_base64 = MUST(encode_base64(label.bytes()));
  549. auto script = MUST(String::formatted("inspector.beginConsoleGroup(\"{}\", {});", label_base64, start_expanded));
  550. m_inspector_web_view.run_javascript(script);
  551. }
  552. void InspectorClient::end_console_group()
  553. {
  554. static constexpr auto script = "inspector.endConsoleGroup();"sv;
  555. m_inspector_web_view.run_javascript(script);
  556. }
  557. }