Update app.js
This commit is contained in:
parent
2c04d69edb
commit
aca4aadf91
1 changed files with 65 additions and 53 deletions
110
js/app.js
110
js/app.js
|
|
@ -397,63 +397,75 @@ document.addEventListener('DOMContentLoaded', async () => {
|
||||||
|
|
||||||
updateLastFMUI();
|
updateLastFMUI();
|
||||||
|
|
||||||
lastfmConnectBtn?.addEventListener('click', async () => {
|
lastfmConnectBtn?.addEventListener('click', async () => {
|
||||||
if (scrobbler.isAuthenticated()) {
|
if (scrobbler.isAuthenticated()) {
|
||||||
if (confirm('Disconnect from Last.fm?')) {
|
if (confirm('Disconnect from Last.fm?')) {
|
||||||
scrobbler.disconnect();
|
scrobbler.disconnect();
|
||||||
updateLastFMUI();
|
updateLastFMUI();
|
||||||
}
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
const authWindow = window.open('', '_blank');
|
||||||
|
|
||||||
|
lastfmConnectBtn.disabled = true;
|
||||||
|
lastfmConnectBtn.textContent = 'Opening Last.fm...';
|
||||||
|
|
||||||
|
try {
|
||||||
|
const { token, url } = await scrobbler.getAuthUrl();
|
||||||
|
|
||||||
|
if (authWindow) {
|
||||||
|
authWindow.location.href = url;
|
||||||
} else {
|
} else {
|
||||||
try {
|
alert('Popup blocked! Please allow popups.');
|
||||||
lastfmConnectBtn.disabled = true;
|
lastfmConnectBtn.textContent = 'Connect Last.fm';
|
||||||
lastfmConnectBtn.textContent = 'Opening Last.fm...';
|
lastfmConnectBtn.disabled = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const { token, url } = await scrobbler.getAuthUrl();
|
lastfmConnectBtn.textContent = 'Waiting for authorization...';
|
||||||
|
|
||||||
const authWindow = window.open(url, '_blank');
|
let attempts = 0;
|
||||||
|
const maxAttempts = 30;
|
||||||
|
|
||||||
lastfmConnectBtn.textContent = 'Waiting for authorization...';
|
const checkAuth = setInterval(async () => {
|
||||||
|
attempts++;
|
||||||
|
|
||||||
let attempts = 0;
|
if (attempts > maxAttempts) {
|
||||||
const maxAttempts = 30;
|
clearInterval(checkAuth);
|
||||||
|
|
||||||
const checkAuth = setInterval(async () => {
|
|
||||||
attempts++;
|
|
||||||
|
|
||||||
if (attempts > maxAttempts) {
|
|
||||||
clearInterval(checkAuth);
|
|
||||||
lastfmConnectBtn.textContent = 'Connect Last.fm';
|
|
||||||
lastfmConnectBtn.disabled = false;
|
|
||||||
alert('Authorization timed out. Please try again.');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
const result = await scrobbler.completeAuthentication(token);
|
|
||||||
|
|
||||||
if (result.success) {
|
|
||||||
clearInterval(checkAuth);
|
|
||||||
if (authWindow && !authWindow.closed) {
|
|
||||||
authWindow.close();
|
|
||||||
}
|
|
||||||
updateLastFMUI();
|
|
||||||
lastfmConnectBtn.disabled = false;
|
|
||||||
lastFMStorage.setEnabled(true);
|
|
||||||
lastfmToggle.checked = true;
|
|
||||||
alert(`Successfully connected to Last.fm as ${result.username}!`);
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
}
|
|
||||||
}, 2000);
|
|
||||||
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Last.fm connection failed:', error);
|
|
||||||
alert('Failed to connect to Last.fm: ' + error.message);
|
|
||||||
lastfmConnectBtn.textContent = 'Connect Last.fm';
|
lastfmConnectBtn.textContent = 'Connect Last.fm';
|
||||||
lastfmConnectBtn.disabled = false;
|
lastfmConnectBtn.disabled = false;
|
||||||
|
if (authWindow && !authWindow.closed) authWindow.close();
|
||||||
|
alert('Authorization timed out. Please try again.');
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
});
|
try {
|
||||||
|
const result = await scrobbler.completeAuthentication(token);
|
||||||
|
|
||||||
|
if (result.success) {
|
||||||
|
clearInterval(checkAuth);
|
||||||
|
if (authWindow && !authWindow.closed) authWindow.close();
|
||||||
|
updateLastFMUI();
|
||||||
|
lastfmConnectBtn.disabled = false;
|
||||||
|
lastFMStorage.setEnabled(true);
|
||||||
|
lastfmToggle.checked = true;
|
||||||
|
alert(`Successfully connected to Last.fm as ${result.username}!`);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
}
|
||||||
|
}, 2000);
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Last.fm connection failed:', error);
|
||||||
|
alert('Failed to connect to Last.fm: ' + error.message);
|
||||||
|
lastfmConnectBtn.textContent = 'Connect Last.fm';
|
||||||
|
lastfmConnectBtn.disabled = false;
|
||||||
|
if (authWindow && !authWindow.closed) authWindow.close();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
lastfmToggle?.addEventListener('change', (e) => {
|
lastfmToggle?.addEventListener('change', (e) => {
|
||||||
lastFMStorage.setEnabled(e.target.checked);
|
lastFMStorage.setEnabled(e.target.checked);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue