-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
107 lines (95 loc) · 3.39 KB
/
app.js
File metadata and controls
107 lines (95 loc) · 3.39 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
const taskInput = document.getElementById('task-input');
const addTaskButton = document.getElementById('add-task');
const pushNotificationButton = document.getElementById('push-notification');
const taskList = document.getElementById('task-list');
// Load tasks from local storage
const loadTasks = () => {
const tasks = JSON.parse(localStorage.getItem('tasks')) || [];
tasks.forEach(task => {
addTaskToList(task);
});
};
// Add task to the list
const addTaskToList = (task) => {
const li = document.createElement('li');
li.innerHTML = `${task} <button class="delete-task">Delete</button>`;
taskList.appendChild(li);
li.querySelector('.delete-task').addEventListener('click', () => {
li.remove();
deleteTask(task);
});
};
// Add task event listener
addTaskButton.addEventListener('click', () => {
const task = taskInput.value.trim();
if (task) {
addTaskToList(task);
saveTask(task);
taskInput.value = '';
}
});
// Save task to local storage
const saveTask = (task) => {
const tasks = JSON.parse(localStorage.getItem('tasks')) || [];
tasks.push(task);
localStorage.setItem('tasks', JSON.stringify(tasks));
};
// Delete task from local storage
const deleteTask = (task) => {
const tasks = JSON.parse(localStorage.getItem('tasks')) || [];
const updatedTasks = tasks.filter(t => t !== task);
localStorage.setItem('tasks', JSON.stringify(updatedTasks));
};
// Initialize application
loadTasks();
// Register service worker
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('service-worker.js')
.then(() => console.log('Service Worker Registered'))
.catch(error => console.error('Service Worker Registration Failed:', error));
}
// Request Notification Permission
if ('Notification' in window && 'serviceWorker' in navigator) {
Notification.requestPermission().then(permission => {
if (permission === 'granted') {
console.log('Notification permission granted.');
} else {
console.log('Notification permission denied.');
}
});
}
// Send Push Notification
pushNotificationButton.addEventListener('click', () => {
navigator.serviceWorker.ready.then(registration => {
registration.showNotification('New Task Added!', {
body: 'Check out your to-do list!',
icon: 'icon.png'
});
});
});
// Background Sync
if ('serviceWorker' in navigator && 'SyncManager' in window) {
navigator.serviceWorker.ready.then(registration => {
document.addEventListener('offline', () => {
console.log('You are offline. Changes will sync when you are back online.');
});
document.addEventListener('online', () => {
registration.sync.register('sync-tasks')
.then(() => console.log('Sync registered.'))
.catch(error => console.error('Sync registration failed:', error));
});
});
}
// Lazy Loading of Images
const lazyImages = document.querySelectorAll('img.lazy');
const observer = new IntersectionObserver(entries => {
entries.forEach(entry => {
if (entry.isIntersecting) {
const img = entry.target;
img.src = img.dataset.src;
img.onload = () => img.classList.add('lazy-loaded');
observer.unobserve(img);
}
});
});
lazyImages.forEach(img => observer.observe(img));