-
Notifications
You must be signed in to change notification settings - Fork 2
/
service-worker.js
124 lines (98 loc) · 3.22 KB
/
service-worker.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
importScripts('./js/config.js')
chrome.runtime.onMessage.addListener(async (message, sender, respond) => {
if (message.type === 'auth:credentials') {
const subscription = await getSubscription()
const { accessToken, accountId } = message.value
const token = await getWebsocketToken(accessToken, accountId)
if (token) {
sendSubscriptionToHaws(subscription, token)
}
}
})
chrome.runtime.onInstalled.addListener(({ reason, previousVersion }) => {
if (reason === chrome.runtime.OnInstalledReason.INSTALL) {
const splashUrl = 'https://www.getharvest.com/harvest-for-chrome-installed'
chrome.tabs.create({ url: `${splashUrl}?version=${chrome.runtime.getManifest().version}` })
}
if (reason === chrome.runtime.OnInstalledReason.UPDATE) {
chrome.notifications.create({
iconUrl: 'images/h-app@128px.png',
message: 'Please reopen the extension to continue to receive updates.',
priority: 0,
title: 'Harvest Time Tracker was updated',
type: 'basic',
})
}
})
chrome.webNavigation.onHistoryStateUpdated.addListener((e) => {
chrome.tabs.sendMessage(e.tabId, { trelloUrlChanged: true });
}, {
url: [{ hostSuffix: 'trello.com' }]
})
self.addEventListener('push', function(event) {
const data = event.data.json()
const running = data.event_type === 'start_timer'
const iconState = running ? 'on' : 'off'
chrome.action.setIcon({
path: {
'19': `images/h-toolbar-${iconState}@19px.png`,
'38': `images/h-toolbar-${iconState}@38px.png`,
},
})
const title = running ? 'View the running Harvest timer' : 'Start a Harvest timer'
chrome.action.setTitle({ title })
})
const sendSubscriptionToHaws = async (subscription, token) => {
const headers = {
authorization: `Bearer ${token}`,
'content-type': 'application/json',
}
const body = JSON.stringify({ subscription })
const response = await fetch(`${settings.haws_host}/subscriptions`, {
body,
headers,
method: 'post',
})
if (response.ok) {
const payload = await response.json()
}
}
const getWebsocketToken = async (accessToken, accountId) => {
const headers = {
'harvest-account-id': accountId,
accept: 'application/json',
authorization: `Bearer ${accessToken}`,
}
const response = await fetch(`${settings.api_host}/v2/websocket_token`, {
headers,
method: 'GET',
})
if (response.ok) {
const payload = await response.json()
return payload.token
}
}
const getSubscription = async () => {
let subscription = await self.registration.pushManager.getSubscription()
if (subscription) {
return subscription
}
convertedVapidKey = urlBase64ToUint8Array(settings.vapid_public_key)
subscription = await self.registration.pushManager.subscribe({
applicationServerKey: convertedVapidKey,
userVisibleOnly: true,
})
return subscription
}
function urlBase64ToUint8Array(base64String) {
const padding = '='.repeat((4 - base64String.length % 4) % 4)
const base64 = (base64String + padding)
.replace(/\-/g, '+')
.replace(/_/g, '/')
const rawData = atob(base64)
const outputArray = new Uint8Array(rawData.length)
for (var i = 0; i < rawData.length; ++i) {
outputArray[i] = rawData.charCodeAt(i)
}
return outputArray
}