Skip to content

Commit ef45b50

Browse files
committed
prevent webview from navigating (#11)
1 parent 24730ec commit ef45b50

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

src/YoutubeIframe.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import React, {
88
} from 'react';
99
import {View, StyleSheet, Platform} from 'react-native';
1010
import WebView from 'react-native-webview';
11-
import {PLAYER_STATES, PLAYER_ERROR} from './constants';
11+
import {PLAYER_STATES, PLAYER_ERROR, CUSTOM_USER_AGENT} from './constants';
1212
import {EventEmitter} from 'events';
1313
import {MAIN_SCRIPT, PLAYER_FUNCTIONS} from './PlayerScripts';
1414

@@ -173,12 +173,11 @@ const YoutubeIframe = (
173173
allowsFullscreenVideo={!initialPlayerParams?.preventFullScreen}
174174
source={{html: MAIN_SCRIPT(videoId, playList, initialPlayerParams)}}
175175
userAgent={
176-
forceAndroidAutoplay &&
177-
Platform.select({
178-
android:
179-
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36',
180-
})
176+
forceAndroidAutoplay && Platform.select({android: CUSTOM_USER_AGENT})
181177
}
178+
onShouldStartLoadWithRequest={request => {
179+
return request.mainDocumentURL === 'about:blank';
180+
}}
182181
{...webViewProps}
183182
/>
184183
</View>

src/constants.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
export const PLAYER_STATES = {
2-
"-1": "unstarted",
3-
"0": "ended",
4-
"1": "playing",
5-
"2": "paused",
6-
"3": "buffering",
7-
"5": "video cued"
2+
'-1': 'unstarted',
3+
'0': 'ended',
4+
'1': 'playing',
5+
'2': 'paused',
6+
'3': 'buffering',
7+
'5': 'video cued',
88
};
99

1010
export const PLAYER_ERROR = {
11-
"2": "invalid_parameter",
12-
"5": "HTML5_error",
13-
"100": "video_not_found",
14-
"101": "embed_not_allowed",
15-
"150": "embed_not_allowed"
11+
'2': 'invalid_parameter',
12+
'5': 'HTML5_error',
13+
'100': 'video_not_found',
14+
'101': 'embed_not_allowed',
15+
'150': 'embed_not_allowed',
1616
};
17+
18+
export const CUSTOM_USER_AGENT =
19+
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36';

0 commit comments

Comments
 (0)