-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
143 lines (132 loc) · 4 KB
/
main.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// document loaded
(function () {
// LOG ERRORS TO SERVER LOG
window.onerror = function (msg, url, line, col, error) {
/* document.write(msg);
document.write(url);
document.write(line);
document.write(col);
document.write(error); */
var req = new XMLHttpRequest();
var params = 'msg=' + encodeURIComponent(msg) + '&url=' + encodeURIComponent(url) + '&line=' + line + '&col=' + col + '&error=' + error;
req.onreadystatechange = function () {
if (req.readyState == XMLHttpRequest.DONE && req.status == 200) {
console.log(req);
}
}
req.open('POST', 'https://screensaver.metazoa.org/screensaver/log/index.php');
req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
req.send(params);
}
})();
var DEBUG = false;
function youtubeAPIInit (iframeId) {
log('youtubeAPIInit', iframeId)
var tag = document.createElement('script')
tag.id = 'iframe-js'
tag.src = 'https://www.youtube.com/iframe_api'
var firstScriptTag = document.getElementsByTagName('script')[0]
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag)
}
var player
function onYouTubeIframeAPIReady () {
// 3. This function creates an <iframe> (and YouTube player)
// after the API code downloads.
player = new YT.Player('iframe-video', {
playerVars: {
'controls': 0,
'color': 'white',
'disablekb': 1,
'enablejsapi': 1,
'fs': 0,
'iv_load_policy': 3,
'modestbranding': 1,
'origin': 'metazoa.org',
'showinfo': 0,
'rel': 0
},
events: {
'onReady': onPlayerReady,
'onPlaybackQualityChange': onPlayerPlaybackQualityChange,
'onStateChange': onPlayerStateChange,
'onError': onPlayerError
}
})
}
// 4. The API will call this function when the video player is ready.
function onPlayerReady (event) {
log('onPlayerReady', event)
var plyr = event.target;
plyr.playVideo() // play
plyr.setVolume(5); // volume in %
plyr.hideVideoInfo();
}
function onPlayerPlaybackQualityChange (event) {
// log('onPlayerPlaybackQualityChange', event)
}
// 5. The API calls this function when the player's state changes.
// The function indicates that when playing a video (state=1),
// the player should play for six seconds and then stop.
function onPlayerStateChange (playerStatus) {
if (playerStatus.data == -1) {
// unstarted
log('unstarted', playerStatus.target)
playerStatus.target.playVideo()
} else if (playerStatus.data == 0) {
// ended
log('ended', playerStatus.target)
playerStatus.target.playVideo() // loop
} else if (playerStatus.data == 1) {
// playing
} else if (playerStatus.data == 2) {
// paused
log('paused', playerStatus.target)
playerStatus.target.playVideo()
} else if (playerStatus.data == 3) {
// buffering = purple
} else if (playerStatus.data == 5) {
// video cued = orange
}
}
function onPlayerError (event) {
log('onPlayerError', event)
}
function stopVideo () {
log('stopVideo')
player.stopVideo()
}
function hideDescription(selector, time) {
setTimeout(function(){fadeOut(selector);}, time);
}
function fadeOut(selector) {
var el = document.querySelector(selector);
// credit to:
// http://www.chrisbuttery.com/articles/fade-in-fade-out-with-javascript/
el.style.opacity = 0.7;
(function fade() {
if ((el.style.opacity -= .1) < 0) {
el.style.display = 'none';
} else {
// do nothing
requestAnimationFrame(fade);
}
})();
}
// change color for each element child
function changeColorForEachChild(elm) {
// console.log('changeColorForEachParagraph', elm);
var children = elm.children;
log(children);
for (var i = 0; i < children.length; i++) {
children[i].style.color = '#'+(Math.random()*0xFFFFFF<<0).toString(16);
}
}
// return random number from to
function randomFromTo(from, to) {
return Math.floor(Math.random() * to) + from
}
function log(msg, val) {
if (DEBUG) {
console.log(msg, val);
}
}