InspectorClient.cpp 25 KB

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