@@ -1475,6 +1475,15 @@ pub enum WebviewMessage {
14751475 EvaluateScript ( String ) ,
14761476 #[ cfg( all( feature = "tracing" , not( target_os = "android" ) ) ) ]
14771477 EvaluateScript ( String , Sender < ( ) > , tracing:: Span ) ,
1478+ #[ cfg( not( all( feature = "tracing" , not( target_os = "android" ) ) ) ) ]
1479+ EvaluateScriptWithCallback ( String , Box < dyn Fn ( String ) + Send + ' static > ) ,
1480+ #[ cfg( all( feature = "tracing" , not( target_os = "android" ) ) ) ]
1481+ EvaluateScriptWithCallback (
1482+ String ,
1483+ Box < dyn Fn ( String ) + Send + ' static > ,
1484+ Sender < ( ) > ,
1485+ tracing:: Span ,
1486+ ) ,
14781487 CookiesForUrl ( Url , Sender < Result < Vec < tauri_runtime:: Cookie < ' static > > > > ) ,
14791488 Cookies ( Sender < Result < Vec < tauri_runtime:: Cookie < ' static > > > > ) ,
14801489 SetCookie ( tauri_runtime:: Cookie < ' static > ) ,
@@ -1830,6 +1839,46 @@ impl<T: UserEvent> WebviewDispatch<T> for WryWebviewDispatcher<T> {
18301839 )
18311840 }
18321841
1842+ #[ cfg( all( feature = "tracing" , not( target_os = "android" ) ) ) ]
1843+ fn eval_script_with_callback < S : Into < String > > (
1844+ & self ,
1845+ script : S ,
1846+ callback : impl Fn ( String ) + Send + ' static ,
1847+ ) -> Result < ( ) > {
1848+ // use a channel so the EvaluateScript task uses the current span as parent
1849+ let ( tx, rx) = channel ( ) ;
1850+ getter ! (
1851+ self ,
1852+ rx,
1853+ Message :: Webview (
1854+ * self . window_id. lock( ) . unwrap( ) ,
1855+ self . webview_id,
1856+ WebviewMessage :: EvaluateScriptWithCallback (
1857+ script. into( ) ,
1858+ Box :: new( callback) ,
1859+ tx,
1860+ tracing:: Span :: current( ) ,
1861+ ) ,
1862+ )
1863+ )
1864+ }
1865+
1866+ #[ cfg( not( all( feature = "tracing" , not( target_os = "android" ) ) ) ) ]
1867+ fn eval_script_with_callback < S : Into < String > > (
1868+ & self ,
1869+ script : S ,
1870+ callback : impl Fn ( String ) + Send + ' static ,
1871+ ) -> Result < ( ) > {
1872+ send_user_message (
1873+ & self . context ,
1874+ Message :: Webview (
1875+ * self . window_id . lock ( ) . unwrap ( ) ,
1876+ self . webview_id ,
1877+ WebviewMessage :: EvaluateScriptWithCallback ( script. into ( ) , Box :: new ( callback) ) ,
1878+ ) ,
1879+ )
1880+ }
1881+
18331882 fn set_zoom ( & self , scale_factor : f64 ) -> Result < ( ) > {
18341883 send_user_message (
18351884 & self . context ,
@@ -3712,6 +3761,20 @@ fn handle_user_message<T: UserEvent>(
37123761 log:: error!( "{e}" ) ;
37133762 }
37143763 }
3764+ #[ cfg( all( feature = "tracing" , not( target_os = "android" ) ) ) ]
3765+ WebviewMessage :: EvaluateScriptWithCallback ( script, callback, tx, span) => {
3766+ let _span = span. entered ( ) ;
3767+ if let Err ( e) = webview. evaluate_script_with_callback ( & script, callback) {
3768+ log:: error!( "{e}" ) ;
3769+ }
3770+ tx. send ( ( ) ) . unwrap ( ) ;
3771+ }
3772+ #[ cfg( not( all( feature = "tracing" , not( target_os = "android" ) ) ) ) ]
3773+ WebviewMessage :: EvaluateScriptWithCallback ( script, callback) => {
3774+ if let Err ( e) = webview. evaluate_script_with_callback ( & script, callback) {
3775+ log:: error!( "{e}" ) ;
3776+ }
3777+ }
37153778 WebviewMessage :: Navigate ( url) => {
37163779 if let Err ( e) = webview. load_url ( url. as_str ( ) ) {
37173780 log:: error!( "failed to navigate to url {}: {}" , url, e) ;
0 commit comments