Browse Source

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.
Andreas Kling 3 years ago
parent
commit
fd922cf92f

+ 5 - 0
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();

+ 2 - 0
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();
 

+ 17 - 0
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();

+ 1 - 0
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;
 

+ 2 - 0
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() =|
 }