This repository has been archived by the owner on Sep 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
149 lines (135 loc) · 4.05 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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import browser from 'webextension-polyfill'
import { clearBadge } from '@/api/badge'
import {
isPopupPageOpen,
addAuthorizeRequestListener,
addReplaceRepoRequestListener,
addCheckReposRequestListener,
addRepoUpdatedMsgtListener,
} from '@/api/message'
import {
replaceRepo,
checkRepos,
getScheduleInfo,
addRateLimitRemainingListener,
} from '@/api/storage'
import { authorize } from '@/api/oauth'
if (process.env.DEBUG_MODE) {
console.log(`Debug mode enabled`)
window.browser = browser
}
/* ------------------------------------ *\
#notifications
\* ------------------------------------ */
browser.notifications.onClicked.addListener(name => {
if (/^[^/\s]+\/[^/\s]+$/.test(name)) {
// is a repo
browser.tabs.create({ url: 'https://github.com/' + name })
browser.notifications.clear(name)
clearBadge()
}
})
/* ------------------------------------ *\
#runtime messages
\* ------------------------------------ */
addReplaceRepoRequestListener(message => {
replaceRepo(message)
.then(releaseData => {
if (!releaseData) { return }
browser.notifications.create(
releaseData.name, // id
{
type: 'basic', // Firefox currently only support basic
title: 'Github Release Notifier',
message: `Start watching ${releaseData.name} for ${releaseData.watching} releases`,
iconUrl: browser.runtime.getURL('icon-128.png'),
eventTime: Date.now() + 5000,
}
)
})
})
addCheckReposRequestListener(() => {
checkRepos().then(setAlarm)
})
addRepoUpdatedMsgtListener(repoData => {
isPopupPageOpen()
.catch(() => false)
.then(result => {
// ignore if popup page is open
if (result) { return }
browser.notifications.create(
repoData.name, // id
{
type: 'basic', // Firefox currently only support basic
title: 'Github Release Notifier',
message: `${repoData.name} has just updated to ${repoData.tag_name}.`,
iconUrl: browser.runtime.getURL('icon-128.png'),
eventTime: Date.now() + 5000,
}
)
})
})
/* ------------------------------------ *\
#rate limit remaining
\* ------------------------------------ */
addRateLimitRemainingListener(remaining => {
if (remaining <= 10) {
browser.notifications.create(
'ratelimit' + remaining, // id
{
type: 'basic', // Firefox currently only support basic
title: 'Github Release Notifier',
message: `You are about to exceed rate limit (${remaining} remains). Please sign in.`,
iconUrl: browser.runtime.getURL('icon-128.png'),
eventTime: Date.now() + 5000,
requireInteraction: true,
}
)
}
})
/* ------------------------------------ *\
#perform oauth authorization
\* ------------------------------------ */
addAuthorizeRequestListener(() => {
return authorize()
.catch(err => {
if (process.env.DEBUG_MODE) {
console.warn(err)
}
browser.notifications.create(
'authorize', // id
{
type: 'basic', // Firefox currently only support basic
title: 'Github Release Notifier',
message: `Unable to sign in.`,
iconUrl: browser.runtime.getURL('icon-128.png'),
eventTime: Date.now() + 5000,
}
)
})
})
/* ------------------------------------ *\
#alarms
\* ------------------------------------ */
browser.alarms.onAlarm.addListener(() => {
if (process.env.DEBUG_MODE) {
console.log('Alarm triggered')
}
checkRepos().then(setAlarm).catch(setAlarm)
})
chrome.runtime.onInstalled.addListener(setAlarm)
browser.runtime.onStartup.addListener(setAlarm)
function setAlarm () {
return browser.alarms.clearAll()
.then(getScheduleInfo)
.then(({lastCheck, period}) => {
if (lastCheck + period * 60 <= Date.now()) {
// check now
return checkRepos().then(setAlarm).catch(setAlarm)
}
if (process.env.DEBUG_MODE) {
console.log(`set alarm: in ${period} miniutes`)
}
return browser.alarms.create({delayInMinutes: period})
})
}