-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
499 lines (460 loc) · 15.3 KB
/
Copy pathscript.js
File metadata and controls
499 lines (460 loc) · 15.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
let players = [];
let earliestStart = null;
let markers = [];
let playInterval = null;
let playing = false;
const SIDEBAR_WIDTH = 250;
const seekBar = document.getElementById('seek-bar');
const seekTime = document.getElementById('seek-time');
const markersContainer = document.getElementById('markers');
const sidebar = document.getElementById('sidebar');
const vodList = document.getElementById('vod-list');
const clientIdInput = document.getElementById('client-id');
const clientSecretInput = document.getElementById('client-secret');
const apiError = document.getElementById('api-error');
const playToggle = document.getElementById('play-toggle');
const playerContainer = document.getElementById('player-container');
function updateGridLayout() {
const sidebarOpen = document.body.classList.contains('sidebar-open');
playerContainer.style.width = sidebarOpen ? `calc(100% - ${SIDEBAR_WIDTH}px)` : '100%';
const count = players.length || 1;
const cols = Math.min(4, Math.max(1, Math.ceil(Math.sqrt(count))));
playerContainer.style.gridTemplateColumns = `repeat(${cols}, 1fr)`;
}
function formatTime(sec) {
const h = Math.floor(sec / 3600).toString().padStart(2, '0');
const m = Math.floor((sec % 3600) / 60).toString().padStart(2, '0');
const s = Math.floor(sec % 60).toString().padStart(2, '0');
return `${h}:${m}:${s}`;
}
function colorFromId(id) {
let hash = 0;
for (let i = 0; i < id.length; i++) {
hash = (hash * 31 + id.charCodeAt(i)) >>> 0;
}
const hue = hash % 360;
return `hsl(${hue},70%,50%)`;
}
function updateSeekDisplay() {
seekTime.textContent = formatTime(parseInt(seekBar.value, 10));
}
function updateMarkers() {
markersContainer.innerHTML = '';
if (!earliestStart) return;
const max = parseInt(seekBar.max, 10);
players.forEach(p => {
const diff = Math.round((p.startTime - earliestStart) / 1000);
if (diff < 0 || diff > max) return;
const marker = document.createElement('div');
marker.className = 'marker';
marker.dataset.vodid = p.id;
marker.style.left = `${diff / max * 100}%`;
marker.style.backgroundColor = p.color;
const label = document.createElement('span');
label.textContent = formatTime(diff);
marker.appendChild(label);
markersContainer.appendChild(marker);
});
}
function renderOrder() {
const container = document.getElementById('player-container');
const infoParent = document.getElementById('vod-list');
players.forEach(p => {
container.appendChild(p.wrapper);
infoParent.appendChild(p.infoElem);
});
}
const toggleSidebarBtn = document.getElementById('toggleSidebar');
const closeSidebarBtn = document.querySelector('.close-sidebar');
toggleSidebarBtn.addEventListener('click', () => {
const open = sidebar.classList.toggle('open');
document.body.classList.toggle('sidebar-open', open);
updateGridLayout();
});
closeSidebarBtn.addEventListener('click', () => {
sidebar.classList.remove('open');
document.body.classList.remove('sidebar-open');
updateGridLayout();
});
function startGlobal() {
if (playing) return;
playing = true;
playToggle.textContent = '⏸';
playInterval = setInterval(() => {
let val = parseInt(seekBar.value, 10);
if (val < parseInt(seekBar.max, 10)) {
seekBar.value = val + 1;
updateSeekDisplay();
syncPlayers();
}
}, 1000);
players.forEach(p => {
p.isPlaying = true;
p.lastControlledBy = 'global';
p.updateStateClass();
});
syncPlayers(true);
}
function pauseGlobal() {
if (!playing) return;
playing = false;
playToggle.textContent = '▶';
clearInterval(playInterval);
playInterval = null;
players.forEach(p => {
p.isPlaying = false;
p.lastControlledBy = 'global';
p.updateStateClass();
});
syncPlayers(true);
}
playToggle.addEventListener('click', () => {
if (playing) {
pauseGlobal();
} else {
startGlobal();
}
});
function adjustOffset(player, diff) {
player.offset += diff;
player.offsetDisplay.textContent = `${player.offset}s`;
if (player.infoOffset) player.infoOffset.textContent = `${player.offset}s`;
syncPlayers(true);
}
function movePlayer(player, dir) {
const idx = players.indexOf(player);
const newIdx = idx + dir;
if (newIdx < 0 || newIdx >= players.length) return;
const other = players[newIdx];
[players[idx], players[newIdx]] = [players[newIdx], players[idx]];
renderOrder();
updateGridLayout();
}
function shouldPlay(player, time) {
const end = player.duration ? player.startTime + player.duration * 1000 : Infinity;
return time >= player.startTime && time <= end;
}
function syncPlayers(force = false) {
if (players.length === 0 || earliestStart === null) return;
const baseSeconds = parseInt(seekBar.value, 10);
const baseTime = earliestStart + baseSeconds * 1000;
players.forEach(p => {
if (!force) {
if (p.lastControlledBy === 'user' && Date.now() - p.lastUserTime < 30000) {
if (p.infoTime) p.infoTime.textContent = new Date(baseTime + p.offset * 1000).toLocaleString();
return;
}
}
let sec = (baseTime - p.startTime) / 1000 + p.offset;
if (p.duration && sec > p.duration) sec = p.duration;
if (sec < 0) sec = 0;
p.player.seek(sec);
if (playing) {
if (shouldPlay(p, baseTime)) {
p.player.play();
p.isPlaying = true;
} else {
p.player.pause();
p.isPlaying = false;
}
} else {
p.player.pause();
p.isPlaying = false;
}
p.lastControlledBy = 'global';
p.updateStateClass();
if (p.infoTime) p.infoTime.textContent = new Date(baseTime + p.offset * 1000).toLocaleString();
});
}
async function addStream() {
const input = document.getElementById('channel-input');
const val = input.value.trim();
if (!val) return;
input.value = '';
const withChat = document.getElementById('with-chat').checked;
if (!TwitchAPI.hasCredentials()) {
apiError.textContent = 'API設定が未完了です';
return;
} else {
apiError.textContent = '';
}
let startTime = null;
let duration = null;
let label = val;
let videoId = null;
let userId = null;
let options = {
width: 640,
height: 360,
parent: [location.hostname]
};
const match = val.match(/twitch\.tv\/videos\/(\d+)/);
if (val.includes('twitch.tv/videos') && !match) {
alert('VODのIDを抽出できませんでした');
return;
}
if (match) {
videoId = match[1];
console.log(`\u{1F3AC} VOD ID: ${videoId}`);
try {
const info = await TwitchAPI.getVideoInfo(videoId);
if (info) {
startTime = info.createdAt;
userId = info.userId;
duration = info.duration;
}
} catch (e) {
console.error(e);
apiError.textContent = '認証情報が正しいか確認してください';
return;
}
options.video = videoId;
label = `v${videoId}`;
} else if (/^\d+$/.test(val)) {
videoId = val;
console.log(`\u{1F3AC} VOD ID: ${videoId}`);
try {
const info = await TwitchAPI.getVideoInfo(videoId);
if (info) {
startTime = info.createdAt;
userId = info.userId;
duration = info.duration;
}
} catch (e) {
console.error(e);
apiError.textContent = '認証情報が正しいか確認してください';
return;
}
options.video = videoId;
label = `v${videoId}`;
} else {
const login = val.toLowerCase();
try {
startTime = await TwitchAPI.getLiveStartTime(login);
if (!startTime) {
const uid = await TwitchAPI.getUserId(login);
if (uid) {
userId = uid;
startTime = await TwitchAPI.getLatestVODStartTime(uid);
}
}
if (!userId) {
userId = await TwitchAPI.getUserId(login);
}
} catch (e) {
console.error(e);
apiError.textContent = '認証情報が正しいか確認してください';
return;
}
options.channel = login;
}
if (!startTime) {
alert('開始時刻を取得できませんでした');
return;
}
createPlayer(label, options, startTime, withChat, videoId, userId, duration);
apiError.textContent = '';
}
function createPlayer(label, options, startTime, withChat, videoId, userId, duration) {
const container = document.getElementById('player-container');
const wrapper = document.createElement('div');
wrapper.className = 'player-wrapper';
const div = document.createElement('div');
const id = `tw-${Date.now()}-${Math.random().toString(36).slice(2,8)}`;
div.id = id;
const overlay = document.createElement('div');
overlay.className = 'player-overlay';
const mute = document.createElement('button');
mute.textContent = '🔈';
const minus = document.createElement('button');
minus.textContent = '-1s';
const plus = document.createElement('button');
plus.textContent = '+1s';
const display = document.createElement('span');
display.className = 'offset-display';
display.textContent = '0s';
const remove = document.createElement('button');
remove.textContent = '×';
overlay.appendChild(mute);
overlay.appendChild(minus);
overlay.appendChild(display);
overlay.appendChild(plus);
overlay.appendChild(remove);
wrapper.appendChild(div);
wrapper.appendChild(overlay);
container.appendChild(wrapper);
function updateStateClass() {
wrapper.classList.remove('global', 'user', 'offset');
wrapper.classList.add(player.lastControlledBy);
}
if (withChat && options.channel) {
const chat = document.createElement('iframe');
chat.src = `https://www.twitch.tv/embed/${options.channel}/chat?parent=${location.hostname}`;
chat.width = "100%";
chat.height = "200";
wrapper.appendChild(chat);
}
const playerInstance = new Twitch.Player(id, options);
playerInstance.addEventListener(Twitch.Player.PLAY, () => {
player.isPlaying = true;
player.lastControlledBy = 'user';
player.lastUserTime = Date.now();
updateStateClass();
});
playerInstance.addEventListener(Twitch.Player.PAUSE, () => {
player.isPlaying = false;
player.lastControlledBy = 'user';
player.lastUserTime = Date.now();
updateStateClass();
});
playerInstance.addEventListener(Twitch.Player.SEEK, () => {
player.lastControlledBy = 'user';
player.lastUserTime = Date.now();
updateStateClass();
});
const info = document.createElement('div');
info.className = 'vod-info';
const pid = videoId ? String(videoId) : label;
const color = colorFromId(pid);
info.dataset.vodid = pid;
info.style.borderLeftColor = color;
info.innerHTML = `<div>🆔 <span class="vid">${videoId ? videoId : 'LIVE'}</span></div>` +
`<div>👤 <span class="uid">${userId || ''}</span></div>` +
`<div>⏰ <span class="ptime"></span></div>`;
const ctrl = document.createElement('div');
ctrl.className = 'controls';
const mute2 = document.createElement('button');
mute2.className = 'mute-toggle';
mute2.textContent = '🔇';
const upBtn = document.createElement('button');
upBtn.className = 'move-up';
upBtn.textContent = '⬆';
const downBtn = document.createElement('button');
downBtn.className = 'move-down';
downBtn.textContent = '⬇';
const minus2 = document.createElement('button');
minus2.textContent = '-1s';
const plus2 = document.createElement('button');
plus2.textContent = '+1s';
const off2 = document.createElement('span');
off2.textContent = '0s';
ctrl.appendChild(mute2);
ctrl.appendChild(upBtn);
ctrl.appendChild(downBtn);
ctrl.appendChild(minus2);
ctrl.appendChild(off2);
ctrl.appendChild(plus2);
info.appendChild(ctrl);
vodList.appendChild(info);
const player = { label, id: pid, color, player: playerInstance, startTime, duration, offset: 0, offsetDisplay: display, infoTime: info.querySelector('.ptime'), infoOffset: off2, infoElem: info, wrapper, isPlaying: true, lastControlledBy: 'global', lastUserTime: 0, updateStateClass };
updateStateClass();
function offsetChanged(diff) {
adjustOffset(player, diff);
player.lastControlledBy = 'offset';
updateStateClass();
}
minus.addEventListener('click', () => offsetChanged(-1));
plus.addEventListener('click', () => offsetChanged(1));
minus2.addEventListener('click', () => offsetChanged(-1));
plus2.addEventListener('click', () => offsetChanged(1));
mute.addEventListener('click', toggleMute);
mute2.addEventListener('click', toggleMute);
function toggleMute() {
const muted = playerInstance.getMuted();
playerInstance.setMuted(!muted);
mute.textContent = mute2.textContent = muted ? '🔈' : '🔇';
}
upBtn.addEventListener('click', () => movePlayer(player, -1));
downBtn.addEventListener('click', () => movePlayer(player, 1));
remove.addEventListener('click', () => {
wrapper.remove();
info.remove();
players = players.filter(p => p !== player);
if (players.length === 0) {
earliestStart = null;
} else {
earliestStart = Math.min(...players.map(p => p.startTime));
}
const maxDiff = players.length === 0 ? 0 : Math.max(...players.map(p => (p.startTime - earliestStart) / 1000));
seekBar.max = Math.max(7200, Math.ceil(maxDiff) + 300);
renderOrder();
updateMarkers();
updateGridLayout();
});
players.push(player);
renderOrder();
updateGridLayout();
if (!earliestStart || startTime < earliestStart) {
earliestStart = startTime;
seekBar.value = 0;
updateSeekDisplay();
}
const maxDiff = Math.max(...players.map(p => (p.startTime - earliestStart) / 1000));
seekBar.max = Math.max(7200, Math.ceil(maxDiff) + 300);
updateMarkers();
}
seekBar.addEventListener('input', () => {
updateSeekDisplay();
players.forEach(p => {
p.lastControlledBy = 'global';
p.updateStateClass();
});
syncPlayers(true);
});
document.getElementById('add-button').addEventListener('click', addStream);
document.getElementById('sync-button').addEventListener('click', () => syncPlayers(true));
document.getElementById('save-api').addEventListener('click', async () => {
const id = clientIdInput.value.trim();
const secret = clientSecretInput.value.trim();
TwitchAPI.setCredentials(id, secret);
try {
await TwitchAPI.init();
apiError.textContent = '';
} catch (e) {
console.error(e);
apiError.textContent = '認証情報が正しいか確認してください';
}
});
document.getElementById('open-dev').addEventListener('click', () => {
window.open('https://dev.twitch.tv/console/apps', '_blank');
});
window.addEventListener('DOMContentLoaded', async () => {
clientIdInput.value = localStorage.getItem('clientId') || '';
clientSecretInput.value = localStorage.getItem('clientSecret') || '';
if (TwitchAPI.hasCredentials()) {
try {
await TwitchAPI.init();
} catch (e) {
console.error(e);
apiError.textContent = '認証情報が正しいか確認してください';
}
} else {
apiError.textContent = 'API設定が未完了です';
}
playToggle.textContent = '▶';
updateSeekDisplay();
updateGridLayout();
});
window.addEventListener('resize', updateGridLayout);
setInterval(() => {
players.forEach(p => {
if (p.infoTime) {
const t = new Date(p.startTime + p.player.getCurrentTime() * 1000);
p.infoTime.textContent = t.toLocaleString();
}
});
}, 1000);
function checkUserTimeout() {
const now = Date.now();
let changed = false;
players.forEach(p => {
if (p.lastControlledBy === 'user' && now - p.lastUserTime > 30000) {
p.lastControlledBy = 'global';
changed = true;
}
});
if (changed) {
syncPlayers();
}
}
setInterval(checkUserTimeout, 1000);