-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtml5notify.js
36 lines (35 loc) · 1.55 KB
/
html5notify.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
(function (w) {
'use strict';
var napi, perms = ['granted', 'default', 'denied'];
if (window.webkitNotifications) { // google chrome implementation
napi = window.webkitNotifications;
w.Html5Notification = function Html5Notification(title, options) {
options = options || {};
var n;
try {
n = napi.createNotification(options.icon || '', title, options.body || '');
n.dir = options.dir || 'auto';
n.tag = options.tag || '';
n.show();
} catch (e) {
n = {close : function () {}};
n.__defineSetter__('onerror', function (callback) { if (typeof callback === 'function') callback.call(this); });
}
return n;
};
w.Html5Notification.__defineGetter__('permission', function () { return perms[napi.checkPermission()]; });
w.Html5Notification.requestPermission = function (callback) { napi.requestPermission(function () {
if (typeof callback === 'function') callback.call(window, perms[napi.checkPermission()]);
}); };
} else if (window.Notification) { // we can work with w3c spec
w.Html5Notification = window.Notification;
} else { // web notifications not supported so let's make some dummy implementation
w.Html5Notification = function Html5Notification() {
this.close = function () {};
};
w.Html5Notification.permission = 'unsupported'; // we can't use setters/getters here cause it can be IE :-)
w.Html5Notification.requestPermission = function (callback) {
if (typeof callback === 'function') callback.call(this, 'unsupported');
};
}
}(window)); // we can use any other object that we what to have notifications