-
Notifications
You must be signed in to change notification settings - Fork 5
/
background.coffee
76 lines (59 loc) · 1.99 KB
/
background.coffee
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
INTERVAL = 1000 * 600
entryList = []
getLastVisitedEpoch = ->
+ localStorage['lastVisited'] || 0
setLastVisitedEpoch = (epoch) ->
localStorage['lastVisited'] = epoch
updateEntryList = (callback) ->
$.ajax
url: 'http://blog.hatena.ne.jp/-/antenna'
dataType: 'html'
success: (res) ->
if entryList.length > 0
keyTime = entryList[entryList.length - 1].time
else
keyTime = getLastVisitedEpoch()
# $.each と Array.reverse を組み合わせたので ごちゃっとしてる 旧→新 の順で見るため
$($(res).find('ul.entry-list li').get().reverse()).each ->
entry_titles = $(this).contents().filter(-> 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
entryList.push entry
callback() if callback
chrome.browserAction.setBadgeBackgroundColor({color: [56,136, 218, 255]})
updateBadge = ->
label = if entryList.length > 0 then String(entryList.length) else ""
chrome.browserAction.setBadgeText
text: label
checkNewBlogs = ->
updateEntryList ->
updateBadge()
setInterval ->
checkNewBlogs()
, INTERVAL
checkNewBlogs()
chrome.extension.onRequest.addListener (request, sender, sendResponse) ->
return if request.method != "getNextEntry"
if entryList.length > 0
entry = entryList.shift()
len = entryList.length
setLastVisitedEpoch(entry.time)
updateBadge()
sendResponse
entry: entry
unread_count: len
else
sendResponse
entry: null
unread_count: 0