Skip to content

Commit ea4af1b

Browse files
authored
fix: add missing origin parameter for YouTube iframe API security (#31)
- Pass webViewUrl as origin parameter to resolve iframe API restrictions - Fix embed access issues when enablejsapi=1 is used
1 parent 358e706 commit ea4af1b

File tree

5 files changed

+18
-4
lines changed

5 files changed

+18
-4
lines changed

.changeset/true-cobras-appear.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"react-native-youtube-bridge": patch
3+
---
4+
5+
fix: add missing origin parameter for YouTube iframe API security
6+
- Pass webViewUrl as origin parameter to resolve iframe API restrictions
7+
- Fix embed access issues when enablejsapi=1 is used

src/hooks/useYoutubePlayer.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const useYouTubePlayer = (config: YoutubePlayerConfig) => {
2020
onAutoplayBlocked,
2121
} = config;
2222

23-
const { startTime, endTime, autoplay, controls, loop, muted, playsinline, rel } = playerVars;
23+
const { startTime, endTime, autoplay, controls, loop, muted, playsinline, rel, origin } = playerVars;
2424

2525
const cleanup = useCallback(() => {
2626
coreRef.current?.destroy();
@@ -68,6 +68,7 @@ const useYouTubePlayer = (config: YoutubePlayerConfig) => {
6868
rel,
6969
startTime,
7070
endTime,
71+
origin,
7172
},
7273
});
7374
} catch (error) {
@@ -80,7 +81,7 @@ const useYouTubePlayer = (config: YoutubePlayerConfig) => {
8081
};
8182

8283
initialize();
83-
}, [videoId, startTime, endTime, autoplay, controls, loop, muted, playsinline, rel, onError]);
84+
}, [videoId, startTime, endTime, autoplay, controls, loop, muted, playsinline, rel, origin, onError]);
8485

8586
useEffect(() => {
8687
if (isReady && videoId && coreRef.current) {

src/modules/YouTubePlayerCore.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ class YouTubePlayerCore {
100100
end: config.playerVars?.endTime,
101101
playsinline: config.playerVars?.playsinline ? 1 : 0,
102102
rel: config.playerVars?.rel ? 1 : 0,
103+
origin: config.playerVars?.origin,
103104
enablejsapi: 1,
104105
},
105106
events: {

src/utils/youtube.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@ export const getYoutubeWebViewUrl = (
1111
return '';
1212
}
1313

14-
const { startTime, autoplay, controls, loop, muted, playsinline, rel, endTime } = playerVars;
14+
const baseUrl = webViewBaseUrl || DEFAULT_EXTERNAL_WEB_URL;
1515

16-
const url = new URL(webViewBaseUrl || DEFAULT_EXTERNAL_WEB_URL);
16+
const { startTime, autoplay, controls, loop, muted, playsinline, rel, endTime, origin } = playerVars;
17+
18+
const url = new URL(baseUrl);
1719

1820
url.searchParams.set('videoId', videoId);
1921
startTime && url.searchParams.set('startTime', startTime.toString());
2022
endTime && url.searchParams.set('endTime', endTime.toString());
23+
url.searchParams.set('origin', origin || baseUrl);
2124
url.searchParams.set('autoplay', autoplay ? 'true' : 'false');
2225
url.searchParams.set('controls', controls ? 'true' : 'false');
2326
url.searchParams.set('loop', loop ? 'true' : 'false');

web/src/App.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ function App() {
1515
const muted = urlParams.get('muted') === 'true';
1616
const playsinline = urlParams.get('playsinline') === 'true';
1717
const rel = urlParams.get('rel') === 'true';
18+
const origin = urlParams.get('origin') ?? '';
1819

1920
const [progressInterval, setProgressInterval] = useState<number>(0);
2021

@@ -30,6 +31,7 @@ function App() {
3031
videoId: youtubeVideoId,
3132
progressInterval,
3233
playerVars: {
34+
origin,
3335
controls,
3436
autoplay,
3537
muted,

0 commit comments

Comments
 (0)