-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
157 lines (135 loc) · 5.38 KB
/
script.js
File metadata and controls
157 lines (135 loc) · 5.38 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
import { audioCache, loadAudioCache, removeFromCache, clearAudioCache, setCurrentTrack } from './src/audioCache.js';
import { loadAudioFromCache, setupMediaSessionHandlers } from './src/audioPlayer.js';
import { fetchMp3 } from './src/api.js';
import { checkOnlineStatus, handleSharedUrl } from './src/utils.js';
document.addEventListener("DOMContentLoaded", async function() {
const audioPlayer = document.getElementById('player');
const playButton = document.getElementById('playButton');
const skipBackwardButton = document.getElementById('skipBackward');
const skipForwardButton = document.getElementById('skipForward');
const settingsBtn = document.getElementById('settingsBtn');
const settingsModal = document.getElementById('settingsModal');
const closeBtn = document.querySelector('.close');
const saveSettingsBtn = document.getElementById('saveSettings');
const apiKeyInput = document.getElementById('apiKey');
const toggleApiKeyBtn = document.getElementById('toggleApiKey');
const apiServerInput = document.getElementById('apiServer');
const historyList = document.getElementById('historyList');
const clearHistoryBtn = document.getElementById('clearHistory');
let originalApiKey = '';
let originalApiServer = '';
//checkOnlineStatus();
// window.addEventListener('online', () => {
// alert('You are back online!');
// updateHistoryList();
// });
// window.addEventListener('offline', () => {
// alert('You are offline. Some features may be limited.');
// });
// Load saved settings
const savedApiKey = localStorage.getItem('openaiApiKey');
const savedApiServer = localStorage.getItem('apiServer') || DEFAULT_API_SERVER;
if (savedApiKey) {
apiKeyInput.value = savedApiKey;
originalApiKey = savedApiKey;
}
apiServerInput.value = savedApiServer;
originalApiServer = savedApiServer;
// Toggle API key visibility
toggleApiKeyBtn.onclick = function() {
if (apiKeyInput.type === "password") {
apiKeyInput.type = "text";
toggleApiKeyBtn.textContent = "🔒";
} else {
apiKeyInput.type = "password";
toggleApiKeyBtn.textContent = "👁️";
}
}
// Settings modal functionality
settingsBtn.onclick = function() {
originalApiKey = apiKeyInput.value;
originalApiServer = apiServerInput.value;
settingsModal.style.display = "block";
apiKeyInput.focus();
}
function closeModal() {
settingsModal.style.display = "none";
apiKeyInput.value = originalApiKey;
}
closeBtn.onclick = closeModal;
window.onclick = function(event) {
if (event.target == settingsModal) {
closeModal();
}
}
document.addEventListener('keydown', function(event) {
if (settingsModal.style.display === "block") {
if (event.key === "Escape") {
closeModal();
} else if (event.key === "Enter") {
saveSettings();
}
}
});
function saveSettings() {
const apiKey = apiKeyInput.value.trim();
const apiServer = apiServerInput.value.trim();
if (apiKey && apiServer) {
localStorage.setItem('openaiApiKey', apiKey);
localStorage.setItem('apiServer', apiServer);
originalApiKey = apiKey;
originalApiServer = apiServer;
alert('Settings saved successfully!');
closeModal();
} else {
alert('Please enter both a valid API key and API server.');
}
}
saveSettingsBtn.onclick = saveSettings;
// Load audio cache and update history list
await loadAudioCache();
updateHistoryList();
function updateHistoryList() {
historyList.innerHTML = '';
Object.keys(audioCache).forEach(link => {
const li = document.createElement('li');
const playBtn = document.createElement('button');
playBtn.textContent = 'Play';
playBtn.onclick = () => {
loadAudioFromCache(link);
setCurrentTrack(link);
};
const removeBtn = document.createElement('button');
removeBtn.textContent = 'Remove';
removeBtn.onclick = () => {
removeFromCache(link);
updateHistoryList();
};
li.appendChild(document.createTextNode(link + ' '));
li.appendChild(playBtn);
li.appendChild(removeBtn);
historyList.appendChild(li);
});
}
clearHistoryBtn.onclick = async function() {
await clearAudioCache();
updateHistoryList();
};
// Save current position every 5 seconds
setInterval(() => {
if (typeof currentTrack !== 'undefined' && currentTrack && audioPlayer.currentTime > 0) {
audioCache[currentTrack].lastPosition = audioPlayer.currentTime;
localStorage.setItem('audioCache', JSON.stringify(audioCache));
}
}, 5000);
// Handle shared URL
const sharedLink = handleSharedUrl();
if (sharedLink) {
console.log('Valid URL provided, calling fetchMp3');
fetchMp3(sharedLink);
} else {
console.log("No URL provided. Waiting for user input.");
}
// Set up media session handlers
setupMediaSessionHandlers(audioPlayer, playButton);
});