Inspector.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 <LibJS/Runtime/VM.h>
  8. #include <LibWeb/Bindings/InspectorPrototype.h>
  9. #include <LibWeb/Bindings/Intrinsics.h>
  10. #include <LibWeb/CSS/Selector.h>
  11. #include <LibWeb/CSS/StyleSheetIdentifier.h>
  12. #include <LibWeb/DOM/NamedNodeMap.h>
  13. #include <LibWeb/HTML/BrowsingContext.h>
  14. #include <LibWeb/HTML/Window.h>
  15. #include <LibWeb/Internals/Inspector.h>
  16. #include <LibWeb/Page/Page.h>
  17. namespace Web::Internals {
  18. JS_DEFINE_ALLOCATOR(Inspector);
  19. Inspector::Inspector(JS::Realm& realm)
  20. : Bindings::PlatformObject(realm)
  21. {
  22. }
  23. Inspector::~Inspector() = default;
  24. void Inspector::initialize(JS::Realm& realm)
  25. {
  26. Base::initialize(realm);
  27. WEB_SET_PROTOTYPE_FOR_INTERFACE(Inspector);
  28. }
  29. PageClient& Inspector::inspector_page_client() const
  30. {
  31. return global_object().browsing_context()->page().client();
  32. }
  33. void Inspector::inspector_loaded()
  34. {
  35. inspector_page_client().inspector_did_load();
  36. }
  37. void Inspector::inspect_dom_node(i32 node_id, Optional<i32> const& pseudo_element)
  38. {
  39. auto& page = global_object().browsing_context()->page();
  40. page.client().inspector_did_select_dom_node(node_id, pseudo_element.map([](auto value) {
  41. VERIFY(value < to_underlying(Web::CSS::Selector::PseudoElement::Type::KnownPseudoElementCount));
  42. return static_cast<Web::CSS::Selector::PseudoElement::Type>(value);
  43. }));
  44. }
  45. void Inspector::set_dom_node_text(i32 node_id, String const& text)
  46. {
  47. inspector_page_client().inspector_did_set_dom_node_text(node_id, text);
  48. }
  49. void Inspector::set_dom_node_tag(i32 node_id, String const& tag)
  50. {
  51. inspector_page_client().inspector_did_set_dom_node_tag(node_id, tag);
  52. }
  53. void Inspector::add_dom_node_attributes(i32 node_id, JS::NonnullGCPtr<DOM::NamedNodeMap> attributes)
  54. {
  55. inspector_page_client().inspector_did_add_dom_node_attributes(node_id, attributes);
  56. }
  57. void Inspector::replace_dom_node_attribute(i32 node_id, WebIDL::UnsignedLongLong attribute_index, JS::NonnullGCPtr<DOM::NamedNodeMap> replacement_attributes)
  58. {
  59. inspector_page_client().inspector_did_replace_dom_node_attribute(node_id, static_cast<size_t>(attribute_index), replacement_attributes);
  60. }
  61. void Inspector::request_dom_tree_context_menu(i32 node_id, i32 client_x, i32 client_y, String const& type, Optional<String> const& tag, Optional<WebIDL::UnsignedLongLong> const& attribute_index)
  62. {
  63. 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); }));
  64. }
  65. void Inspector::request_cookie_context_menu(WebIDL::UnsignedLongLong cookie_index, i32 client_x, i32 client_y)
  66. {
  67. inspector_page_client().inspector_did_request_cookie_context_menu(cookie_index, { client_x, client_y });
  68. }
  69. void Inspector::request_style_sheet_source(String const& type_string, Optional<i32> const& dom_node_unique_id, Optional<String> const& url)
  70. {
  71. auto type = CSS::style_sheet_identifier_type_from_string(type_string);
  72. VERIFY(type.has_value());
  73. inspector_page_client().inspector_did_request_style_sheet_source({
  74. .type = type.value(),
  75. .dom_element_unique_id = dom_node_unique_id,
  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. }