-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
201 lines (186 loc) · 5.23 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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
const DEVELOPMENT_URL = "http://localhost:3000";
const PRODUCTION_URL = "https://kangarooos.com";
const BASE_URL = DEVELOPMENT_URL;
chrome.tabs.onCreated.addListener(async (tab) => {
if (tab.pendingUrl === 'chrome://newtab/') {
await chrome.tabs.update({url: `${BASE_URL}/desktop`});
}
})
const uploadFileToKangaroo = async (name, url, type) => {
const downloadResponse = await fetch(url)
const blob = await downloadResponse.blob()
const file = new File([blob], name, { type: type })
const formData = new FormData()
formData.append('file', file)
await fetch(`${BASE_URL}/cloud_files`, {
headers: await getAuthHeader(),
method: 'POST',
body: formData,
})
}
// Prevents the file dialogue from showing
// Automatically downloads the file to Kangaroo
chrome.downloads.onDeterminingFilename.addListener(async (item, suggest) => {
suggest({
filename: item.filename,
conflict_action: 'prompt',
conflictAction: 'prompt'
})
if (!pressedKeys['ShiftLeft']) {
chrome.downloads.cancel(item.id)
await fetchSessionToken()
uploadFileToKangaroo(item.filename, item.finalUrl, item.mime)
}
})
const fetchSessionTokenFromKangaroo = () => {
return localStorage.getItem('user')
}
const fetchSessionToken = async () => {
const tab = await getKangarooTab()
try {
if (tab) {
const [scriptResponse] = await chrome.scripting.executeScript({
target: { tabId: tab.id },
function: fetchSessionTokenFromKangaroo,
})
const { accessToken, client, email, tokenExpiresAt } = JSON.parse(scriptResponse.result)
await chrome.storage.local.set({
auth_header: {
'access-token': accessToken,
client: client,
expiry: tokenExpiresAt,
uid: email,
'token-type': 'Bearer',
},
})
return true
}
} catch (e) {
console.error(e || 'Ran into an error finding the access token')
return false
}
}
const getKangarooTab = async () => {
const tabs = await chrome.tabs.query({
url: `${BASE_URL}/*`,
})
for (let i = 0; i < tabs.length; i++) {
const url = tabs[i].url
// Double check? Don't want reach into other people's stuff.
if (url?.startsWith(BASE_URL)) {
return tabs[i]
}
}
console.error('Could not find Kangaroo tab')
return null
}
const getFilesFromS3 = async () => {
const res = await fetch(`${BASE_URL}/cloud_files`, {
headers: await getAuthHeader(),
})
const { files } = await res.json()
return files
}
const getAuthHeader = async () => {
const storage = await chrome.storage.local.get('auth_header')
return storage.auth_header
}
const retrieveFileFromS3 = async (fileId) => {
const res = await fetch(`${BASE_URL}/cloud_files/${fileId}`, {
headers: await getAuthHeader(),
})
return res.json()
}
const uploadLinkToKangaroo = async (url) => {
await fetch(`${BASE_URL}/link_files`, {
headers: {
...(await getAuthHeader()),
Accept: 'application/json',
'Content-Type': 'application/json',
},
method: 'POST',
body: JSON.stringify({
path: url,
}),
})
}
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.event === 'cloud-file-detected') {
;(async () => {
if (await fetchSessionToken()) {
try {
const { fileId } = message
const file = await retrieveFileFromS3(fileId)
sendResponse({
status: 'ok',
url: file.url,
name: file.name,
fileType: file.file_type,
})
} catch (e) {
sendResponse({
status: 'error',
error: e,
})
}
}
})()
} else if (message.event === 'upload-to-kangaroo-from-url') {
;(async () => {
if (await fetchSessionToken()) {
try {
let { name, url, type } = message
if (!name) {
const [tab] = await chrome.tabs.query({
active: true,
currentWindow: true,
})
name = tab.title
}
uploadFileToKangaroo(name, url, type)
sendResponse({ status: 'ok' })
} catch (e) {
sendResponse({ status: 'error', error: e })
}
}
})()
} else if (message.event === 'get-file-list') {
;(async () => {
if (await fetchSessionToken()) {
try {
const files = await getFilesFromS3()
sendResponse({ status: 'ok', files: files })
} catch (e) {
sendResponse({ status: 'error', error: e })
}
}
})()
} else if (message.event === 'key-pressed') {
pressedKeys[message.key] = true
handleKeyClicks()
console.log(pressedKeys)
} else if (message.event === 'key-released') {
delete pressedKeys[message.key]
}
return true
})
const pressedKeys = {
}
const handleKeyClicks = () => {
requiredKeys = ['ShiftLeft', 'MetaLeft', 'KeyS']
if (requiredKeys.every(key => pressedKeys[key])) {
(async () => {
if (await fetchSessionToken()) {
try {
await uploadLinkToKangaroo(message.url)
sendResponse({ status: 'ok' })
} catch (e) {
sendResponse({ status: 'error', error: e })
}
}
})()
}
}
function getUserData() {
return localStorage.getItem('user_data')
}