-
Notifications
You must be signed in to change notification settings - Fork 0
/
youtube.js
121 lines (111 loc) · 3.49 KB
/
youtube.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
function isValidURL(url) {
var parser = new URL(url);
if (
!parser.searchParams.has("playlist_id") ||
!parser.searchParams.has("type")
) {
return false;
}
let type = parser.searchParams.get("type");
if (type !== "random" && type !== "reverse" && type !== "normal") {
return false;
}
return true;
}
function displayInvalidMessage() {
window.alert("Invalid query parameters. Please try again.");
document.getElementById("youtube").style.display = "none";
document.getElementById("how-to-use").style.display = "block";
}
function processURL() {
let url = window.location.href;
let splitted = url.split("?");
if (url === splitted[0]) {
// If no '?' in URL
return;
}
if (!isValidURL(url)) {
displayInvalidMessage();
return;
}
document.getElementById("how-to-use").style.display = "none";
document.getElementById("youtube").style.display = "block";
}
function redirect(type) {
let playlist_id = document.getElementById("id_input").value;
window.location.href = `https://ternbusty.github.io/youtube.html?type=${type}&playlist_id=${playlist_id}`;
// window.location.href = `http://localhost:4000/youtube.html?type=${type}&playlist_id=${playlist_id}`;
}
processURL();
// Load YouTube API
let tag = document.createElement("script");
tag.src = "https://www.youtube.com/iframe_api";
let firstScriptTag = document.getElementsByTagName("script")[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
function onYouTubeIframeAPIReady() {
player = new YT.Player("player", {
height: "360",
width: "640",
events: {
onReady: this.onPlayerReady,
onStateChange: this.onPlayerStateChange,
},
});
document.player = player;
player.yts = new YouTubeShuffler();
}
function onPlayerReady(event) {
document.not_found_cnt = 0;
let id = setInterval(() => {
if (document.id_arr && document.id_arr[0] !== undefined) {
console.log("found");
clearInterval(id);
event.target.yts.loadVideoByCnt(event.target, 0);
} else if (document.failflag === 1) {
displayInvalidMessage();
clearInterval(id);
} else {
console.log("not found");
document.not_found_cnt++;
if (document.not_found_cnt++ > 5) {
console.log("Please install tampermonkey script");
clearInterval(id);
}
}
}, 1000);
}
function onPlayerStateChange(event) {
if (event.data == 0) {
// Erase the now playing mark
let table = document.getElementById("que");
table.rows[event.target.yts.cnt].cells[1].innerHTML = "";
// Load next video
event.target.yts.loadVideoByCnt(event.target, event.target.yts.cnt + 1);
}
}
class YouTubeShuffler {
constructor() {
this.cnt = 0;
}
loadVideoByCnt(player, cnt) {
if (this.cnt < 0 || this.cnt >= document.id_arr.length) return;
this.cnt = cnt;
// If the video specified by cnt is unavailable, move forward
while (document.id_arr[this.cnt] === "") {
if (this.cnt === document.id_arr.length - 1) return;
this.cnt++;
}
console.log("Loading " + document.id_arr[this.cnt]);
player.loadVideoById(document.id_arr[this.cnt], 0);
let table = document.getElementById("que");
table.rows[this.cnt].cells[1].innerHTML =
'<i class="fa-solid fa-volume-high"></i>';
}
changeVideoByClick(player, cnt) {
// Erase the now playing mark
let table = document.getElementById("que");
table.rows[this.cnt].cells[1].innerHTML = "";
// Load the specified video
player.yts.loadVideoByCnt(player, cnt);
}
}