@@ -9,7 +9,12 @@ import React, {
9
9
} from 'react' ;
10
10
import { View , StyleSheet , Platform } from 'react-native' ;
11
11
import { WebView } from './WebView' ;
12
- import { PLAYER_STATES , PLAYER_ERROR , CUSTOM_USER_AGENT } from './constants' ;
12
+ import {
13
+ PLAYER_ERROR ,
14
+ PLAYER_STATES ,
15
+ DEFAULT_BASE_URL ,
16
+ CUSTOM_USER_AGENT ,
17
+ } from './constants' ;
13
18
import { EventEmitter } from 'events' ;
14
19
import {
15
20
playMode ,
@@ -18,9 +23,6 @@ import {
18
23
PLAYER_FUNCTIONS ,
19
24
} from './PlayerScripts' ;
20
25
21
- const defaultBaseUrl =
22
- 'https://lonelycpp.github.io/react-native-youtube-iframe/iframe.html' ;
23
-
24
26
const YoutubeIframe = ( props , ref ) => {
25
27
const {
26
28
height,
@@ -125,8 +127,9 @@ const YoutubeIframe = (props, ref) => {
125
127
126
128
const onWebMessage = useCallback (
127
129
event => {
128
- const message = JSON . parse ( event . nativeEvent . data ) ;
129
130
try {
131
+ const message = JSON . parse ( event . nativeEvent . data ) ;
132
+
130
133
switch ( message . eventType ) {
131
134
case 'fullScreenChange' :
132
135
onFullScreenChange ( message . data ) ;
@@ -177,6 +180,20 @@ const YoutubeIframe = (props, ref) => {
177
180
] ,
178
181
) ;
179
182
183
+ const onShouldStartLoadWithRequest = useCallback (
184
+ request => {
185
+ try {
186
+ const url = request . mainDocumentURL || request . url ;
187
+ return url . startsWith ( baseUrlOverride || DEFAULT_BASE_URL ) ;
188
+ } catch ( error ) {
189
+ // defaults to true in case of error
190
+ // returning false stops the video from loading
191
+ return true ;
192
+ }
193
+ } ,
194
+ [ baseUrlOverride ] ,
195
+ ) ;
196
+
180
197
const source = useMemo ( ( ) => {
181
198
const ytScript = MAIN_SCRIPT (
182
199
videoId ,
@@ -194,7 +211,7 @@ const YoutubeIframe = (props, ref) => {
194
211
return res ;
195
212
}
196
213
197
- const base = baseUrlOverride || defaultBaseUrl ;
214
+ const base = baseUrlOverride || DEFAULT_BASE_URL ;
198
215
const data = ytScript . urlEncodedJSON ;
199
216
200
217
return { uri : base + '?data=' + data } ;
@@ -211,23 +228,18 @@ const YoutubeIframe = (props, ref) => {
211
228
return (
212
229
< View style = { { height, width} } >
213
230
< WebView
231
+ bounces = { false }
214
232
originWhitelist = { [ '*' ] }
215
233
allowsInlineMediaPlayback
216
234
style = { [ styles . webView , webViewStyle ] }
217
235
mediaPlaybackRequiresUserAction = { false }
236
+ onShouldStartLoadWithRequest = { onShouldStartLoadWithRequest }
218
237
allowsFullscreenVideo = { ! initialPlayerParams ?. preventFullScreen }
219
238
userAgent = {
220
239
forceAndroidAutoplay
221
240
? Platform . select ( { android : CUSTOM_USER_AGENT , ios : '' } )
222
241
: ''
223
242
}
224
- onShouldStartLoadWithRequest = { request => {
225
- return request . mainDocumentURL . startsWith (
226
- baseUrlOverride || defaultBaseUrl ,
227
- ) ;
228
- // return request.mainDocumentURL === 'about:blank';
229
- } }
230
- bounces = { false }
231
243
// props above this are override-able
232
244
233
245
// --
0 commit comments