-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.js
406 lines (352 loc) · 15.8 KB
/
app.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
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
import QrScanner from "https://unpkg.com/qr-scanner/qr-scanner.min.js";
let player; // Define player globally
let playbackTimer; // hold the timer reference
let playbackDuration = 30; // Default playback duration
let qrScanner;
let csvCache = {};
document.addEventListener('DOMContentLoaded', function () {
let lastDecodedText = ""; // Store the last decoded text
const video = document.getElementById('qr-video');
const resultContainer = document.getElementById("qr-reader-results");
qrScanner = new QrScanner(video, result => {
console.log('decoded qr code:', result);
if (result.data !== lastDecodedText) {
lastDecodedText = result.data; // Update the last decoded text
handleScannedLink(result.data);
}
}, {
highlightScanRegion: true,
highlightCodeOutline: true,
}
);
// Function to determine the type of link and act accordingly
async function handleScannedLink(decodedText) {
let youtubeURL = "";
if (isYoutubeLink(decodedText)) {
youtubeURL = decodedText;
} else if (isHitsterLink(decodedText)) {
const hitsterData = parseHitsterUrl(decodedText);
if (hitsterData) {
console.log("Hitster data:", hitsterData.id, hitsterData.lang);
try {
const csvContent = await getCachedCsv(`/hitster-${hitsterData.lang}.csv`);
const youtubeLink = lookupYoutubeLink(hitsterData.id, csvContent);
if (youtubeLink) {
// Handle YouTube link obtained from the CSV
console.log(`YouTube Link from CSV: ${youtubeLink}`);
youtubeURL = youtubeLink;
// Example: player.cueVideoById(parseYoutubeLink(youtubeLink).videoId);
}
} catch (error) {
console.error("Failed to fetch CSV:", error);
}
}
else {
console.log("Invalid Hitster URL:", decodedText);
}
}
console.log(`YouTube Video URL: ${youtubeURL}`);
const youtubeLinkData = parseYoutubeLink(youtubeURL);
if (youtubeLinkData) {
qrScanner.stop(); // Stop scanning after a result is found
document.getElementById('qr-reader').style.display = 'none'; // Hide the scanner after successful scan
document.getElementById('cancelScanButton').style.display = 'none'; // Hide the cancel-button
lastDecodedText = ""; // Reset the last decoded text
document.getElementById('video-id').textContent = youtubeLinkData.videoId;
console.log(youtubeLinkData.videoId);
player.cueVideoById(youtubeLinkData.videoId, youtubeLinkData.startTime || 0);
}
}
function isHitsterLink(url) {
// Regular expression to match with or without "http://" or "https://"
const regex = /^(?:http:\/\/|https:\/\/)?(www\.hitstergame|app\.hitsternordics)\.com\/.+/;
return regex.test(url);
}
// Example implementation for isYoutubeLink
function isYoutubeLink(url) {
return url.startsWith("https://www.youtube.com") || url.startsWith("https://youtu.be") || url.startsWith("https://music.youtube.com/");
}
// Example implementation for parseHitsterUrl
function parseHitsterUrl(url) {
const regex = /^(?:http:\/\/|https:\/\/)?www\.hitstergame\.com\/(.+?)\/(\d+)$/;
const match = url.match(regex);
if (match) {
// Hitster URL is in the format: https://www.hitstergame.com/{lang}/{id}
// lang can be things like "en", "de", "pt", etc., but also "de/aaaa0007"
const processedLang = match[1].replace(/\//g, "-");
return { lang: processedLang, id: match[2] };
}
const regex_nordics = /^(?:http:\/\/|https:\/\/)?app.hitster(nordics).com\/resources\/songs\/(\d+)$/;
const match_nordics = url.match(regex_nordics);
if (match_nordics) {
// Hitster URL can also be in the format: https://app.hitsternordics.com/resources/songs/{id}
return { lang: match_nordics[1], id: match_nordics[2] };
}
return null;
}
// Looks up the YouTube link in the CSV content based on the ID
function lookupYoutubeLink(id, csvContent) {
const headers = csvContent[0]; // Get the headers from the CSV content
const cardIndex = headers.indexOf('Card#');
const urlIndex = headers.indexOf('URL');
const targetId = parseInt(id, 10); // Convert the incoming ID to an integer
const lines = csvContent.slice(1); // Exclude the first row (headers) from the lines
if (cardIndex === -1 || urlIndex === -1) {
throw new Error('Card# or URL column not found');
}
for (let row of lines) {
const csvId = parseInt(row[cardIndex], 10);
if (csvId === targetId) {
return row[urlIndex].trim(); // Return the YouTube link
}
}
return null; // If no matching ID is found
}
// Could also use external library, but for simplicity, we'll define it here
function parseCSV(text) {
const lines = text.split('\n');
return lines.map(line => {
const result = [];
let startValueIdx = 0;
let inQuotes = false;
for (let i = 0; i < line.length; i++) {
if (line[i] === '"' && line[i-1] !== '\\') {
inQuotes = !inQuotes;
} else if (line[i] === ',' && !inQuotes) {
result.push(line.substring(startValueIdx, i).trim().replace(/^"(.*)"$/, '$1'));
startValueIdx = i + 1;
}
}
result.push(line.substring(startValueIdx).trim().replace(/^"(.*)"$/, '$1')); // Push the last value
return result;
});
}
async function getCachedCsv(url) {
if (!csvCache[url]) { // Check if the URL is not in the cache
console.log(`URL not cached, fetching CSV from URL: ${url}`);
const response = await fetch(url);
const data = await response.text();
csvCache[url] = parseCSV(data); // Cache the parsed CSV data using the URL as a key
}
return csvCache[url]; // Return the cached data for the URL
}
function parseYoutubeLink(url) {
// First, ensure that the URL is decoded (handles encoded URLs)
url = decodeURIComponent(url);
const regex = /^https?:\/\/(www\.youtube\.com\/watch\?v=|youtu\.be\/|music\.youtube\.com\/watch\?v=)(.{11}).*/;
const match = url.match(regex);
if (match) {
const queryParams = new URLSearchParams(match[4]); // Correctly capture and parse the query string part of the URL
const videoId = match[2];
let startTime = queryParams.get('start') || queryParams.get('t');
const endTime = queryParams.get('end');
// Normalize and parse 't' and 'start' parameters
startTime = normalizeTimeParameter(startTime);
const parsedEndTime = normalizeTimeParameter(endTime);
return { videoId, startTime, endTime: parsedEndTime };
}
return null;
}
function normalizeTimeParameter(timeValue) {
if (!timeValue) return null; // Return null if timeValue is falsy
// Handle time formats (e.g., 't=1m15s' or '75s')
let seconds = 0;
if (timeValue.endsWith('s')) {
seconds = parseInt(timeValue, 10);
} else {
// Additional parsing can be added here for 'm', 'h' formats if needed
seconds = parseInt(timeValue, 10);
}
return isNaN(seconds) ? null : seconds;
}
});
// This function creates an <iframe> (and YouTube player) after the API code downloads.
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '0',
width: '0',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
window.onYouTubeIframeAPIReady = onYouTubeIframeAPIReady;
// Load the YouTube IFrame API script
const tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
const firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
// The API will call this function when the video player is ready.
function onPlayerReady(event) {
// Cue a video using the videoId from the QR code (example videoId used here)
// player.cueVideoById('dQw4w9WgXcQ');
}
// Display video information when it's cued
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.CUED) {
document.getElementById('startstop-video').style.background = "green";
// Display title and duration
var videoData = player.getVideoData();
document.getElementById('video-title').textContent = videoData.title;
var duration = player.getDuration();
document.getElementById('video-duration').textContent = formatDuration(duration);
// Check for Autoplay
if (document.getElementById('autoplay').checked == true) {
document.getElementById('startstop-video').innerHTML = "Stop";
if (document.getElementById('randomplayback').checked == true) {
playVideoAtRandomStartTime();
}
else {
player.playVideo();
}
}
}
else if (event.data == YT.PlayerState.PLAYING) {
document.getElementById('startstop-video').style.background = "red";
}
else if (event.data == YT.PlayerState.PAUSED | event.data == YT.PlayerState.ENDED) {
document.getElementById('startstop-video').style.background = "green";
}
else if (event.data == YT.PlayerState.BUFFERING) {
document.getElementById('startstop-video').style.background = "orange";
}
}
// Helper function to format duration from seconds to a more readable format
function formatDuration(duration) {
var minutes = Math.floor(duration / 60);
var seconds = duration % 60;
return minutes + ":" + (seconds < 10 ? '0' : '') + seconds;
}
// Add event listeners to Play and Stop buttons
document.getElementById('startstop-video').addEventListener('click', function() {
if (this.innerHTML == "Play") {
this.innerHTML = "Stop";
if (document.getElementById('randomplayback').checked == true) {
playVideoAtRandomStartTime();
}
else {
player.playVideo();
}
}
else {
this.innerHTML = "Play";
player.pauseVideo();
}
});
function playVideoAtRandomStartTime() {
const minStartPercentage = 0.10;
const maxEndPercentage = 0.90;
let videoDuration = player.getDuration()
let startTime = player.getCurrentTime(); // If the video is already cued to a specific start time
playbackDuration = parseInt(document.getElementById('playback-duration').value, 10) || 30;
let endTime = startTime + playbackDuration;
// Adjust start and end time based on video duration
const minStartTime = Math.max(startTime, videoDuration * minStartPercentage);
const maxEndTime = videoDuration * maxEndPercentage;
// Ensure the video ends by 90% of its total duration
if (endTime > maxEndTime) {
endTime = maxEndTime;
startTime = Math.max(minStartTime, endTime - playbackDuration);
}
// If custom start time is 0 or very close to the beginning, pick a random start time within the range
if (startTime <= minStartTime) {
const range = maxEndTime - minStartTime - playbackDuration;
const randomOffset = Math.random() * range;
startTime = minStartTime + randomOffset;
endTime = startTime + playbackDuration;
}
// Cue video at calculated start time and play
console.log("play random", startTime, endTime)
player.seekTo(startTime, true);
player.playVideo();
clearTimeout(playbackTimer); // Clear any existing timer
// Schedule video stop after the specified duration
playbackTimer = setTimeout(() => {
player.pauseVideo();
document.getElementById('startstop-video').innerHTML = "Play";
}, (endTime - startTime) * 1000); // Convert to milliseconds
}
// Assuming you have an element with the ID 'qr-reader' for the QR scanner
document.getElementById('qr-reader').style.display = 'none'; // Initially hide the QR Scanner
document.getElementById('startScanButton').addEventListener('click', function() {
document.getElementById('cancelScanButton').style.display = 'block';
document.getElementById('qr-reader').style.display = 'block'; // Show the scanner
qrScanner.start().catch(err => {
console.error('Unable to start QR Scanner', err);
qrResult.textContent = "QR Scanner failed to start.";
});
qrScanner.start().then(() => {
qrScanner.setInversionMode('both'); // we want to scan also for Hitster QR codes which use inverted colors
});
});
document.getElementById('songinfo').addEventListener('click', function() {
var cb = document.getElementById('songinfo');
var videoid = document.getElementById('videoid');
var videotitle = document.getElementById('videotitle');
var videoduration = document.getElementById('videoduration');
if(cb.checked == true){
videoid.style.display = 'block';
videotitle.style.display = 'block';
videoduration.style.display = 'block';
} else {
videoid.style.display = 'none';
videotitle.style.display = 'none';
videoduration.style.display = 'none';
}
});
document.getElementById('cancelScanButton').addEventListener('click', function() {
qrScanner.stop(); // Stop scanning after a result is found
document.getElementById('qr-reader').style.display = 'none'; // Hide the scanner after successful scan
document.getElementById('cancelScanButton').style.display = 'none'; // Hide the cancel-button
});
document.getElementById('cb_settings').addEventListener('click', function() {
var cb = document.getElementById('cb_settings');
if (cb.checked == true) {
document.getElementById('settings_div').style.display = 'block';
}
else {
document.getElementById('settings_div').style.display = 'none';
}
});
document.getElementById('randomplayback').addEventListener('click', function() {
document.cookie = "RandomPlaybackChecked=" + this.checked + ";max-age=2592000"; //30 Tage
listCookies();
});
document.getElementById('autoplay').addEventListener('click', function() {
document.cookie = "autoplayChecked=" + this.checked + ";max-age=2592000"; //30 Tage
listCookies();
});
document.getElementById('cookies').addEventListener('click', function() {
var cb = document.getElementById('cookies');
if (cb.checked == true) {
document.getElementById('cookielist').style.display = 'block';
}
else {
document.getElementById('cookielist').style.display = 'none';
}
});
function listCookies() {
var result = document.cookie;
document.getElementById("cookielist").innerHTML=result;
}
function getCookieValue(name) {
const regex = new RegExp(`(^| )${name}=([^;]+)`);
const match = document.cookie.match(regex);
if (match) {
return match[2];
}
}
function getCookies() {
var isTrueSet;
if (getCookieValue("RandomPlaybackChecked") != "") {
isTrueSet = (getCookieValue("RandomPlaybackChecked") === 'true');
document.getElementById('randomplayback').checked = isTrueSet;
}
if (getCookieValue("autoplayChecked") != "") {
isTrueSet = (getCookieValue("autoplayChecked") === 'true');
document.getElementById('autoplay').checked = isTrueSet;
}
listCookies();
}
window.addEventListener("DOMContentLoaded", getCookies());