-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
137 lines (118 loc) · 5.3 KB
/
index.html
File metadata and controls
137 lines (118 loc) · 5.3 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
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
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<meta name="google-site-verification" content="PN1qz1isBNDvr24NEyfcxU214WMwG_LJ6GHqG0nj_IU" />
<title>YouTube Playlist</title>
<script src="app.js"></script>
<script src="https://www.youtube.com/iframe_api"></script>
</head>
<body>
<div id="elm"></div>
<!-- <script src="https://unpkg.com/elm-debug-transformer@latest/dist/elm-console-debug.js"></script> -->
<!-- <script>ElmConsoleDebug.register({simple_mode: true, debug: false, limit: 10000});</script> -->
<script>
const bytesStorageKey = 'bytes';
const tokenStorageKey = 'token';
const playlistStorageKey = 'playlist';
const oauthClientId = '1004146990872-svm4c3j6nof3afhjbjsf4mask09kc85n.apps.googleusercontent.com';
var player;
var noOp = () => {};
var popup;
var app = Elm.Main.init({
node: document.getElementById('elm'),
flags: {
bytes: getBytes(),
playlistInStorage: localStorage.getItem(playlistStorageKey) ? true : false,
playlistStorageKey: playlistStorageKey,
oauthClientId: oauthClientId,
time: Date.now(),
token: getToken(),
tokenStorageKey: tokenStorageKey,
},
});
function onYouTubeIframeAPIReady() {
app.ports && app.ports.onYouTubeApiReady && app.ports.onYouTubeApiReady.send();
};
function getBytes() {
const bytes = sessionStorage.getItem(bytesStorageKey);
return bytes ? bytes.split(',').map(x => parseInt(x, 10)) : [];
}
function getToken() {
const token = localStorage.getItem(tokenStorageKey);
return token ? JSON.parse(token) : null;
}
window.addEventListener('storage', event => {
if (event.storageArea !== localStorage || !event.key) {
return;
}
if (event.newValue === null) {
app.ports && app.ports.storageDeleted && app.ports.storageDeleted.send(event.key);
} else {
try {
var val = JSON.parse(event.newValue);
app.ports && app.ports.storageChanged && app.ports.storageChanged.send({
key: event.key,
value: val,
});
} catch {}
}
}, false);
app.ports && app.ports.createPlayer && app.ports.createPlayer.subscribe(element => {
if (player) {
app.ports && app.ports.onPlayerReady && app.ports.onPlayerReady.send();
return;
}
player = new YT.Player(element, {
events: {
'onReady': (app.ports && app.ports.onPlayerReady) ? app.ports.onPlayerReady.send : noOp,
'onStateChange': (app.ports && app.ports.onPlayerStateChange) ? app.ports.onPlayerStateChange.send : noOp,
'onError': (app.ports && app.ports.onPlayerError) ? app.ports.onPlayerError.send : noOp,
},
});
});
app.ports && app.ports.playVideo && app.ports.playVideo.subscribe(data => {
player.loadVideoById(data);
});
app.ports && app.ports.saveToStorage && app.ports.saveToStorage.subscribe(data => {
localStorage.setItem(
data.key,
JSON.stringify(data.value)
);
app.ports && app.ports.storageChanged && app.ports.storageChanged.send({
key: data.key,
value: data.value,
});
});
app.ports && app.ports.loadFromStorage && app.ports.loadFromStorage.subscribe(key => {
try {
var val = JSON.parse(localStorage.getItem(key));
app.ports && app.ports.receiveFromStorage && app.ports.receiveFromStorage.send({
key: key,
value: val,
});
} catch {}
});
app.ports && app.ports.removeFromStorage && app.ports.removeFromStorage.subscribe(key => {
localStorage.removeItem(key);
app.ports && app.ports.storageDeleted && app.ports.storageDeleted.send(key);
});
app.ports && app.ports.generateRandomBytes && app.ports.generateRandomBytes.subscribe(n => {
const buffer = new Uint8Array(n);
crypto.getRandomValues(buffer);
const bytes = Array.from(buffer);
sessionStorage.setItem(bytesStorageKey, bytes);
app.ports && app.ports.receiveRandomBytes && app.ports.receiveRandomBytes.send(bytes);
});
app.ports && app.ports.openPopup && app.ports.openPopup.subscribe(url => {
popup = window.open(url, 'popup', 'height=600,width=450');
});
app.ports && app.ports.closePopup && app.ports.closePopup.subscribe(d => {
popup && popup.close();
});
app.ports && app.ports.consoleErr && app.ports.consoleErr.subscribe(msg => console.error(msg));
app.ports && app.ports.onYouTubeApiReady || console.warn('Port onYouTubeApiReady not found.');
app.ports && app.ports.onPlayerStateChange || console.warn('Port onPlayerStateChange not found.');
</script>
</body>
</html>