mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 22:02:44 +00:00 
			
		
		
		
	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.
This commit is contained in:
		
							parent
							
								
									6b2aadce11
								
							
						
					
					
						commit
						fd922cf92f
					
				
					 5 changed files with 27 additions and 0 deletions
				
			
		|  | @ -412,6 +412,11 @@ void OutOfProcessWebView::js_console_input(const String& js_source) | ||||||
|     client().async_js_console_input(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() | String OutOfProcessWebView::selected_text() | ||||||
| { | { | ||||||
|     return client().get_selected_text(); |     return client().get_selected_text(); | ||||||
|  |  | ||||||
|  | @ -35,6 +35,8 @@ public: | ||||||
|     void js_console_initialize(); |     void js_console_initialize(); | ||||||
|     void js_console_input(const String& js_source); |     void js_console_input(const String& js_source); | ||||||
| 
 | 
 | ||||||
|  |     void run_javascript(StringView); | ||||||
|  | 
 | ||||||
|     String selected_text(); |     String selected_text(); | ||||||
|     void select_all(); |     void select_all(); | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -242,6 +242,23 @@ void ClientConnection::js_console_input(const String& js_source) | ||||||
|         m_console_client->handle_input(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() | Messages::WebContentServer::GetSelectedTextResponse ClientConnection::get_selected_text() | ||||||
| { | { | ||||||
|     return page().focused_context().selected_text(); |     return page().focused_context().selected_text(); | ||||||
|  |  | ||||||
|  | @ -51,6 +51,7 @@ private: | ||||||
|     virtual void inspect_dom_tree() override; |     virtual void inspect_dom_tree() override; | ||||||
|     virtual void js_console_initialize() override; |     virtual void js_console_initialize() override; | ||||||
|     virtual void js_console_input(String const&) 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 Messages::WebContentServer::GetSelectedTextResponse get_selected_text() override; | ||||||
|     virtual void select_all() override; |     virtual void select_all() override; | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -30,6 +30,8 @@ endpoint WebContentServer | ||||||
|     js_console_initialize() =| |     js_console_initialize() =| | ||||||
|     js_console_input(String js_source) =| |     js_console_input(String js_source) =| | ||||||
| 
 | 
 | ||||||
|  |     run_javascript(String js_source) =| | ||||||
|  | 
 | ||||||
|     get_selected_text() => (String selection) |     get_selected_text() => (String selection) | ||||||
|     select_all() =| |     select_all() =| | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Andreas Kling
						Andreas Kling