Skip to content

Commit fd922cf

Browse files
committed
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.
1 parent 6b2aadc commit fd922cf

File tree

5 files changed

+27
-0
lines changed

5 files changed

+27
-0
lines changed

Userland/Libraries/LibWeb/OutOfProcessWebView.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,11 @@ void OutOfProcessWebView::js_console_input(const String& js_source)
412412
client().async_js_console_input(js_source);
413413
}
414414

415+
void OutOfProcessWebView::run_javascript(StringView js_source)
416+
{
417+
client().async_run_javascript(js_source);
418+
}
419+
415420
String OutOfProcessWebView::selected_text()
416421
{
417422
return client().get_selected_text();

Userland/Libraries/LibWeb/OutOfProcessWebView.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ class OutOfProcessWebView final
3535
void js_console_initialize();
3636
void js_console_input(const String& js_source);
3737

38+
void run_javascript(StringView);
39+
3840
String selected_text();
3941
void select_all();
4042

Userland/Services/WebContent/ClientConnection.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,23 @@ void ClientConnection::js_console_input(const String& js_source)
242242
m_console_client->handle_input(js_source);
243243
}
244244

245+
void ClientConnection::run_javascript(String const& js_source)
246+
{
247+
if (!page().top_level_browsing_context().document())
248+
return;
249+
250+
auto& interpreter = page().top_level_browsing_context().document()->interpreter();
251+
252+
auto parser = JS::Parser(JS::Lexer(js_source));
253+
auto program = parser.parse_program();
254+
interpreter.run(interpreter.global_object(), *program);
255+
256+
if (interpreter.vm().exception()) {
257+
dbgln("Exception :(");
258+
interpreter.vm().clear_exception();
259+
}
260+
}
261+
245262
Messages::WebContentServer::GetSelectedTextResponse ClientConnection::get_selected_text()
246263
{
247264
return page().focused_context().selected_text();

Userland/Services/WebContent/ClientConnection.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class ClientConnection final
5151
virtual void inspect_dom_tree() override;
5252
virtual void js_console_initialize() override;
5353
virtual void js_console_input(String const&) override;
54+
virtual void run_javascript(String const&) override;
5455
virtual Messages::WebContentServer::GetSelectedTextResponse get_selected_text() override;
5556
virtual void select_all() override;
5657

Userland/Services/WebContent/WebContentServer.ipc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ endpoint WebContentServer
3030
js_console_initialize() =|
3131
js_console_input(String js_source) =|
3232

33+
run_javascript(String js_source) =|
34+
3335
get_selected_text() => (String selection)
3436
select_all() =|
3537
}

0 commit comments

Comments
 (0)