Files
serenity/Userland/Libraries/LibWeb/Internals/Inspector.cpp
Timothy Flynn 335b527f19 LibWeb+LibWebView: Add a button to the Inspector to export its contents
When working on the Inspector's HTML, it's often kind of tricky to debug
when an element is styled / positioned incorrectly. We don't have a way
to inspect the Inspector itself.

This adds a button to the Inspector to export its HTML/CSS/JS contents
to the downloads directory. This allows for more easily testing changes,
especially by opening the exported HTML in another browser's dev tools.

We will ultimately likely remove this button (or make it hidden) by the
time we are production-ready. But it's quite useful for now.

(cherry picked from commit 3ec5c1941f25041c6bf073f2940f6035e084a4e2)
2024-11-08 07:44:45 -05:00

85 lines
2.9 KiB
C++

/*
* Copyright (c) 2023-2024, Tim Flynn <trflynn89@serenityos.org>
*
* 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>
#include <LibWeb/DOM/NamedNodeMap.h>
#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);
WEB_SET_PROTOTYPE_FOR_INTERFACE(Inspector);
}
void Inspector::inspector_loaded()
{
global_object().browsing_context()->page().client().inspector_did_load();
}
void Inspector::inspect_dom_node(i32 node_id, Optional<i32> const& pseudo_element)
{
auto& page = global_object().browsing_context()->page();
page.client().inspector_did_select_dom_node(node_id, pseudo_element.map([](auto value) {
VERIFY(value < to_underlying(Web::CSS::Selector::PseudoElement::Type::KnownPseudoElementCount));
return static_cast<Web::CSS::Selector::PseudoElement::Type>(value);
}));
}
void Inspector::set_dom_node_text(i32 node_id, String const& text)
{
global_object().browsing_context()->page().client().inspector_did_set_dom_node_text(node_id, text);
}
void Inspector::set_dom_node_tag(i32 node_id, String const& tag)
{
global_object().browsing_context()->page().client().inspector_did_set_dom_node_tag(node_id, tag);
}
void Inspector::add_dom_node_attributes(i32 node_id, JS::NonnullGCPtr<DOM::NamedNodeMap> attributes)
{
global_object().browsing_context()->page().client().inspector_did_add_dom_node_attributes(node_id, attributes);
}
void Inspector::replace_dom_node_attribute(i32 node_id, WebIDL::UnsignedLongLong attribute_index, JS::NonnullGCPtr<DOM::NamedNodeMap> replacement_attributes)
{
global_object().browsing_context()->page().client().inspector_did_replace_dom_node_attribute(node_id, static_cast<size_t>(attribute_index), replacement_attributes);
}
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)
{
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); }));
}
void Inspector::execute_console_script(String const& script)
{
global_object().browsing_context()->page().client().inspector_did_execute_console_script(script);
}
void Inspector::export_inspector_html(String const& html)
{
global_object().browsing_context()->page().client().inspector_did_export_inspector_html(html);
}
}