InspectorClient.cpp 25 KB

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