@@ -357,15 +357,19 @@ pub struct WebViewAttributes<'a> {
357
357
/// - **Windows:** the string can not be larger than 2 MB (2 * 1024 * 1024 bytes) in total size
358
358
pub html : Option < String > ,
359
359
360
- /// Initialize javascript code when loading new pages. When webview load a new page, this
361
- /// initialization code will be executed. It is guaranteed that code is executed before
362
- /// `window.onload`.
360
+ /// A list of initialization javascript scripts to run when loading new pages.
361
+ /// When webview load a new page, this initialization code will be executed.
362
+ /// It is guaranteed that code is executed before `window.onload`.
363
+ ///
364
+ /// Second parameter represents if script should be added to main frame only or sub frames also.
365
+ /// `true` for main frame only, `false` for sub frames.
363
366
///
364
367
/// ## Platform-specific
365
368
///
369
+ /// - **Windows**: scripts are injected into sub frames.
366
370
/// - **Android:** The Android WebView does not provide an API for initialization scripts,
367
371
/// so we prepend them to each HTML head. They are only implemented on custom protocol URLs.
368
- pub initialization_scripts : Vec < String > ,
372
+ pub initialization_scripts : Vec < ( String , bool ) > ,
369
373
370
374
/// A list of custom loading protocols with pairs of scheme uri string and a handling
371
375
/// closure.
@@ -693,16 +697,28 @@ impl<'a> WebViewBuilder<'a> {
693
697
///
694
698
/// ## Platform-specific
695
699
///
700
+ /// - **Windows:** scripts are added to subframes as well.
696
701
/// - **Android:** When [addDocumentStartJavaScript] is not supported,
697
702
/// we prepend them to each HTML head (implementation only supported on custom protocol URLs).
698
703
/// For remote URLs, we use [onPageStarted] which is not guaranteed to run before other scripts.
699
704
///
700
705
/// [addDocumentStartJavaScript]: https://developer.android.com/reference/androidx/webkit/WebViewCompat#addDocumentStartJavaScript(android.webkit.WebView,java.lang.String,java.util.Set%3Cjava.lang.String%3E)
701
706
/// [onPageStarted]: https://developer.android.com/reference/android/webkit/WebViewClient#onPageStarted(android.webkit.WebView,%20java.lang.String,%20android.graphics.Bitmap)
702
707
pub fn with_initialization_script ( self , js : & str ) -> Self {
708
+ self . with_initialization_script_for_main_only ( js, true )
709
+ }
710
+
711
+ /// Same as [`with_initialization_script`](Self::with_initialization_script) but with option to inject into main frame only or sub frames.
712
+ ///
713
+ /// ## Platform-specific:
714
+ ///
715
+ /// - **Windows:** scripts are always added to subframes regardless of the option.
716
+ pub fn with_initialization_script_for_main_only ( self , js : & str , main_only : bool ) -> Self {
703
717
self . and_then ( |mut b| {
704
718
if !js. is_empty ( ) {
705
- b. attrs . initialization_scripts . push ( js. to_string ( ) ) ;
719
+ b. attrs
720
+ . initialization_scripts
721
+ . push ( ( js. to_string ( ) , main_only) ) ;
706
722
}
707
723
Ok ( b)
708
724
} )
0 commit comments