From 3db92885cd7e28a098e268f5f85faae1a139fe20 Mon Sep 17 00:00:00 2001 From: Tobias Christiansen Date: Wed, 19 Oct 2022 20:45:25 +0200 Subject: [PATCH] WebContent+Friends: Add get_element_property IPC and plumbing --- .../Applications/Browser/BrowserWindow.cpp | 4 +++ Userland/Applications/Browser/Tab.h | 1 + .../LibWebView/OutOfProcessWebView.cpp | 5 ++++ .../LibWebView/OutOfProcessWebView.h | 1 + .../WebContent/ConnectionFromClient.cpp | 27 +++++++++++++++++++ .../WebContent/ConnectionFromClient.h | 1 + .../Services/WebContent/WebContentServer.ipc | 1 + 7 files changed, 40 insertions(+) diff --git a/Userland/Applications/Browser/BrowserWindow.cpp b/Userland/Applications/Browser/BrowserWindow.cpp index c6dd766936..3c9e851652 100644 --- a/Userland/Applications/Browser/BrowserWindow.cpp +++ b/Userland/Applications/Browser/BrowserWindow.cpp @@ -594,6 +594,10 @@ void BrowserWindow::create_new_tab(URL url, bool activate) return active_tab().view().get_element_attribute(element_id, name); }; + new_tab.on_get_element_property = [this](i32 element_id, String const& name) { + return active_tab().view().get_element_property(element_id, name); + }; + new_tab.load(url); dbgln_if(SPAM_DEBUG, "Added new tab {:p}, loading {}", &new_tab, url); diff --git a/Userland/Applications/Browser/Tab.h b/Userland/Applications/Browser/Tab.h index bc3d3f755c..38914b8644 100644 --- a/Userland/Applications/Browser/Tab.h +++ b/Userland/Applications/Browser/Tab.h @@ -71,6 +71,7 @@ public: Function()> on_get_document_element; Function>(i32 start_node_id, String const&)> on_query_selector_all; Function(i32 element_id, String const&)> on_get_element_attribute; + Function(i32 element_id, String const&)> on_get_element_property; enum class InspectorTarget { Document, diff --git a/Userland/Libraries/LibWebView/OutOfProcessWebView.cpp b/Userland/Libraries/LibWebView/OutOfProcessWebView.cpp index 026a3d338b..8c719943a7 100644 --- a/Userland/Libraries/LibWebView/OutOfProcessWebView.cpp +++ b/Userland/Libraries/LibWebView/OutOfProcessWebView.cpp @@ -530,6 +530,11 @@ Optional OutOfProcessWebView::get_element_attribute(i32 element_id, Stri return client().get_element_attribute(element_id, name); } +Optional OutOfProcessWebView::get_element_property(i32 element_id, String const& name) +{ + return client().get_element_property(element_id, name); +} + void OutOfProcessWebView::set_content_filters(Vector filters) { client().async_set_content_filters(filters); diff --git a/Userland/Libraries/LibWebView/OutOfProcessWebView.h b/Userland/Libraries/LibWebView/OutOfProcessWebView.h index d7d325a816..da6846d7e1 100644 --- a/Userland/Libraries/LibWebView/OutOfProcessWebView.h +++ b/Userland/Libraries/LibWebView/OutOfProcessWebView.h @@ -61,6 +61,7 @@ public: Optional get_document_element(); Optional> query_selector_all(i32 start_node_id, String const& selector); Optional get_element_attribute(i32 element_id, String const& name); + Optional get_element_property(i32 element_id, String const& name); void set_content_filters(Vector); void set_proxy_mappings(Vector proxies, HashMap mappings); diff --git a/Userland/Services/WebContent/ConnectionFromClient.cpp b/Userland/Services/WebContent/ConnectionFromClient.cpp index a003cf6207..c2dec08eaf 100644 --- a/Userland/Services/WebContent/ConnectionFromClient.cpp +++ b/Userland/Services/WebContent/ConnectionFromClient.cpp @@ -490,6 +490,33 @@ Messages::WebContentServer::GetElementAttributeResponse ConnectionFromClient::ge return { element.get_attribute(name) }; } +Messages::WebContentServer::GetElementPropertyResponse ConnectionFromClient::get_element_property(i32 element_id, String const& name) +{ + auto* node = Web::DOM::Node::from_id(element_id); + if (!node) + return Optional {}; + + if (!node->is_element()) + return Optional {}; + + auto& element = verify_cast(*node); + + auto property_or_error = element.get(name); + if (property_or_error.is_throw_completion()) + return Optional {}; + + auto property = property_or_error.release_value(); + + if (property.is_undefined()) + return Optional {}; + + auto string_or_error = property.to_string(element.vm()); + if (string_or_error.is_error()) + return Optional {}; + + return { string_or_error.release_value() }; +} + Messages::WebContentServer::GetSelectedTextResponse ConnectionFromClient::get_selected_text() { return page().focused_context().selected_text(); diff --git a/Userland/Services/WebContent/ConnectionFromClient.h b/Userland/Services/WebContent/ConnectionFromClient.h index d342add598..7406a98536 100644 --- a/Userland/Services/WebContent/ConnectionFromClient.h +++ b/Userland/Services/WebContent/ConnectionFromClient.h @@ -82,6 +82,7 @@ private: virtual Messages::WebContentServer::GetDocumentElementResponse get_document_element() override; virtual Messages::WebContentServer::QuerySelectorAllResponse query_selector_all(i32 start_node_id, String const& selector) override; virtual Messages::WebContentServer::GetElementAttributeResponse get_element_attribute(i32 element_id, String const& name) override; + virtual Messages::WebContentServer::GetElementPropertyResponse get_element_property(i32 element_id, String const& name) override; virtual Messages::WebContentServer::GetLocalStorageEntriesResponse get_local_storage_entries() override; virtual Messages::WebContentServer::GetSessionStorageEntriesResponse get_session_storage_entries() override; diff --git a/Userland/Services/WebContent/WebContentServer.ipc b/Userland/Services/WebContent/WebContentServer.ipc index 99be81958d..1f9bd696ff 100644 --- a/Userland/Services/WebContent/WebContentServer.ipc +++ b/Userland/Services/WebContent/WebContentServer.ipc @@ -40,6 +40,7 @@ endpoint WebContentServer get_document_element() => (Optional node_id) query_selector_all(i32 start_node_id, String selector) => (Optional> elements_ids) get_element_attribute(i32 element_id, String name) => (Optional attribute) + get_element_property(i32 element_id, String name) => (Optional property) run_javascript(String js_source) =|