-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
267 lines (236 loc) · 8.27 KB
/
Copy pathscript.js
File metadata and controls
267 lines (236 loc) · 8.27 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
const kelimeInput = document.getElementById('kelime-input');
const anlamInput = document.getElementById('anlam-input');
const ekleBtn = document.getElementById('ekle-btn');
const videoEkleBtn = document.getElementById('video-ekle-btn');
const videoContainer = document.getElementById('video-container');
const textArea = document.getElementById('text-area');
const addTextBtn = document.getElementById('add-text-btn');
const deleteTextBtn = document.getElementById('delete-text-btn');
const textViewer = document.getElementById('text-viewer');
const anlamGosterDiv = document.getElementById('anlam-goster');
const API_BASE = 'http://localhost:3000/api/transcript';
// Kelimeler dizisi (localStorage'dan yükle)
let kelimeler = [];
if (localStorage.getItem('kelimeler')) {
try {
kelimeler = JSON.parse(localStorage.getItem('kelimeler')) || [];
} catch (e) {
kelimeler = [];
}
}
// Video URL'sini localStorage'dan yükle
let videoUrl = localStorage.getItem('videoUrl') || '';
if (videoUrl) {
const videoId = getYouTubeVideoId(videoUrl);
if (videoId) {
loadVideo(videoId);
}
}
// Tüm metni localStorage'da sakla
let allLines = [];
if (localStorage.getItem('allLines')) {
try {
allLines = JSON.parse(localStorage.getItem('allLines')) || [];
} catch (e) {
allLines = [];
}
}
function kaydetKelimeler() {
localStorage.setItem('kelimeler', JSON.stringify(kelimeler));
}
function kelimeleriVurgula() {
// Tüm metni göster (sayfalama yok)
let lines = allLines;
// Uzunluktan kısaya sırala (çakışma önle)
const kelimeList = kelimeler.map(obj => obj.kelime).sort((a, b) => b.length - a.length);
let html = lines.map(line => {
let l = line;
for (const kelime of kelimeList) {
// Büyük/küçük harf duyarsız vurgulama
const regex = new RegExp(`\\b${kelime}\\b`, 'gi');
l = l.replace(regex, `<span class=\"lgt_kelime\">$&</span>`);
}
return l;
}).join('<br>');
textViewer.innerHTML = html;
anlamGosterHazirla();
}
function anlamGosterHazirla() {
const bar = document.getElementById('anlam-goster-bar');
document.querySelectorAll('.lgt_kelime').forEach(el => {
el.onclick = function(e) {
e.stopPropagation();
const kelime = el.textContent.trim().toLowerCase();
const kayit = kelimeler.find(obj => obj.kelime.toLowerCase() === kelime);
if (bar.textContent === (kayit ? kayit.anlam : '')) {
bar.textContent = '';
return;
}
bar.textContent = kayit ? kayit.anlam : '';
};
});
}
function sozlukListesiniGuncelle() {
const kelimeListesiDiv = document.getElementById('kelime-listesi');
kelimeListesiDiv.innerHTML = '';
// Alfabetik sırala
const sirali = [...kelimeler].sort((a, b) => a.kelime.localeCompare(b.kelime, 'tr'));
sirali.forEach((item, idx) => {
const div = document.createElement('div');
div.className = 'kelime-item';
div.innerHTML = `<span class="kelime-adi">${item.kelime}</span><span class="kelime-anlam">${item.anlam}</span>`;
const silBtn = document.createElement('button');
silBtn.className = 'sil-btn';
silBtn.textContent = 'Delete';
// Orijinal dizideki indexi bul
const realIdx = kelimeler.findIndex(obj => obj.kelime === item.kelime);
silBtn.onclick = function() {
kelimeler.splice(realIdx, 1);
kaydetKelimeler();
kelimeleriVurgula();
sozlukListesiniGuncelle();
};
div.appendChild(silBtn);
kelimeListesiDiv.appendChild(div);
});
}
/**
* YouTube linkinden video ID'sini çeken yardımcı fonksiyon
*/
function getYouTubeVideoId(url) {
const regExp = /^.*(?:youtu\.be\/|v\/|u\/\w\/|embed\/|watch\?v=|&v=)([^#&?]{11}).*/;
const match = url.match(regExp);
return match ? match[1] : null;
}
/**
* Express API'den altyazıyı çek
*/
async function fetchTranscript(videoId) {
try {
const res = await fetch(`${API_BASE}/${videoId}`);
const json = await res.json();
if (json.success && json.text) {
return json.text;
}
throw new Error(json.error || 'No captions found');
} catch (err) {
console.error('Transcript fetch error:', err);
return null;
}
}
/**
* Video yükleme fonksiyonu
*/
function loadVideo(videoId) {
const iframe = document.createElement('iframe');
iframe.width = '100%';
iframe.height = '100%';
iframe.src = `https://www.youtube-nocookie.com/embed/${videoId}?rel=0&showinfo=0&origin=${encodeURIComponent(window.location.origin)}`;
iframe.frameBorder = '0';
iframe.allow = 'accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture';
iframe.allowFullscreen = true;
videoContainer.innerHTML = '';
videoContainer.appendChild(iframe);
}
// Video Ekleme
videoEkleBtn.onclick = function() {
const url = prompt('Enter YouTube video URL:');
if (!url) return;
const videoId = getYouTubeVideoId(url);
if (!videoId) {
alert('Invalid YouTube URL!');
return;
}
videoUrl = url;
localStorage.setItem('videoUrl', url);
// Videoyu yükle
loadVideo(videoId);
};
function saveAllLines() {
localStorage.setItem('allLines', JSON.stringify(allLines));
}
// Add Text butonu
addTextBtn.onclick = function() {
// Modalı aç
const modal = new bootstrap.Modal(document.getElementById('addTextModal'));
document.getElementById('modal-textarea').value = '';
modal.show();
};
// Modal Kaydet butonu
const modalSaveBtn = document.getElementById('modal-save-btn');
modalSaveBtn.onclick = function() {
const textarea = document.getElementById('modal-textarea');
const text = textarea.value.trim();
if (text) {
allLines.push(...text.split(/\r?\n/));
saveAllLines();
kelimeleriVurgula();
}
// Modalı kapat
const modal = bootstrap.Modal.getInstance(document.getElementById('addTextModal'));
modal.hide();
};
// Modal kapatıldığında textarea temizle
const addTextModal = document.getElementById('addTextModal');
addTextModal.addEventListener('hidden.bs.modal', function () {
document.getElementById('modal-textarea').value = '';
});
// Delete Text butonu
deleteTextBtn.onclick = function() {
if (confirm('Are you sure you want to delete all text?')) {
allLines = [];
saveAllLines();
kelimeleriVurgula();
}
};
// Kelime Ekleme
ekleBtn.onclick = function() {
const kelime = kelimeInput.value.trim();
const anlam = anlamInput.value.trim();
if (!kelime || !anlam) return;
const idx = kelimeler.findIndex(obj => obj.kelime === kelime);
if (idx !== -1) {
kelimeler[idx].anlam = anlam;
} else {
kelimeler.push({kelime, anlam});
}
kaydetKelimeler();
kelimeleriVurgula();
sozlukListesiniGuncelle();
kelimeInput.value = '';
anlamInput.value = '';
kelimeInput.focus();
};
// Sayfa yüklendiğinde
window.addEventListener('DOMContentLoaded', () => {
kelimeleriVurgula();
sozlukListesiniGuncelle();
});
// Test kodu - JavaScript'in çalışıp çalışmadığını kontrol et
const ta = document.getElementById('text-area');
if (!ta) {
console.error('Textarea bulunamadı!');
} else {
ta.value = 'JavaScript çalışıyor!';
console.log('Textarea bulundu ve JavaScript çalışıyor!');
}
// Theme Toggle
const themeToggle = document.getElementById('theme-toggle');
const themeIcon = themeToggle.querySelector('i');
// Check for saved theme preference
const savedTheme = localStorage.getItem('theme') || 'dark';
document.body.classList.add(`${savedTheme}-theme`);
updateThemeIcon(savedTheme);
themeToggle.addEventListener('click', () => {
const isDarkTheme = document.body.classList.contains('dark-theme');
const newTheme = isDarkTheme ? 'light' : 'dark';
// Remove both theme classes first
document.body.classList.remove('dark-theme', 'light-theme');
// Add the new theme class
document.body.classList.add(`${newTheme}-theme`);
localStorage.setItem('theme', newTheme);
updateThemeIcon(newTheme);
});
function updateThemeIcon(theme) {
themeIcon.className = theme === 'dark' ? 'bi bi-sun-fill' : 'bi bi-moon-fill';
}