InspectorClient.cpp 25 KB

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