This repository has been archived by the owner on Mar 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
service-worker.js
169 lines (143 loc) · 4.68 KB
/
service-worker.js
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
@license
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
*/
'use strict';
var options = JSON.parse(decodeURIComponent(location.search.substring(1)));
var DEFAULT_TAG = self.registration.scope;
var DATA_SUPPORT = Notification.prototype.hasOwnProperty('data');
self.skipWaiting();
/**
* Resolves a URL that is relative to the registering page to an absolute URL
*
* @param url {String} a relative URL
* @return {String} the equivalent absolute URL
*/
var absUrl = function(url) {
if (typeof(url) === 'string') {
return new URL(url, options.baseUrl).href;
}
};
var getClientWindows = function() {
return clients.matchAll({
type: 'window',
includeUncontrolled: true
}).catch(function(error) {
// Couldn't get client list, possibly not yet implemented in the browser
return [];
});
};
var getVisible = function(url) {
return getClientWindows().then(function(clientList) {
for (var client of clientList) {
if (client.url === url && client.focused &&
client.visibilityState === 'visible') {
return client;
}
}
return null;
});
};
var messageClient = function(client, message, notificationShown) {
client.postMessage({
source: self.registration.scope,
message: message,
type: notificationShown ? 'click' : 'push'
});
};
var notify = function(data) {
var messagePromise;
if (options.messageUrl) {
var fetchOptions = {};
if (options.useCredentials) {
fetchOptions.credentials = 'include';
}
var request = new Request(absUrl(options.messageUrl), fetchOptions);
messagePromise = fetch(request).then(function(response) {
return response.json();
});
} else {
messagePromise = data ? Promise.resolve(data.json()) : Promise.resolve({});
}
return messagePromise.then(function(message) {
// Not included: body, data, icon, sound, tag - we special case those
var validNotificationOptions = ['dir', 'lang', 'noscreen', 'renotify',
'silent', 'sticky', 'vibrate'];
var detail = {
title: message.title || options.title || '',
body: message.message || options.message || '',
tag: message.tag || options.tag || DEFAULT_TAG,
icon: absUrl(message.icon || options.iconUrl),
sound: absUrl(message.sound || options.sound),
data: message
};
validNotificationOptions.forEach(function(option) {
detail[option] = message[option] || options[option];
});
var clickUrl = absUrl(message.url || options.clickUrl);
if (!DATA_SUPPORT) {
// If there is no 'data' property support on the notification then we have
// to pass the link URL (and anything else) some other way. We use the
// hash of the icon URL to store it.
var iconUrl = new URL(detail.icon || 'about:blank');
iconUrl.hash = encodeURIComponent(JSON.stringify(detail.data));
detail.icon = iconUrl.href;
}
return getVisible(clickUrl).then(function(visibleClient) {
if (visibleClient) {
messageClient(visibleClient, message, false);
} else {
return self.registration.showNotification(detail.title, detail);
}
});
});
};
var clickHandler = function(notification) {
notification.close();
var message;
if ('data' in notification) {
message = notification.data;
} else {
message = new URL(notification.icon).hash.substring(1);
message = JSON.parse(decodeURIComponent(message));
}
var url = absUrl(message.url || options.clickUrl);
if (!url) {
return;
}
return getClientWindows().then(function(clientList) {
for (var client of clientList) {
if (client.url === url && 'focus' in client) {
client.focus();
return client;
}
}
if ('openWindow' in clients) {
return clients.openWindow(url);
}
}).then(function(client) {
if (client) {
messageClient(client, message, true);
}
});
};
self.addEventListener('push', function(event) {
event.waitUntil(notify(event.data));
});
self.addEventListener('notificationclick', function(event) {
event.waitUntil(clickHandler(event.notification));
});
self.addEventListener('message', function(event) {
if (event.data.type == 'test-push') {
notify({
json: function() {
return Promise.resolve(event.data.message);
}
});
}
});