-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjyoutube.js
More file actions
29 lines (27 loc) · 1.06 KB
/
jyoutube.js
File metadata and controls
29 lines (27 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
const videos = document.querySelectorAll('.jyoutube');
let generateUrl = function(id) {
let query = '?rel=0&showinfo=0&autoplay=1';
return 'https://www.youtube.com/embed/' + id + query;
};
let createIframe = function(id) {
let iframe = document.createElement('iframe');
iframe.setAttribute('allowfullscreen', '');
iframe.setAttribute('allow', 'autoplay; encrypted-media');
iframe.setAttribute('src', generateUrl(id));
return iframe;
};
videos.forEach((el) => {
let videoHref = el.getAttribute('data-video');
let deletedLength = 'https://youtu.be/'.length;
let videoId = videoHref.substring(deletedLength, videoHref.length);
let img = el.querySelector('img');
let youtubeImgSrc = 'https://i.ytimg.com/vi_webp/' + videoId + '/maxresdefault.webp';
img.setAttribute('src', youtubeImgSrc);
el.addEventListener('click', (e) => {
e.preventDefault();
let iframe = createIframe(videoId);
el.querySelector('img').remove();
el.appendChild(iframe);
el.querySelector('button').remove();
});
});