-
Notifications
You must be signed in to change notification settings - Fork 4
/
background.html
79 lines (61 loc) · 2.32 KB
/
background.html
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
<!doctype html>
<html>
<head>
<script type="text/javascript" src="jquery-2.0.3.min.js"></script>
<script type="text/javascript" src="underscore-min.js"></script>
<script type="text/javascript" src="coffee-script.js"></script>
<script type="text/coffeescript">
$.ajaxSetup
crossDomain: true
dataType: 'json'
class Fetcher
totalPR: 0
accessToken: null
repositories: []
port: null
constructor: ->
@port = chrome.extension.connect name: 'connection'
chrome.extension.onConnect.addListener (port) =>
port.onMessage.addListener (msg) =>
console.log msg
@fetch(port) if msg.refresh?
fetch: (port = null) ->
@totalPR = 0
@accessToken = localStorage.getItem('accessToken')
@apihost = if localStorage.getItem('githubApiHost') then localStorage.getItem('githubApiHost') else 'https://api.github.com'
@repositories = JSON.parse(localStorage.getItem('repositories'))
dfds = []
_(@repositories).each (repo) =>
dfds.push $.ajax
type: 'get'
url: @apihost + "/repos/#{repo}/pulls?access_token=#{@accessToken}"
success: (data) =>
@store(repo, data)
error: =>
debugger
$.when.apply($, dfds).done =>
repos = JSON.parse(localStorage.getItem('repos')) or {}
hiddenPRs = JSON.parse(localStorage.getItem('hiddenPRs')) or []
totalPR = _(repos).reduce (prev, prs) ->
filtered = _(prs).filter (pr) -> not _(hiddenPRs).contains pr.id
prev + filtered.length
, 0
if totalPR > 0
chrome.browserAction.setBadgeText text: "#{totalPR}"
else
chrome.browserAction.setBadgeText text: ''
if port
port.postMessage success: true
store: (repo, data) ->
repos = JSON.parse(localStorage.getItem('repos')) or {}
repos[repo] = data
localStorage.setItem('repos', JSON.stringify(repos))
fetcher = new Fetcher
fetcher.fetch()
interval = localStorage.getItem('refreshRate') or 600000
setInterval ->
fetcher.fetch()
, interval
</script>
</head>
</html>