From fd922cf92fc68624fda5de037274e112decb2640 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Tue, 24 Aug 2021 16:56:42 +0200 Subject: [PATCH] LibWeb+WebContent: Add a simple API for running arbitrary JavaScript This patch adds OutOfProcessWebView::run_javascript(StringView). This can be used by the OOPWV embedder to execute arbitrary JavaScript in the top-level browsing context on the WebContent process side. --- .../Libraries/LibWeb/OutOfProcessWebView.cpp | 5 +++++ Userland/Libraries/LibWeb/OutOfProcessWebView.h | 2 ++ .../Services/WebContent/ClientConnection.cpp | 17 +++++++++++++++++ Userland/Services/WebContent/ClientConnection.h | 1 + .../Services/WebContent/WebContentServer.ipc | 2 ++ 5 files changed, 27 insertions(+) diff --git a/Userland/Libraries/LibWeb/OutOfProcessWebView.cpp b/Userland/Libraries/LibWeb/OutOfProcessWebView.cpp index 8485992626..c260ad2982 100644 --- a/Userland/Libraries/LibWeb/OutOfProcessWebView.cpp +++ b/Userland/Libraries/LibWeb/OutOfProcessWebView.cpp @@ -412,6 +412,11 @@ void OutOfProcessWebView::js_console_input(const String& js_source) client().async_js_console_input(js_source); } +void OutOfProcessWebView::run_javascript(StringView js_source) +{ + client().async_run_javascript(js_source); +} + String OutOfProcessWebView::selected_text() { return client().get_selected_text(); diff --git a/Userland/Libraries/LibWeb/OutOfProcessWebView.h b/Userland/Libraries/LibWeb/OutOfProcessWebView.h index 37d354b478..fb705a006d 100644 --- a/Userland/Libraries/LibWeb/OutOfProcessWebView.h +++ b/Userland/Libraries/LibWeb/OutOfProcessWebView.h @@ -35,6 +35,8 @@ public: void js_console_initialize(); void js_console_input(const String& js_source); + void run_javascript(StringView); + String selected_text(); void select_all(); diff --git a/Userland/Services/WebContent/ClientConnection.cpp b/Userland/Services/WebContent/ClientConnection.cpp index 9d4d5753a0..670329fe88 100644 --- a/Userland/Services/WebContent/ClientConnection.cpp +++ b/Userland/Services/WebContent/ClientConnection.cpp @@ -242,6 +242,23 @@ void ClientConnection::js_console_input(const String& js_source) m_console_client->handle_input(js_source); } +void ClientConnection::run_javascript(String const& js_source) +{ + if (!page().top_level_browsing_context().document()) + return; + + auto& interpreter = page().top_level_browsing_context().document()->interpreter(); + + auto parser = JS::Parser(JS::Lexer(js_source)); + auto program = parser.parse_program(); + interpreter.run(interpreter.global_object(), *program); + + if (interpreter.vm().exception()) { + dbgln("Exception :("); + interpreter.vm().clear_exception(); + } +} + Messages::WebContentServer::GetSelectedTextResponse ClientConnection::get_selected_text() { return page().focused_context().selected_text(); diff --git a/Userland/Services/WebContent/ClientConnection.h b/Userland/Services/WebContent/ClientConnection.h index 0e6d59c437..64c2404ad2 100644 --- a/Userland/Services/WebContent/ClientConnection.h +++ b/Userland/Services/WebContent/ClientConnection.h @@ -51,6 +51,7 @@ private: virtual void inspect_dom_tree() override; virtual void js_console_initialize() override; virtual void js_console_input(String const&) override; + virtual void run_javascript(String const&) override; virtual Messages::WebContentServer::GetSelectedTextResponse get_selected_text() override; virtual void select_all() override; diff --git a/Userland/Services/WebContent/WebContentServer.ipc b/Userland/Services/WebContent/WebContentServer.ipc index 158f84364b..39db7ff713 100644 --- a/Userland/Services/WebContent/WebContentServer.ipc +++ b/Userland/Services/WebContent/WebContentServer.ipc @@ -30,6 +30,8 @@ endpoint WebContentServer js_console_initialize() =| js_console_input(String js_source) =| + run_javascript(String js_source) =| + get_selected_text() => (String selection) select_all() =| }