-
Notifications
You must be signed in to change notification settings - Fork 5
/
background.js
94 lines (83 loc) · 2.43 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
88
89
90
91
92
93
94
var INTERVAL, checkNewBlogs, entryList, getLastVisitedEpoch, setLastVisitedEpoch, updateBadge, updateEntryList;
INTERVAL = 1000 * 600;
entryList = [];
getLastVisitedEpoch = function() {
return +localStorage['lastVisited'] || 0;
};
setLastVisitedEpoch = function(epoch) {
return localStorage['lastVisited'] = epoch;
};
updateEntryList = function(callback) {
return $.ajax({
url: 'http://blog.hatena.ne.jp/-/antenna',
dataType: 'html',
success: function(res) {
var keyTime;
if (entryList.length > 0) {
keyTime = entryList[entryList.length - 1].time;
} else {
keyTime = getLastVisitedEpoch();
}
$($(res).find('ul.entry-list li').get().reverse()).each(function() {
var entry, entry_title, entry_titles;
entry_titles = $(this).contents().filter(function() {
return this.nodeType === 3 && this.textContent.match(/\S/);
});
if (entry_titles.length > 0) {
entry_title = entry_titles[0].textContent;
} else {
entry_title = '■';
}
entry = {
blog_title: $(this).find('a').text(),
entry_title: entry_title,
entry_url: $(this).find('a').attr('href'),
user_image: $(this).find('img').attr('src'),
user_name: $(this).attr('data-author'),
time: +$(this).find('time').attr('data-epoch'),
time_text: $(this).find('time').text()
};
if (entry.time > keyTime) return entryList.push(entry);
});
if (callback) return callback();
}
});
};
chrome.browserAction.setBadgeBackgroundColor({
color: [56, 136, 218, 255]
});
updateBadge = function() {
var label;
label = entryList.length > 0 ? String(entryList.length) : "";
return chrome.browserAction.setBadgeText({
text: label
});
};
checkNewBlogs = function() {
return updateEntryList(function() {
return updateBadge();
});
};
setInterval(function() {
return checkNewBlogs();
}, INTERVAL);
checkNewBlogs();
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
var entry, len;
if (request.method !== "getNextEntry") return;
if (entryList.length > 0) {
entry = entryList.shift();
len = entryList.length;
setLastVisitedEpoch(entry.time);
updateBadge();
return sendResponse({
entry: entry,
unread_count: len
});
} else {
return sendResponse({
entry: null,
unread_count: 0
});
}
});