Inspector.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright (c) 2023-2024, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/Realm.h>
  7. #include <LibWeb/Bindings/InspectorPrototype.h>
  8. #include <LibWeb/Bindings/Intrinsics.h>
  9. #include <LibWeb/CSS/Selector.h>
  10. #include <LibWeb/CSS/StyleSheetIdentifier.h>
  11. #include <LibWeb/DOM/NamedNodeMap.h>
  12. #include <LibWeb/HTML/Window.h>
  13. #include <LibWeb/Internals/Inspector.h>
  14. #include <LibWeb/Page/Page.h>
  15. namespace Web::Internals {
  16. GC_DEFINE_ALLOCATOR(Inspector);
  17. Inspector::Inspector(JS::Realm& realm)
  18. : Bindings::PlatformObject(realm)
  19. {
  20. }
  21. Inspector::~Inspector() = default;
  22. void Inspector::initialize(JS::Realm& realm)
  23. {
  24. Base::initialize(realm);
  25. WEB_SET_PROTOTYPE_FOR_INTERFACE(Inspector);
  26. }
  27. PageClient& Inspector::inspector_page_client() const
  28. {
  29. return verify_cast<HTML::Window>(HTML::relevant_global_object(*this)).page().client();
  30. }
  31. void Inspector::inspector_loaded()
  32. {
  33. inspector_page_client().inspector_did_load();
  34. }
  35. void Inspector::inspect_dom_node(i64 node_id, Optional<i32> const& pseudo_element)
  36. {
  37. inspector_page_client().inspector_did_select_dom_node(node_id, pseudo_element.map([](auto value) {
  38. VERIFY(value < to_underlying(Web::CSS::Selector::PseudoElement::Type::KnownPseudoElementCount));
  39. return static_cast<Web::CSS::Selector::PseudoElement::Type>(value);
  40. }));
  41. }
  42. void Inspector::set_dom_node_text(i64 node_id, String const& text)
  43. {
  44. inspector_page_client().inspector_did_set_dom_node_text(node_id, text);
  45. }
  46. void Inspector::set_dom_node_tag(i64 node_id, String const& tag)
  47. {
  48. inspector_page_client().inspector_did_set_dom_node_tag(node_id, tag);
  49. }
  50. void Inspector::add_dom_node_attributes(i64 node_id, GC::Ref<DOM::NamedNodeMap> attributes)
  51. {
  52. inspector_page_client().inspector_did_add_dom_node_attributes(node_id, attributes);
  53. }
  54. void Inspector::replace_dom_node_attribute(i64 node_id, WebIDL::UnsignedLongLong attribute_index, GC::Ref<DOM::NamedNodeMap> replacement_attributes)
  55. {
  56. inspector_page_client().inspector_did_replace_dom_node_attribute(node_id, static_cast<size_t>(attribute_index), replacement_attributes);
  57. }
  58. void Inspector::request_dom_tree_context_menu(i64 node_id, i32 client_x, i32 client_y, String const& type, Optional<String> const& tag, Optional<WebIDL::UnsignedLongLong> const& attribute_index)
  59. {
  60. inspector_page_client().inspector_did_request_dom_tree_context_menu(node_id, { client_x, client_y }, type, tag, attribute_index.map([](auto index) { return static_cast<size_t>(index); }));
  61. }
  62. void Inspector::request_cookie_context_menu(WebIDL::UnsignedLongLong cookie_index, i32 client_x, i32 client_y)
  63. {
  64. inspector_page_client().inspector_did_request_cookie_context_menu(cookie_index, { client_x, client_y });
  65. }
  66. void Inspector::request_style_sheet_source(String const& type_string, Optional<i64> const& dom_node_unique_id, Optional<String> const& url)
  67. {
  68. auto type = CSS::style_sheet_identifier_type_from_string(type_string);
  69. VERIFY(type.has_value());
  70. Optional<UniqueNodeID> dom_node_unique_id_opt;
  71. if (dom_node_unique_id.has_value())
  72. dom_node_unique_id_opt = dom_node_unique_id.value();
  73. inspector_page_client().inspector_did_request_style_sheet_source({
  74. .type = type.value(),
  75. .dom_element_unique_id = dom_node_unique_id_opt,
  76. .url = url,
  77. });
  78. }
  79. void Inspector::execute_console_script(String const& script)
  80. {
  81. inspector_page_client().inspector_did_execute_console_script(script);
  82. }
  83. void Inspector::export_inspector_html(String const& html)
  84. {
  85. inspector_page_client().inspector_did_export_inspector_html(html);
  86. }
  87. }