Inspector.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Copyright (c) 2023, 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/DOM/NamedNodeMap.h>
  12. #include <LibWeb/HTML/BrowsingContext.h>
  13. #include <LibWeb/HTML/Window.h>
  14. #include <LibWeb/Internals/Inspector.h>
  15. #include <LibWeb/Page/Page.h>
  16. namespace Web::Internals {
  17. JS_DEFINE_ALLOCATOR(Inspector);
  18. Inspector::Inspector(JS::Realm& realm)
  19. : Bindings::PlatformObject(realm)
  20. {
  21. }
  22. Inspector::~Inspector() = default;
  23. void Inspector::initialize(JS::Realm& realm)
  24. {
  25. Base::initialize(realm);
  26. WEB_SET_PROTOTYPE_FOR_INTERFACE(Inspector);
  27. }
  28. void Inspector::inspector_loaded()
  29. {
  30. global_object().browsing_context()->page().client().inspector_did_load();
  31. }
  32. void Inspector::inspect_dom_node(i32 node_id, Optional<i32> const& pseudo_element)
  33. {
  34. auto& page = global_object().browsing_context()->page();
  35. page.client().inspector_did_select_dom_node(node_id, pseudo_element.map([](auto value) {
  36. VERIFY(value < to_underlying(Web::CSS::Selector::PseudoElement::Type::KnownPseudoElementCount));
  37. return static_cast<Web::CSS::Selector::PseudoElement::Type>(value);
  38. }));
  39. }
  40. void Inspector::set_dom_node_text(i32 node_id, String const& text)
  41. {
  42. global_object().browsing_context()->page().client().inspector_did_set_dom_node_text(node_id, text);
  43. }
  44. void Inspector::set_dom_node_tag(i32 node_id, String const& tag)
  45. {
  46. global_object().browsing_context()->page().client().inspector_did_set_dom_node_tag(node_id, tag);
  47. }
  48. void Inspector::add_dom_node_attributes(i32 node_id, JS::NonnullGCPtr<DOM::NamedNodeMap> attributes)
  49. {
  50. global_object().browsing_context()->page().client().inspector_did_add_dom_node_attributes(node_id, attributes);
  51. }
  52. void Inspector::replace_dom_node_attribute(i32 node_id, WebIDL::UnsignedLongLong attribute_index, JS::NonnullGCPtr<DOM::NamedNodeMap> replacement_attributes)
  53. {
  54. global_object().browsing_context()->page().client().inspector_did_replace_dom_node_attribute(node_id, static_cast<size_t>(attribute_index), replacement_attributes);
  55. }
  56. 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)
  57. {
  58. global_object().browsing_context()->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); }));
  59. }
  60. void Inspector::execute_console_script(String const& script)
  61. {
  62. global_object().browsing_context()->page().client().inspector_did_execute_console_script(script);
  63. }
  64. }