| 
									
										
										
										
											2023-11-23 12:22:23 -05:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2024-08-19 12:11:39 -04:00
										 |  |  |  * Copyright (c) 2023-2024, Tim Flynn <trflynn89@serenityos.org> | 
					
						
							| 
									
										
										
										
											2023-11-23 12:22:23 -05:00
										 |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <LibJS/Runtime/Realm.h>
 | 
					
						
							|  |  |  | #include <LibJS/Runtime/VM.h>
 | 
					
						
							|  |  |  | #include <LibWeb/Bindings/InspectorPrototype.h>
 | 
					
						
							|  |  |  | #include <LibWeb/Bindings/Intrinsics.h>
 | 
					
						
							|  |  |  | #include <LibWeb/CSS/Selector.h>
 | 
					
						
							| 
									
										
										
										
											2023-12-03 11:49:08 -05:00
										 |  |  | #include <LibWeb/DOM/NamedNodeMap.h>
 | 
					
						
							| 
									
										
										
										
											2023-11-23 12:22:23 -05:00
										 |  |  | #include <LibWeb/HTML/BrowsingContext.h>
 | 
					
						
							|  |  |  | #include <LibWeb/HTML/Window.h>
 | 
					
						
							|  |  |  | #include <LibWeb/Internals/Inspector.h>
 | 
					
						
							|  |  |  | #include <LibWeb/Page/Page.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Web::Internals { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | JS_DEFINE_ALLOCATOR(Inspector); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Inspector::Inspector(JS::Realm& realm) | 
					
						
							|  |  |  |     : Bindings::PlatformObject(realm) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Inspector::~Inspector() = default; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Inspector::initialize(JS::Realm& realm) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Base::initialize(realm); | 
					
						
							| 
									
										
										
										
											2024-03-16 13:13:08 +01:00
										 |  |  |     WEB_SET_PROTOTYPE_FOR_INTERFACE(Inspector); | 
					
						
							| 
									
										
										
										
											2023-11-23 12:22:23 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-22 10:16:23 +01:00
										 |  |  | PageClient& Inspector::inspector_page_client() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return global_object().browsing_context()->page().client(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-23 12:22:23 -05:00
										 |  |  | void Inspector::inspector_loaded() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2024-08-22 10:16:23 +01:00
										 |  |  |     inspector_page_client().inspector_did_load(); | 
					
						
							| 
									
										
										
										
											2023-11-23 12:22:23 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Inspector::inspect_dom_node(i32 node_id, Optional<i32> const& pseudo_element) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-12-03 20:29:37 +13:00
										 |  |  |     auto& page = global_object().browsing_context()->page(); | 
					
						
							|  |  |  |     page.client().inspector_did_select_dom_node(node_id, pseudo_element.map([](auto value) { | 
					
						
							| 
									
										
										
										
											2023-12-10 22:06:55 +13:00
										 |  |  |         VERIFY(value < to_underlying(Web::CSS::Selector::PseudoElement::Type::KnownPseudoElementCount)); | 
					
						
							| 
									
										
										
										
											2023-12-10 21:00:03 +13:00
										 |  |  |         return static_cast<Web::CSS::Selector::PseudoElement::Type>(value); | 
					
						
							| 
									
										
										
										
											2023-12-03 20:29:37 +13:00
										 |  |  |     })); | 
					
						
							| 
									
										
										
										
											2023-11-23 12:22:23 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-03 11:49:08 -05:00
										 |  |  | void Inspector::set_dom_node_text(i32 node_id, String const& text) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2024-08-22 10:16:23 +01:00
										 |  |  |     inspector_page_client().inspector_did_set_dom_node_text(node_id, text); | 
					
						
							| 
									
										
										
										
											2023-12-03 11:49:08 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Inspector::set_dom_node_tag(i32 node_id, String const& tag) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2024-08-22 10:16:23 +01:00
										 |  |  |     inspector_page_client().inspector_did_set_dom_node_tag(node_id, tag); | 
					
						
							| 
									
										
										
										
											2023-12-03 11:49:08 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-05 16:43:14 -05:00
										 |  |  | void Inspector::add_dom_node_attributes(i32 node_id, JS::NonnullGCPtr<DOM::NamedNodeMap> attributes) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2024-08-22 10:16:23 +01:00
										 |  |  |     inspector_page_client().inspector_did_add_dom_node_attributes(node_id, attributes); | 
					
						
							| 
									
										
										
										
											2023-12-05 16:43:14 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-18 13:59:28 -05:00
										 |  |  | void Inspector::replace_dom_node_attribute(i32 node_id, WebIDL::UnsignedLongLong attribute_index, JS::NonnullGCPtr<DOM::NamedNodeMap> replacement_attributes) | 
					
						
							| 
									
										
										
										
											2023-12-03 11:49:08 -05:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2024-08-22 10:16:23 +01:00
										 |  |  |     inspector_page_client().inspector_did_replace_dom_node_attribute(node_id, static_cast<size_t>(attribute_index), replacement_attributes); | 
					
						
							| 
									
										
										
										
											2023-12-03 11:49:08 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-18 13:59:28 -05:00
										 |  |  | 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) | 
					
						
							| 
									
										
										
										
											2023-12-05 14:47:56 -05:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2024-08-22 10:16:23 +01:00
										 |  |  |     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); })); | 
					
						
							| 
									
										
										
										
											2023-12-05 14:47:56 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-30 19:27:32 -05:00
										 |  |  | void Inspector::execute_console_script(String const& script) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2024-08-22 10:16:23 +01:00
										 |  |  |     inspector_page_client().inspector_did_execute_console_script(script); | 
					
						
							| 
									
										
										
										
											2023-11-30 19:27:32 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-19 12:11:39 -04:00
										 |  |  | void Inspector::export_inspector_html(String const& html) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2024-08-22 10:16:23 +01:00
										 |  |  |     inspector_page_client().inspector_did_export_inspector_html(html); | 
					
						
							| 
									
										
										
										
											2024-08-19 12:11:39 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-23 12:22:23 -05:00
										 |  |  | } |