-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
178 lines (149 loc) · 4.83 KB
/
script.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
const subtitleText = document.getElementById('subtitleText');
const subtitleFileInput = document.getElementById('subtitleFile');
const playButton = document.getElementById('playButton');
const seekBar = document.getElementById('seekBar');
const currentTimeDisplay = document.getElementById('currentTime');
const fullscreenButton = document.getElementById('fullscreenButton');
const seekAdd = document.getElementById('seekButtonAdd');
const seekSubtract = document.getElementById('seekButtonSubtract');
let subtitles = [];
let currentSubtitleIndex = 0;
let isPlaying = false;
let currentTime = 0;
let timerId;
subtitleFileInput.addEventListener('change', handleFileSelect);
playButton.addEventListener('click', play);
seekBar.addEventListener('input', seek);
fullscreenButton.addEventListener('click', toggleFullscreen);
seekButtonAdd.addEventListener('click', () => {
currentTime += 10000;
})
seekButtonSubtract.addEventListener('click', () => {
currentTime -= 10000;
});
document.addEventListener("fullscreenchange", (event) => {
fullscreen = !fullscreen;
if(fullscreen){
fullscreenButton.style.visibility = 'hidden';
}
else{
fullscreenButton.style.visibility = 'visible';
}
});
let fullscreen = false;
var noSleep = new NoSleep();
function toggleFullscreen() {
const container = document.querySelector('.container');
if(!fullscreen){
noSleep.enable();
if (container.requestFullscreen) {
container.requestFullscreen();
} else if (container.mozRequestFullScreen) {
container.mozRequestFullScreen();
} else if (container.webkitRequestFullscreen) {
container.webkitRequestFullscreen();
} else if (container.msRequestFullscreen) {
container.msRequestFullscreen();
}
fullscreenButton.style.visibility = 'hidden';
}
}
function handleFileSelect(event) {
const file = event.target.files[0];
if (!file) return;
const reader = new FileReader();
reader.onload = function (e) {
parseSubtitle(e.target.result);
};
reader.readAsText(file);
}
function parseSubtitle(data) {
subtitles = [];
const lines = data.split(/\r?\n/);
let index = 0;
while (index < lines.length) {
if (lines[index].trim() === '') {
index++;
continue;
}
const id = parseInt(lines[index++]);
const timeString = lines[index++];
let text = '';
// Concatenate multiline text
while (lines[index] && lines[index].trim() !== '') {
text += lines[index++] + ' ';
}
index++; // Skip the empty line after the text
const [startTime, endTime] = timeString.split(' --> ').map(parseTime);
subtitles.push({ id, startTime, endTime, text });
}
seekBar.max = subtitles[subtitles.length - 1].endTime;
document.getElementById('filePicker').style.display = 'none';
currentTime = subtitles[0].startTime;
updateTime();
}
function parseTime(timeString) {
const parts = timeString.split(':');
const lastPart = parts[2].split(',');
const hours = parseInt(parts[0]);
const minutes = parseInt(parts[1]);
const seconds = parseInt(lastPart[0]);
const milliseconds = parseInt(lastPart[1]);
return hours * 3600000 + minutes * 60000 + seconds * 1000 + milliseconds;
}
function play() {
if (isPlaying){
isPlaying = false;
playButton.className = 'play-button'
clearInterval(timerId);
}
else{
playButton.className = 'pause-button'
isPlaying = true;
timerId = setInterval(updateTime, 100);
}
}
function updateTime() {
currentTime += 100; // Simulate 100ms of video playback time
currentTimeDisplay.textContent = formatTime(currentTime);
const subtitle = subtitles.find(sub => currentTime >= sub.startTime && currentTime < sub.endTime);
if (subtitle) {
subtitleText.innerHTML = subtitle.text;
} else {
subtitleText.innerHTML = '';
}
// Update seek bar position
seekBar.value = currentTime;
}
function formatTime(time) {
const milliseconds = time % 1000;
time = (time - milliseconds) / 1000;
const seconds = time % 60;
time = (time - seconds) / 60;
const minutes = time % 60;
const hours = (time - minutes) / 60;
return `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
}
function seek() {
currentTime = parseFloat(seekBar.value);
currentTimeDisplay.textContent = formatTime(currentTime);
}
function closeDialog() {
const dialog = document.getElementById('myDialog');
dialog.close();
}
function checkAndShowDialog() {
const dialogShownBefore = localStorage.getItem('dialogShown');
if (!dialogShownBefore) {
const dialog = document.getElementById('myDialog');
dialog.showModal();
localStorage.setItem('dialogShown', 'true');
}
}
// Function to close the dialog
function closeDialog() {
const dialog = document.getElementById('myDialog');
dialog.close();
}
// Show the dialog on initial load if not shown before
window.onload = checkAndShowDialog;