-
Notifications
You must be signed in to change notification settings - Fork 0
/
videoplayer.js
107 lines (94 loc) · 3.37 KB
/
videoplayer.js
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
document.addEventListener('DOMContentLoaded', () => {
const videoPlayerBtn = document.getElementById('video-player-btn');
if (!videoPlayerBtn) {
console.error('Video player button not found');
return;
}
const videoPlayerPopup = document.createElement('div');
videoPlayerPopup.id = 'video-player-popup';
document.body.appendChild(videoPlayerPopup);
const playlistId = 'PLQ2eZ195XtL126eyaiVXinY5Z3mpsyAu3';
function createVideoPlayerPopup() {
videoPlayerPopup.innerHTML = `
<div class="video-player-content">
<button id="close-video-player">×</button>
<div id="video-container"></div>
<div id="playlist-title">YouTube Playlist</div>
</div>
`;
document.getElementById('close-video-player').addEventListener('click', closeVideoPlayerPopup);
videoPlayerPopup.style.display = 'none';
}
function openVideoPlayerPopup() {
videoPlayerPopup.style.display = 'block';
loadPlaylist();
}
function closeVideoPlayerPopup() {
videoPlayerPopup.style.display = 'none';
document.getElementById('video-container').innerHTML = '';
}
function loadPlaylist(retryCount = 0) {
const container = document.getElementById('video-container');
container.innerHTML = '<div id="loading">Loading playlist...</div>';
const iframe = document.createElement('iframe');
iframe.width = '560';
iframe.height = '315';
iframe.src = `https://www.youtube.com/embed/videoseries?list=${playlistId}`;
iframe.frameBorder = '0';
iframe.allow = 'autoplay; encrypted-media';
iframe.allowFullscreen = true;
iframe.onload = () => {
container.innerHTML = '';
container.appendChild(iframe);
};
iframe.onerror = () => {
if (retryCount < 3) {
setTimeout(() => loadPlaylist(retryCount + 1), 2000);
} else {
container.innerHTML = 'Error loading playlist. Please try again later.';
}
};
// Set a timeout in case the iframe fails to load or trigger any event
setTimeout(() => {
if (container.innerHTML.includes('Loading playlist')) {
if (retryCount < 3) {
loadPlaylist(retryCount + 1);
} else {
container.innerHTML = 'Error loading playlist. Please check your internet connection and try again.';
}
}
}, 10000); // 10 seconds timeout
}
// Call this function to set up the popup
createVideoPlayerPopup();
// Add styles
const style = document.createElement('style');
style.textContent = `
#video-player-popup {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: white;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0,0,0,0.3);
z-index: 1000;
}
#close-video-player {
position: absolute;
top: 10px;
right: 10px;
background: none;
border: none;
font-size: 20px;
cursor: pointer;
}
#playlist-title {
text-align: center;
margin-top: 10px;
font-weight: bold;
}
`;
document.head.appendChild(style);
});