Skip to content

Commit

Permalink
WebContent+Friends: Add get_element_property IPC and plumbing
Browse files Browse the repository at this point in the history
  • Loading branch information
TobyAsE authored and linusg committed Oct 19, 2022
1 parent 6641c99 commit 3db9288
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Userland/Applications/Browser/BrowserWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions Userland/Applications/Browser/Tab.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class Tab final : public GUI::Widget {
Function<Optional<i32>()> on_get_document_element;
Function<Optional<Vector<i32>>(i32 start_node_id, String const&)> on_query_selector_all;
Function<Optional<String>(i32 element_id, String const&)> on_get_element_attribute;
Function<Optional<String>(i32 element_id, String const&)> on_get_element_property;

enum class InspectorTarget {
Document,
Expand Down
5 changes: 5 additions & 0 deletions Userland/Libraries/LibWebView/OutOfProcessWebView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,11 @@ Optional<String> OutOfProcessWebView::get_element_attribute(i32 element_id, Stri
return client().get_element_attribute(element_id, name);
}

Optional<String> OutOfProcessWebView::get_element_property(i32 element_id, String const& name)
{
return client().get_element_property(element_id, name);
}

void OutOfProcessWebView::set_content_filters(Vector<String> filters)
{
client().async_set_content_filters(filters);
Expand Down
1 change: 1 addition & 0 deletions Userland/Libraries/LibWebView/OutOfProcessWebView.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class OutOfProcessWebView final
Optional<i32> get_document_element();
Optional<Vector<i32>> query_selector_all(i32 start_node_id, String const& selector);
Optional<String> get_element_attribute(i32 element_id, String const& name);
Optional<String> get_element_property(i32 element_id, String const& name);

void set_content_filters(Vector<String>);
void set_proxy_mappings(Vector<String> proxies, HashMap<String, size_t> mappings);
Expand Down
27 changes: 27 additions & 0 deletions Userland/Services/WebContent/ConnectionFromClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {};

if (!node->is_element())
return Optional<String> {};

auto& element = verify_cast<Web::DOM::Element>(*node);

auto property_or_error = element.get(name);
if (property_or_error.is_throw_completion())
return Optional<String> {};

auto property = property_or_error.release_value();

if (property.is_undefined())
return Optional<String> {};

auto string_or_error = property.to_string(element.vm());
if (string_or_error.is_error())
return Optional<String> {};

return { string_or_error.release_value() };
}

Messages::WebContentServer::GetSelectedTextResponse ConnectionFromClient::get_selected_text()
{
return page().focused_context().selected_text();
Expand Down
1 change: 1 addition & 0 deletions Userland/Services/WebContent/ConnectionFromClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class ConnectionFromClient final
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;
Expand Down
1 change: 1 addition & 0 deletions Userland/Services/WebContent/WebContentServer.ipc
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ endpoint WebContentServer
get_document_element() => (Optional<i32> node_id)
query_selector_all(i32 start_node_id, String selector) => (Optional<Vector<i32>> elements_ids)
get_element_attribute(i32 element_id, String name) => (Optional<String> attribute)
get_element_property(i32 element_id, String name) => (Optional<String> property)

run_javascript(String js_source) =|

Expand Down

0 comments on commit 3db9288

Please sign in to comment.