-
Notifications
You must be signed in to change notification settings - Fork 63
/
notification-logger.js
57 lines (51 loc) · 1.35 KB
/
notification-logger.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
(function() {
var isInitialized = false, _console = {};
Notification.requestPermission();
// Get current notification icon
happyIcon = "happy.png"
sadIcon = "unhappy.png"
function log(body, title, icon) {
icon = icon || logger.happyIcon;
title = title || "Notification";
if (!("Notification" in window)) {
alert("This browser does not support desktop notification");
} else if (Notification.permission === "granted") {
new Notification(title ,{body: body, icon: icon});
} else if (Notification.permission !== 'denied') {
Notification.requestPermission(function (permission) {
if (permission === "granted") {
new Notification(title ,{body: body, icon: icon});
}
});
}
}
function err(body, title) {
log(body, title, logger.sadIcon);
}
function originalFnCallDecorator(fn, fnName) {
return function() {
fn.apply(this, arguments);
if (typeof _console[fnName] === 'function') {
_console[fnName].apply(console, arguments);
}
};
}
function destroy() {
isInitialized = false;
console.log = _console.log;
}
function init() {
if (isInitialized) { return; }
isInitialized = true;
_console.log = console.log;
console.log = originalFnCallDecorator(log, 'log');
}
window.logger = {
log: log,
err: err,
init: init,
destroy:destroy,
happyIcon: happyIcon,
sadIcon: sadIcon
}
})();