-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
66 lines (59 loc) · 1.59 KB
/
main.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
'use strict';
var origin = 'https://cnodejs.org';
var unreadCount = 0;
var errorMsg = '';
var accessTokenCachekey = 'cnode-accessToken';
var interval = 60000;
window.chrome.browserAction.onClicked.addListener(function() {
if (unreadCount) {
window.open(origin + '/my/messages');
} else {
window.open(origin);
}
});
var timer = setInterval(function () {
var accessToken = window.localStorage.getItem(accessTokenCachekey);
if (!accessToken) return;
window.fetch(origin + '/api/v1/message/count?accesstoken=' + accessToken)
.then(res => res.json())
.then(function (res) {
if (res.success) {
errorMsg = '';
unreadCount = res.data;
renderCount();
} else {
errorMsg = res.error_msg;
unreadCount = 0;
renderError();
}
})
.catch(function (e) {
errorMsg = e.message;
unreadCount = 0;
renderError();
})
}, interval);
function renderCount() {
var color = [65, 131, 196, 255];
render(getCountString(unreadCount), color);
}
function renderError() {
var color = [166, 41, 41, 255];
var title = errorMsg;
render('X', color, title);
}
function render(text, color, title) {
window.chrome.browserAction.setBadgeText({ text: text });
window.chrome.browserAction.setBadgeBackgroundColor({ color: color });
if (title) {
window.chrome.browserAction.setTitle({ title: title });
}
};
function getCountString(count) {
if (count === 0) {
return '';
} else if (count > 9999) {
return '∞';
}
return String(count);
};