-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathsw.js
More file actions
129 lines (114 loc) · 3.91 KB
/
sw.js
File metadata and controls
129 lines (114 loc) · 3.91 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
// XSSNow Service Worker - PWA Functionality
const CACHE_NAME = 'xssnow-v1.0.0';
const STATIC_CACHE_URLS = [
'/',
'/index.html',
'/payloads.html',
'/xss-payload-generator.html',
'/contributors.html',
'/docs.html',
'/404.html',
'/manifest.json',
'/css/styles.css',
'/css/animations.css',
'/css/components.css',
'/js/app.js',
'/js/animations.js',
'/assets/ninja.svg'
];
// Install event - cache static assets
self.addEventListener('install', event => {
console.log('XSSNow SW: Installing...');
event.waitUntil(
caches.open(CACHE_NAME)
.then(cache => {
console.log('XSSNow SW: Caching static assets');
return cache.addAll(STATIC_CACHE_URLS);
})
.then(() => self.skipWaiting())
);
});
// Activate event - clean up old caches
self.addEventListener('activate', event => {
console.log('XSSNow SW: Activating...');
event.waitUntil(
caches.keys()
.then(cacheNames => {
return Promise.all(
cacheNames
.filter(cacheName => cacheName !== CACHE_NAME)
.map(cacheName => caches.delete(cacheName))
);
})
.then(() => self.clients.claim())
);
});
// Fetch event - serve from cache, fallback to network
self.addEventListener('fetch', event => {
// Skip non-GET requests
if (event.request.method !== 'GET') return;
// Skip external domains
if (!event.request.url.startsWith(self.location.origin)) return;
event.respondWith(
caches.match(event.request)
.then(cachedResponse => {
if (cachedResponse) {
return cachedResponse;
}
return fetch(event.request)
.then(response => {
// Don't cache non-successful responses
if (!response || response.status !== 200 || response.type !== 'basic') {
return response;
}
// Clone response for caching
const responseToCache = response.clone();
// Cache dynamic content with shorter TTL
caches.open(CACHE_NAME)
.then(cache => {
cache.put(event.request, responseToCache);
});
return response;
})
.catch(() => {
// Fallback to 404 page for navigation requests
if (event.request.destination === 'document') {
return caches.match('/404.html');
}
});
})
);
});
// Background sync for offline payload submissions (future feature)
self.addEventListener('sync', event => {
if (event.tag === 'background-sync') {
console.log('XSSNow SW: Background sync triggered');
// Future: Handle offline payload submissions
}
});
// Push notifications (future feature for new payloads)
self.addEventListener('push', event => {
if (event.data) {
const data = event.data.json();
console.log('XSSNow SW: Push received', data);
const options = {
body: data.body || 'New XSS payloads available!',
icon: '/assets/ninja.svg',
badge: '/assets/ninja.svg',
vibrate: [100, 50, 100],
data: {
url: data.url || '/'
}
};
event.waitUntil(
self.registration.showNotification(data.title || 'XSSNow', options)
);
}
});
// Handle notification clicks
self.addEventListener('notificationclick', event => {
event.notification.close();
event.waitUntil(
clients.openWindow(event.notification.data.url || '/')
);
});