@@ -73,11 +73,15 @@ export const DEFAULT_PLAYER_HEIGHT = 390;
73
73
interface Player extends YT . Player {
74
74
videoId ?: string ;
75
75
playerVars ?: YT . PlayerVars ;
76
+ host ?: string ;
76
77
}
77
78
78
79
// The player isn't fully initialized when it's constructed.
79
80
// The only field available is destroy and addEventListener.
80
- type UninitializedPlayer = Pick < Player , 'videoId' | 'playerVars' | 'destroy' | 'addEventListener' > ;
81
+ type UninitializedPlayer = Pick <
82
+ Player ,
83
+ 'videoId' | 'playerVars' | 'destroy' | 'addEventListener' | 'host'
84
+ > ;
81
85
82
86
/**
83
87
* Object used to store the state of the player if the
@@ -179,6 +183,16 @@ export class YouTubePlayer implements AfterViewInit, OnDestroy, OnInit {
179
183
}
180
184
private _playerVars = new BehaviorSubject < YT . PlayerVars | undefined > ( undefined ) ;
181
185
186
+ /** Whether cookies inside the player have been disabled. */
187
+ @Input ( )
188
+ get disableCookies ( ) : boolean {
189
+ return this . _disableCookies . value ;
190
+ }
191
+ set disableCookies ( value : unknown ) {
192
+ this . _disableCookies . next ( ! ! value ) ;
193
+ }
194
+ private readonly _disableCookies = new BehaviorSubject < boolean > ( false ) ;
195
+
182
196
/**
183
197
* Whether the iframe will attempt to load regardless of the status of the api on the
184
198
* page. Set this to true if you don't want the `onYouTubeIframeAPIReady` field to be
@@ -241,10 +255,15 @@ export class YouTubePlayer implements AfterViewInit, OnDestroy, OnInit {
241
255
iframeApiAvailableObs = iframeApiAvailableSubject . pipe ( take ( 1 ) , startWith ( false ) ) ;
242
256
}
243
257
258
+ const hostObservable = this . _disableCookies . pipe (
259
+ map ( cookiesDisabled => ( cookiesDisabled ? 'https://www.youtube-nocookie.com' : undefined ) ) ,
260
+ ) ;
261
+
244
262
// An observable of the currently loaded player.
245
263
const playerObs = createPlayerObservable (
246
264
this . _youtubeContainer ,
247
265
this . _videoId ,
266
+ hostObservable ,
248
267
iframeApiAvailableObs ,
249
268
this . _width ,
250
269
this . _height ,
@@ -648,18 +667,19 @@ function waitUntilReady(
648
667
function createPlayerObservable (
649
668
youtubeContainer : Observable < HTMLElement > ,
650
669
videoIdObs : Observable < string | undefined > ,
670
+ hostObs : Observable < string | undefined > ,
651
671
iframeApiAvailableObs : Observable < boolean > ,
652
672
widthObs : Observable < number > ,
653
673
heightObs : Observable < number > ,
654
674
playerVarsObs : Observable < YT . PlayerVars | undefined > ,
655
675
ngZone : NgZone ,
656
676
) : Observable < UninitializedPlayer | undefined > {
657
- const playerOptions = combineLatest ( [ videoIdObs , playerVarsObs ] ) . pipe (
677
+ const playerOptions = combineLatest ( [ videoIdObs , hostObs , playerVarsObs ] ) . pipe (
658
678
withLatestFrom ( combineLatest ( [ widthObs , heightObs ] ) ) ,
659
679
map ( ( [ constructorOptions , sizeOptions ] ) => {
660
- const [ videoId , playerVars ] = constructorOptions ;
680
+ const [ videoId , host , playerVars ] = constructorOptions ;
661
681
const [ width , height ] = sizeOptions ;
662
- return videoId ? { videoId, playerVars, width, height} : undefined ;
682
+ return videoId ? { videoId, playerVars, width, height, host } : undefined ;
663
683
} ) ,
664
684
) ;
665
685
@@ -684,7 +704,11 @@ function syncPlayerState(
684
704
player : UninitializedPlayer | undefined ,
685
705
[ container , videoOptions , ngZone ] : [ HTMLElement , YT . PlayerOptions | undefined , NgZone ] ,
686
706
) : UninitializedPlayer | undefined {
687
- if ( player && videoOptions && player . playerVars !== videoOptions . playerVars ) {
707
+ if (
708
+ player &&
709
+ videoOptions &&
710
+ ( player . playerVars !== videoOptions . playerVars || player . host !== videoOptions . host )
711
+ ) {
688
712
// The player needs to be recreated if the playerVars are different.
689
713
player . destroy ( ) ;
690
714
} else if ( ! videoOptions ) {
@@ -704,6 +728,7 @@ function syncPlayerState(
704
728
) ;
705
729
newPlayer . videoId = videoOptions . videoId ;
706
730
newPlayer . playerVars = videoOptions . playerVars ;
731
+ newPlayer . host = videoOptions . host ;
707
732
return newPlayer ;
708
733
}
709
734
0 commit comments