-
Notifications
You must be signed in to change notification settings - Fork 1
/
play.js
121 lines (103 loc) · 3.28 KB
/
play.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
const button = document.getElementById('play');
const playerFrame = document.getElementById('playerFrame');
const body = document.querySelector('body');
const isFirefox = navigator.userAgent.toLowerCase().indexOf('firefox') > -1;
const fps = 30,
msPerFrame = 1000 / fps;
let frames;
const isVideoLoadedAndPlaying = (e) => {
const { event, info } = JSON.parse(e.data);
return event === 'infoDelivery' && info.currentTime > 0;
};
const stop = () => {
window.stopTimer = true;
button.innerText = 'Play';
body.classList.remove('hide-text');
window.player.stopVideo();
};
const play = async () => {
button.innerText = 'Loading video...';
button.disabled = true;
body.classList.add('hide-text');
console.clear();
const listener = (e) => {
// Wait for video to start playing
if (!isVideoLoadedAndPlaying(e)) return;
removeEventListener('message', listener);
button.disabled = false;
button.innerText = 'Stop';
// start printing
console.log(frames[0]);
doTimer(frames.length * msPerFrame, fps, (steps, count) => {
if (frames[count + 1]) {
if (isFirefox && (msPerFrame * count) % 6000 < msPerFrame) {
// Firefox console slows down a bit if console isn't cleared once in a while
console.clear();
}
console.log(frames[count + 1]);
} else {
stop();
}
});
};
addEventListener('message', listener);
window.player.playVideo();
window.stopTimer = false;
};
button.addEventListener('click', (e) => {
if (window.stopTimer === undefined || window.stopTimer === true) {
play();
} else {
stop();
}
});
// Fetch, unzip and load text
const load = () => {
button.innerText = 'Fetching frames...';
JSZipUtils.getBinaryContent('braille.zip', (err, data) => {
if (err) throw err;
button.innerText = 'Unzipping frames...';
JSZip.loadAsync(data).then((zip) => {
button.innerText = 'Loading frames...';
zip
.file('braille.txt')
.async('string')
.then((content) => {
frames = content.split('\n\n');
delete frames[frames.length - 1]; // Delete last empty 'frame' created by split command
button.innerText = 'Play';
button.disabled = false;
// Show tip to properly display frames
const helpLine =
new Array(frames[0].split('\n')[0].length + 1).join('⣿') +
'⣿⣿⣿⣿⣿⣿⣿⣿';
console.clear();
console.info(
"%cTweak console width/zoom so that the following line doesn't break:",
'font-size: 22pt'
);
console.info('%c' + helpLine + '\n', 'color: cyan; ');
});
});
});
};
function onYouTubeIframeAPIReady() {
// Creating a player enables window message events from the embed we can listen to
window.player = new YT.Player('playerFrame', {
width: 640,
height: 360,
videoId: 'FtutLA63Cp8',
playerVars: {
disablekb: 1,
fs: 0,
iv_load_policy: 3,
showinfo: 0,
},
});
}
// This code loads the IFrame Player API code asynchronously.
let tag = document.createElement('script');
tag.src = 'https://www.youtube.com/iframe_api';
let firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
load();