-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackground.js
87 lines (69 loc) · 1.89 KB
/
background.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
OSC = 'http://www.oschina.net';
// ajax get
function get(url, success) {
var xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
success(xhr.responseText);
}
};
xhr.send();
}
// 显示通知
function show(content) {
var notification = window.webkitNotifications.createNotification(
'osc.png',
'有消息了',
content
);
// 点击打开首页
notification.onclick = function () {
chrome.tabs.create({url:OSC});
this.cancel();
}
// 60秒自动关闭
notification.ondisplay = function () {
var temp = this;
var fn = function () {
temp.cancel();
};
window.setTimeout(fn, 60000);
};
notification.show();
}
// 请求首页
function checkAtMe() {
console.log('checkAtMe....');
get(OSC, function (html) {
var msg = /\d+条新留言/.exec(html) || '';
var at = /\d+条提到我/.exec(html) || '';
var comment = /\d+条新评论/.exec(html) || '';
if (at || msg || comment) {
show(msg + ' ' + comment + ' ' + at);
} else {
console.log('nothing....');
}
});
}
//=====================================================
window.addEventListener('load', function () {
// 加载设置信息
if (!localStorage.isInitialized) {
localStorage.isActivated = true;
localStorage.frequency = 1;
localStorage.isInitialized = true;
}
// 立刻检查一次
if (JSON.parse(localStorage.isActivated)) checkAtMe();
// 间隔检查
var interval = 0;
setInterval(function () {
interval++;
if (JSON.parse(localStorage.isActivated)
&& localStorage.frequency <= interval) {
checkAtMe();
interval = 0;
}
}, 60000);
});