@@ -63,6 +63,12 @@ export function withJavascriptInterface<Props extends WebViewProps>(
6363 private provider : InterfaceProvider ;
6464 private composer : WebScriptComposer ;
6565 private isCurrentURLInWhitelist : boolean ;
66+ // to workaround the issue that on some Android device load script on page start
67+ // may get cleared after page starting load, we try to inject js on both load start
68+ // and first progress event
69+ // https://github.com/react-native-webview/react-native-webview/pull/1099
70+ // https://github.com/react-native-webview/react-native-webview/issues/1609
71+ private isJSInjectedInProgress : boolean ;
6672 private currentURL : string ;
6773 private webView : WebView ;
6874
@@ -76,6 +82,7 @@ export function withJavascriptInterface<Props extends WebViewProps>(
7682
7783 this . currentURL = '' ;
7884 this . isCurrentURLInWhitelist = true ;
85+ this . isJSInjectedInProgress = true ;
7986 this . onURLUpdated ( props . source ?. uri ) ;
8087 const keys = Object . keys ( rootObj ) ;
8188 this . provider = new InterfaceProvider ( name , rootObj , keys , this . isEnabled , logger ) ;
@@ -125,17 +132,24 @@ export function withJavascriptInterface<Props extends WebViewProps>(
125132 } ;
126133
127134 onLoadStart = ( event : Parameters < WebViewProps [ 'onLoadStart' ] > [ 0 ] ) => {
128- if ( Platform . OS === 'android' ) {
129- // to workaround the issue that load script on some Android device may have issue
130- // https://github.com/react-native-webview/react-native-webview/pull/1099
131- // https://github.com/react-native-webview/react-native-webview/issues/1609
132- this . webView . injectJavaScript ( this . composer . getScriptToInject ( ) ) ;
133- }
135+ const { url } = event . nativeEvent ;
136+ this . onURLUpdated ( url ) ;
137+ this . injectJavascriptForAndroid ( ) ;
138+ this . isJSInjectedInProgress = false ;
134139
135140 // delegate to event handler
136141 this . props . onLoadStart ?.( event ) ;
137142 } ;
138143
144+ onLoadProgress = ( event : Parameters < WebViewProps [ 'onLoadProgress' ] > [ 0 ] ) => {
145+ if ( ! this . isJSInjectedInProgress ) {
146+ this . isJSInjectedInProgress = true ;
147+ this . injectJavascriptForAndroid ( ) ;
148+ }
149+ // delegate to event handler
150+ this . props . onLoadProgress ?.( event ) ;
151+ } ;
152+
139153 private onURLUpdated ( url : string ) : void {
140154 if ( url === this . currentURL ) {
141155 return ;
@@ -152,6 +166,13 @@ export function withJavascriptInterface<Props extends WebViewProps>(
152166 }
153167 }
154168
169+ private injectJavascriptForAndroid ( ) : void {
170+ if ( Platform . OS !== 'android' || ! this . isCurrentURLInWhitelist || ! this . webView ) {
171+ return ;
172+ }
173+ this . webView . injectJavaScript ( this . composer . getScriptToInject ( ) ) ;
174+ }
175+
155176 render ( ) {
156177 return (
157178 < WrappedComponent
@@ -160,6 +181,7 @@ export function withJavascriptInterface<Props extends WebViewProps>(
160181 onMessage = { this . onMessage }
161182 onNavigationStateChange = { this . onNavigationStateChange }
162183 onLoadStart = { this . onLoadStart }
184+ onLoadProgress = { this . onLoadProgress }
163185 injectedJavaScriptBeforeContentLoaded = { this . composer . getScriptToInject (
164186 this . props . injectedJavaScriptBeforeContentLoaded ,
165187 ) }
0 commit comments