-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
99 lines (80 loc) · 3.25 KB
/
Copy pathsw.js
File metadata and controls
99 lines (80 loc) · 3.25 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
// if you are using chrome verson 41 or older, then you have to include this file.
// As still, most of the browser does not support the CACHE Api yet.
console.log("i am in");
var Config = {
CACHE_VERSION : "testApp",
CACHE_FILES : [],
NOTIFICATION_TITLE : 'I Was running in the background',
OPTIONS :{
body: "Notification from AGWeb.",
icon: 'http://wfarm3.dataknet.com/static/resources/icons/set57/dbba6d94.png'
}
};
// Install Service Worker First
// A callBack has been passed whenever install event will be called, for handelling the event
// Remember, Even if a small bit of code gets changed, this event will be called again
self.addEventListener("install",function(e){
console.log(e,"waiting");
e.waitUntil(
caches.open(Config.CACHE_VERSION).then(function(cache){
return cache.addAll(Config.CACHE_FILES);
})
);
});
// Fetch request gets triggered on each of the network request, Service worker intercept
// each and every network request going from the browser for that application
self.addEventListener("fetch",function(e){
console.log("Going to fetch", e.request.url);
e.respondWith(
// If request got matched with that of cache, then it will show that, otherwise fetch it from the network
caches.match(e.request).then(function (cachedResponse) {
var fetchRequest = e.request.clone();
return fetch(fetchRequest).then(function (response) {
console.log(response.type);
// Check if we received a valid response
if(!response || e.request.url === "chrome-extension://gppongmhjkpfnbhagpmjfkannfbllamg/js/inject.js") {
return response;
}
//clone the response
var responseToCache = response.clone()
caches.open(Config.CACHE_VERSION).then(function (cache) {
cache.put(e.request, responseToCache);
})
return response;
}).catch(function (error) {
if(cachedResponse){
console.log("From the cache",e.request.url);
return cachedResponse
}
return cachedResponse;
})
}).catch(function (error) {
console.log("error");
})
);
});
// activate event will be fired to kill the old running service worker instance and new service
// worker will take control
self.addEventListener("activate",function (e) {
e.waitUntil(
caches.keys().then(function(keys){
return Promise.all(keys.map(function(key, i){
if(key !== Config.CACHE_VERSION){
return caches.delete(keys[i]);
}
}))
})
);
});
// To recive Notification from server.
self.addEventListener('push', function(e) {
console.log('[Service Worker] Push Received.');
e.waitUntil(self.registration.showNotification(e.data.text(), Config.OPTIONS));
});
// Service to run in background
self.addEventListener('sync', function(event) {
console.log("Sync Recieved",event);
if (event.tag == 'SyncInBackground') {
self.registration.showNotification(Config.NOTIFICATION_TITLE, Config.OPTIONS)
}
});