-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoffline.js
More file actions
96 lines (82 loc) · 3.1 KB
/
offline.js
File metadata and controls
96 lines (82 loc) · 3.1 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
const OFFLINE_BANNER_ID = 'offline-banner';
let __mostudy_last_offline_log = 0; // prevent duplicate online/offline console spam
function getOfflineMessage() {
const hour = new Date().getHours();
const messages = [
'You are offline. Some features may be unavailable.',
'Offline mode activated. Using cached content.',
'No internet connection. Switching to offline mode.',
'Working offline with locally cached data.'
];
return messages[Math.floor(Math.random() * messages.length)];
}
function updateOfflineBanner() {
const banner = document.getElementById(OFFLINE_BANNER_ID);
if (!banner) return;
const isOffline = !navigator.onLine;
if (isOffline) {
// Show banner
banner.textContent = getOfflineMessage();
banner.classList.add('is-visible');
// Add a subtle visual indicator to the header if it exists
const header = document.querySelector('header');
if (header) {
header.style.filter = 'brightness(0.98)';
header.setAttribute('data-offline', 'true');
}
// Log offline status (debounced to avoid spam)
if (Date.now() - __mostudy_last_offline_log > 2000) {
console.log('[MoStudy] Offline mode enabled');
__mostudy_last_offline_log = Date.now();
}
} else {
// Hide banner with animation
banner.classList.remove('is-visible');
// Remove header dimming
const header = document.querySelector('header');
if (header) {
header.style.filter = '';
header.removeAttribute('data-offline');
}
// Log online status (debounced to avoid repeated messages)
if (Date.now() - __mostudy_last_offline_log > 2000) {
console.log('[MoStudy] Back online');
__mostudy_last_offline_log = Date.now();
}
}
}
async function registerServiceWorker() {
if (!('serviceWorker' in navigator)) return;
try {
const registration = await navigator.serviceWorker.register('/service-worker.js', { scope: '/' });
console.log('[MoStudy] Service worker registered successfully');
// Check for updates periodically
setInterval(() => {
registration.update();
}, 60000); // Check every minute
} catch (error) {
console.warn('[MoStudy] Service worker registration failed', error);
}
}
// Listen for online/offline events
window.addEventListener('online', () => {
updateOfflineBanner();
// Optional: You could refresh data here when coming back online
});
window.addEventListener('offline', updateOfflineBanner);
// Handle visibility changes (app comes to foreground)
document.addEventListener('visibilitychange', () => {
if (!document.hidden) {
updateOfflineBanner();
}
});
// Initialize on page load
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', () => {
updateOfflineBanner();
registerServiceWorker();
});
} else {
updateOfflineBanner();
registerServiceWorker();
}