Skip to content

Commit 5759c49

Browse files
committed
feat: code by Trae
1 parent 7165289 commit 5759c49

1 file changed

Lines changed: 56 additions & 36 deletions

File tree

epub_browser/assets/library.js

Lines changed: 56 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -312,58 +312,78 @@ function initScript() {
312312
});
313313
}
314314

315-
// PWA 安装提示
315+
// PWA 安装提示和更新按钮
316316
let deferredPrompt;
317-
const installBtn = document.createElement('button');
317+
const readingControls = document.querySelector('.reading-controls');
318+
319+
// 安装应用按钮
320+
const installBtn = document.createElement('a');
318321
installBtn.id = 'pwa-install-btn';
319-
installBtn.innerHTML = '<i class="fas fa-download"></i> Install App';
320-
installBtn.style.cssText = 'position:fixed;bottom:20px;right:20px;padding:12px 20px;background:#4a90d9;color:#fff;border:none;border-radius:8px;cursor:pointer;font-size:14px;box-shadow:0 2px 10px rgba(0,0,0,0.2);z-index:9999;display:none;';
321-
document.body.appendChild(installBtn);
322+
installBtn.href = '#';
323+
installBtn.className = 'control-btn';
324+
installBtn.innerHTML = '<i class="fas fa-download"></i><div class="control-name">Install</div>';
325+
installBtn.style.display = 'none';
326+
if (readingControls) {
327+
readingControls.appendChild(installBtn);
328+
}
322329

323330
// 更新缓存按钮
324-
const updateCacheBtn = document.createElement('button');
331+
const updateCacheBtn = document.createElement('a');
325332
updateCacheBtn.id = 'update-cache-btn';
326-
updateCacheBtn.innerHTML = '<i class="fas fa-sync"></i> Update';
327-
updateCacheBtn.style.cssText = 'position:fixed;bottom:80px;right:20px;padding:12px 20px;background:#28a745;color:#fff;border:none;border-radius:8px;cursor:pointer;font-size:14px;box-shadow:0 2px 10px rgba(0,0,0,0.2);z-index:9999;';
328-
document.body.appendChild(updateCacheBtn);
333+
updateCacheBtn.href = '#';
334+
updateCacheBtn.className = 'control-btn';
335+
updateCacheBtn.innerHTML = '<i class="fas fa-sync"></i><div class="control-name">Update</div>';
336+
if (readingControls) {
337+
readingControls.appendChild(updateCacheBtn);
338+
}
329339

330340
window.addEventListener('beforeinstallprompt', function(e) {
331341
e.preventDefault();
332342
deferredPrompt = e;
333-
installBtn.style.display = 'block';
343+
if (installBtn) {
344+
installBtn.style.display = 'block';
345+
}
334346
console.log('PWA install prompt captured');
335347
});
336348

337-
installBtn.addEventListener('click', function() {
338-
if (deferredPrompt) {
339-
installBtn.style.display = 'none';
340-
deferredPrompt.prompt();
341-
deferredPrompt.userChoice.then(function(choiceResult) {
342-
if (choiceResult.outcome === 'accepted') {
343-
console.log('User accepted the install prompt');
344-
} else {
345-
console.log('User dismissed the install prompt');
346-
}
347-
deferredPrompt = null;
348-
});
349-
}
350-
});
349+
if (installBtn) {
350+
installBtn.addEventListener('click', function(e) {
351+
e.preventDefault();
352+
if (deferredPrompt) {
353+
installBtn.style.display = 'none';
354+
deferredPrompt.prompt();
355+
deferredPrompt.userChoice.then(function(choiceResult) {
356+
if (choiceResult.outcome === 'accepted') {
357+
console.log('User accepted the install prompt');
358+
} else {
359+
console.log('User dismissed the install prompt');
360+
}
361+
deferredPrompt = null;
362+
});
363+
}
364+
});
365+
}
351366

352-
updateCacheBtn.addEventListener('click', function() {
353-
if ('serviceWorker' in navigator && navigator.serviceWorker.controller) {
354-
// 发送消息给 Service Worker 清除缓存
355-
navigator.serviceWorker.controller.postMessage({ action: 'CLEAR_CACHE' });
356-
showNotification('Cache cleared, page will reload...', 'info');
357-
setTimeout(function() {
367+
if (updateCacheBtn) {
368+
updateCacheBtn.addEventListener('click', function(e) {
369+
e.preventDefault();
370+
if ('serviceWorker' in navigator && navigator.serviceWorker.controller) {
371+
// 发送消息给 Service Worker 清除缓存
372+
navigator.serviceWorker.controller.postMessage({ action: 'CLEAR_CACHE' });
373+
showNotification('Cache cleared, page will reload...', 'info');
374+
setTimeout(function() {
375+
location.reload();
376+
}, 1000);
377+
} else {
358378
location.reload();
359-
}, 1000);
360-
} else {
361-
location.reload();
362-
}
363-
});
379+
}
380+
});
381+
}
364382

365383
window.addEventListener('appinstalled', function() {
366-
installBtn.style.display = 'none';
384+
if (installBtn) {
385+
installBtn.style.display = 'none';
386+
}
367387
console.log('PWA installed successfully');
368388
});
369389

0 commit comments

Comments
 (0)