Skip to content

Commit d5b7faf

Browse files
committed
feat(video): support youtube nocookie embed
1 parent 0a6f93b commit d5b7faf

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed

src/plugins/video/lg-video-utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ export const getYouTubeParams = (
5454
return youTubeParams;
5555
};
5656

57+
export const isYouTubeNoCookie = (url: string): boolean => {
58+
return url.includes('youtube-nocookie.com');
59+
};
60+
5761
export const getVimeoURLParams = (
5862
defaultParams: PlayerParams,
5963
videoInfo?: VideoInfo,

src/plugins/video/lg-video.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,12 @@ import {
3535
} from '../../types';
3636
import { lGEvents } from '../../lg-events';
3737
import { VideoSource } from './types';
38-
import { getVimeoURLParams, getYouTubeParams, param } from './lg-video-utils';
38+
import {
39+
getVimeoURLParams,
40+
getYouTubeParams,
41+
isYouTubeNoCookie,
42+
param,
43+
} from './lg-video-utils';
3944

4045
declare let Vimeo: any;
4146
declare let videojs: any;
@@ -236,7 +241,13 @@ export default class Video {
236241
this.settings.youTubePlayerParams,
237242
);
238243

239-
video = `<iframe allow="autoplay" id=${videoId} class="lg-video-object lg-youtube ${addClass}" ${videoTitle} src="//www.youtube.com/embed/${
244+
const isYouTubeNoCookieURL = isYouTubeNoCookie(src);
245+
246+
const youtubeURL = isYouTubeNoCookieURL
247+
? '//www.youtube-nocookie.com/'
248+
: '//www.youtube.com/';
249+
250+
video = `<iframe allow="autoplay" id=${videoId} class="lg-video-object lg-youtube ${addClass}" ${videoTitle} src="${youtubeURL}embed/${
240251
videoInfo.youtube[1] + youTubeParams
241252
}" ${commonIframeProps}></iframe>`;
242253
} else if (videoInfo.vimeo) {

test/lg-video.test.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import '@testing-library/jest-dom';
1111
import {
1212
getVimeoURLParams,
1313
getYouTubeParams,
14+
isYouTubeNoCookie,
1415
} from '../src/plugins/video/lg-video-utils';
1516
import utils from '../src/lg-utils';
1617
import { VideoInfo } from '../src/types';
@@ -136,7 +137,7 @@ describe('Vimeo Video', () => {
136137

137138
describe('YouTube Video', () => {
138139
it('should build player params', async () => {
139-
const url = '="//www.youtube.com/watch?v=EIUJfXk3_3w';
140+
const url = '//www.youtube.com/watch?v=EIUJfXk3_3w';
140141
const videoInfo = utils.isVideo(url, false, 0);
141142
const settingParam = {
142143
color: 'red',
@@ -148,7 +149,7 @@ describe('YouTube Video', () => {
148149
);
149150
});
150151
it('should override default params with src parmas', async () => {
151-
const url = '="//www.youtube.com/watch?v=EIUJfXk3_3w&mute=0';
152+
const url = '//www.youtube.com/watch?v=EIUJfXk3_3w&mute=0';
152153
const videoInfo = utils.isVideo(url, false, 0);
153154
const settingParam = {
154155
color: 'red',
@@ -160,21 +161,21 @@ describe('YouTube Video', () => {
160161
);
161162
});
162163
it('should work if settingsParams not provided', async () => {
163-
const url = '="//www.youtube.com/watch?v=EIUJfXk3_3w&mute=0';
164+
const url = '//www.youtube.com/watch?v=EIUJfXk3_3w&mute=0';
164165
const videoInfo = utils.isVideo(url, false, 0);
165166
const settingParam = false;
166167
const params = getYouTubeParams(videoInfo as VideoInfo, settingParam);
167168
expect(params).toBe('?wmode=opaque&autoplay=0&mute=0&enablejsapi=1');
168169
});
169170
it('should work if settingsParams and src params not provided', async () => {
170-
const url = '="//www.youtube.com/watch?v=EIUJfXk3_3w';
171+
const url = '//www.youtube.com/watch?v=EIUJfXk3_3w';
171172
const videoInfo = utils.isVideo(url, false, 0);
172173
const settingParam = false;
173174
const params = getYouTubeParams(videoInfo as VideoInfo, settingParam);
174175
expect(params).toBe('?wmode=opaque&autoplay=0&mute=1&enablejsapi=1');
175176
});
176177
it('should override default params with settings params', async () => {
177-
const url = '="//www.youtube.com/watch?v=EIUJfXk3_3w';
178+
const url = '//www.youtube.com/watch?v=EIUJfXk3_3w';
178179
const videoInfo = utils.isVideo(url, false, 0);
179180
const settingParam = {
180181
autoplay: 1,
@@ -184,7 +185,7 @@ describe('YouTube Video', () => {
184185
expect(params).toBe('?wmode=opaque&autoplay=1&mute=0&enablejsapi=1');
185186
});
186187
it('should override settings params with src params', async () => {
187-
const url = '="//www.youtube.com/watch?v=EIUJfXk3_3w&mute=0&color=red';
188+
const url = '//www.youtube.com/watch?v=EIUJfXk3_3w&mute=0&color=red';
188189
const videoInfo = utils.isVideo(url, false, 0);
189190
const settingParam = {
190191
autoplay: 1,
@@ -195,4 +196,16 @@ describe('YouTube Video', () => {
195196
'?wmode=opaque&autoplay=1&mute=0&enablejsapi=1&color=red',
196197
);
197198
});
199+
200+
it('should detect as no-cookie url', async () => {
201+
const url = 'https://www.youtube-nocookie.com/embed/r87A-Ql2czg';
202+
const isYouTubeNoCookieURL = isYouTubeNoCookie(url);
203+
expect(isYouTubeNoCookieURL).toBeTruthy();
204+
});
205+
206+
it('should detect as normal youtube url', async () => {
207+
const url = '//www.youtube.com/watch?v=EIUJfXk3_3w&mute=0&color=red';
208+
const isYouTubeNoCookieURL = isYouTubeNoCookie(url);
209+
expect(isYouTubeNoCookieURL).toBeFalsy();
210+
});
198211
});

0 commit comments

Comments
 (0)