mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 00:37:35 +00:00
WebContent: Implement $0
special variable in Browser JS Console
`$0` is a helpful variable in other browsers' JS consoles, which points to whichever DOM Node is currently selected in the DOM Inspector. And now we have it too! :^)
This commit is contained in:
parent
7838eab341
commit
5220d6d2e5
2 changed files with 35 additions and 0 deletions
|
@ -5,6 +5,8 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "ConsoleGlobalObject.h"
|
#include "ConsoleGlobalObject.h"
|
||||||
|
#include <LibWeb/Bindings/NodeWrapper.h>
|
||||||
|
#include <LibWeb/Bindings/NodeWrapperFactory.h>
|
||||||
#include <LibWeb/Bindings/WindowObject.h>
|
#include <LibWeb/Bindings/WindowObject.h>
|
||||||
#include <LibWeb/DOM/Document.h>
|
#include <LibWeb/DOM/Document.h>
|
||||||
#include <LibWeb/DOM/Window.h>
|
#include <LibWeb/DOM/Window.h>
|
||||||
|
@ -20,6 +22,14 @@ ConsoleGlobalObject::~ConsoleGlobalObject()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ConsoleGlobalObject::initialize_global_object()
|
||||||
|
{
|
||||||
|
Base::initialize_global_object();
|
||||||
|
|
||||||
|
// $0 magic variable
|
||||||
|
define_native_accessor("$0", inspected_node_getter, nullptr, 0);
|
||||||
|
}
|
||||||
|
|
||||||
void ConsoleGlobalObject::visit_edges(Visitor& visitor)
|
void ConsoleGlobalObject::visit_edges(Visitor& visitor)
|
||||||
{
|
{
|
||||||
Base::visit_edges(visitor);
|
Base::visit_edges(visitor);
|
||||||
|
@ -87,4 +97,24 @@ JS::MarkedValueList ConsoleGlobalObject::internal_own_property_keys() const
|
||||||
return m_window_object->internal_own_property_keys();
|
return m_window_object->internal_own_property_keys();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
JS_DEFINE_NATIVE_GETTER(ConsoleGlobalObject::inspected_node_getter)
|
||||||
|
{
|
||||||
|
auto* this_object = vm.this_value(global_object).to_object(global_object);
|
||||||
|
if (!this_object)
|
||||||
|
return JS::js_null();
|
||||||
|
|
||||||
|
if (!is<ConsoleGlobalObject>(this_object)) {
|
||||||
|
vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotA, "ConsoleGlobalObject");
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
auto console_global_object = static_cast<ConsoleGlobalObject*>(this_object);
|
||||||
|
auto& window = console_global_object->m_window_object->impl();
|
||||||
|
auto* inspected_node = window.document().inspected_node();
|
||||||
|
if (!inspected_node)
|
||||||
|
return JS::js_undefined();
|
||||||
|
|
||||||
|
return Web::Bindings::wrap(global_object, *inspected_node);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,9 +34,14 @@ public:
|
||||||
virtual bool internal_delete(JS::PropertyName const& name) override;
|
virtual bool internal_delete(JS::PropertyName const& name) override;
|
||||||
virtual JS::MarkedValueList internal_own_property_keys() const override;
|
virtual JS::MarkedValueList internal_own_property_keys() const override;
|
||||||
|
|
||||||
|
virtual void initialize_global_object() override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
virtual void visit_edges(Visitor&) override;
|
virtual void visit_edges(Visitor&) override;
|
||||||
|
|
||||||
|
// Because $0 is not a nice C++ function name
|
||||||
|
JS_DECLARE_NATIVE_GETTER(inspected_node_getter);
|
||||||
|
|
||||||
Web::Bindings::WindowObject* m_window_object;
|
Web::Bindings::WindowObject* m_window_object;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue