Skip to content

Commit 3645311

Browse files
authored
fix: prevent YouTube website from loading inside video player (#71)
- YouTube logo clicks now open in external browser instead of WebView - Add default `onShouldStartLoadWithRequest` handler to intercept navigation - Maintain embed video playback while redirecting external YouTube links - core: export `MATCH_URL_YOUTUBE` regex constant
1 parent d02eb41 commit 3645311

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

.changeset/flat-showers-tease.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@react-native-youtube-bridge/core": patch
3+
---
4+
5+
feat: export `MATCH_URL_YOUTUBE` regex constant

.changeset/quick-needles-stay.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"react-native-youtube-bridge": patch
3+
---
4+
5+
fix: prevent YouTube website from loading inside video player
6+
7+
- YouTube logo clicks now open in external browser instead of WebView
8+
- Add default `onShouldStartLoadWithRequest` handler to intercept navigation
9+
- Maintain embed video playback while redirecting external YouTube links

packages/core/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ export {
1515
export type { MessageData } from './types/webview';
1616
export { default as WebYoutubePlayerController } from './WebYoutubePlayerController';
1717
export { escapeHtml, extractVideoIdFromUrl, safeNumber, validateVideoId } from './utils';
18-
export { ERROR_CODES } from './constants';
18+
export { ERROR_CODES, MATCH_URL_YOUTUBE } from './constants';

packages/react-native-youtube-bridge/src/YoutubeView.tsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
2-
import { type DataDetectorTypes, Dimensions, StyleSheet } from 'react-native';
2+
import { type DataDetectorTypes, Dimensions, StyleSheet, Linking } from 'react-native';
33
import WebView, { type WebViewMessageEvent } from 'react-native-webview';
4-
import type { MessageData } from '@react-native-youtube-bridge/core';
4+
import type { ShouldStartLoadRequest } from 'react-native-webview/lib/WebViewTypes';
5+
import { type MessageData, MATCH_URL_YOUTUBE } from '@react-native-youtube-bridge/core';
56

67
import YoutubeViewWrapper from './YoutubeViewWrapper';
78
import useCreateLocalPlayerHtml from './hooks/useCreateLocalPlayerHtml';
@@ -113,6 +114,15 @@ function YoutubeView({
113114
[player],
114115
);
115116

117+
const handleShouldStartLoadWithRequest = useCallback((request: ShouldStartLoadRequest) => {
118+
if (MATCH_URL_YOUTUBE.test(request.url) && !request.url.includes('/embed/')) {
119+
Linking.openURL(request.url);
120+
return false;
121+
}
122+
123+
return true;
124+
}, []);
125+
116126
useEffect(() => {
117127
if (isReady && webViewRef.current) {
118128
const controller = WebviewYoutubePlayerController.createInstance(webViewRef);
@@ -156,6 +166,7 @@ function YoutubeView({
156166
mixedContentMode="compatibility"
157167
thirdPartyCookiesEnabled={false}
158168
webviewDebuggingEnabled={__DEV__}
169+
onShouldStartLoadWithRequest={handleShouldStartLoadWithRequest}
159170
{...webViewProps}
160171
ref={webViewRef}
161172
javaScriptEnabled

0 commit comments

Comments
 (0)