InspectorClient.cpp 25 KB

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