-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathYTAPIScript.js
More file actions
50 lines (42 loc) · 1.5 KB
/
YTAPIScript.js
File metadata and controls
50 lines (42 loc) · 1.5 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const YTAPI = "https://www.youtube.com/iframe_api";
async function loadYTAPI() {
var tag = document.createElement('script');
tag.src = YTAPI;
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
await new Promise(function(resolve,reject) {
onYouTubeIframeAPIReady = resolve; //!!! THIS IS AN EVENT - empty function can be altered!!!
setTimeout(() => {
reject("Failed to load YT API");
}, 10000);
});
onYouTubeIframeAPIReady = function() {throw new Error("You cant just override this... again! Evil maggit!")};
}
var onYouTubeIframeAPIReady = function() {
throw new Error("Overwriting \"onYouTubeIframeAPIReady\" variable has not worked propperly...")
}
async function createYTPlayer(playerID, width, height, videoID) {
let ret;
await new Promise(function(resolve,reject) {
ret = new YT.Player(playerID, {
width:width,
height:height,
videoId:videoID,
events:{
onReady:resolve,
onStateChange: function(e) {ret.onPlayerStateChange(e)}
}
});
});
ret.onPlayerStateChange = function() {};
return ret;
};
async function changeYTVideo(YTPlayer, newVideo) {
YTPlayer.loadVideoById(newVideo);
};
async function jumpInVideo(YTPlayer, position) {
YTPlayer.seekTo(position, true);
}
function currentPosition(YTPlayer) {
return YTPlayer.getCurrentTime();
}