-
Notifications
You must be signed in to change notification settings - Fork 1
/
notifier.js
60 lines (51 loc) · 1.65 KB
/
notifier.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
(function () {
var Notifier = function () {
var titlebar = document.getElementsByTagName('title')[0];
var pageTitle = titlebar.innerHTML;
var total = 0;
var totalMax = 99;
var withSuffix = false;
var updateTotal = function (value) {
if (value > 0 && value <= totalMax) {
total = value;
withSuffix = false;
} else if (value <= 0) {
total = 0;
withSuffix = false;
} else if (total > totalMax) {
total = totalMax;
withSuffix = true;
}
updateTitle();
};
var updateTitle = function () {
titlebar.innerHTML = '(' + total + (withSuffix ? '+' : '') + ') ' + pageTitle;
};
return {
set: function (value) {
if (!isNaN(value)) {
updateTotal(parseInt(value, 10));
}
return this;
},
add: function (value) {
value = (!isNaN(value)) ? total + parseInt(value, 10) : total + 1;
updateTotal(value);
return this;
},
sub: function (value) {
value = (!isNaN(value)) ? total - parseInt(value, 10) : total - 1;
updateTotal(value);
return this;
},
reset: function () {
updateTotal(0);
return this;
},
remove: function () {
titlebar.innerHTML = pageTitle;
}
};
};
window.notifier = new Notifier();
})();