-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
99 lines (87 loc) · 3.18 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
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
switch (message.type) {
case 'auth_api':
handleAuthApi(message, sendResponse);
break;
case 'archive_message':
handleBulkAction(message, sendResponse, 'archive');
break;
case 'pin_message':
handleBulkAction(message, sendResponse, 'pin');
break;
case 'update_card_title':
handleUpdateCardTitle(message, sendResponse);
break;
}
return true;
});
function handleAuthApi(message, sendResponse) {
const { clientId, clientSecret, redirectUri, refreshToken } = message;
const url = `https://launchpad.37signals.com/authorization/token?type=refresh&refresh_token=${refreshToken}&client_id=${clientId}&redirect_uri=${redirectUri}&client_secret=${clientSecret}`;
fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
})
.then(handleFetchResponse)
.then(data => sendResponse({ status: 'success', data }))
.catch(error => handleError(error, sendResponse));
}
function handleBulkAction(message, sendResponse, action) {
const { bucketId, currentAccountSlug, allMessagesId, apiToken } = message;
let urlPart, method;
switch (action) {
case 'archive':
urlPart = 'status/archived.json';
method = 'PUT';
break;
case 'pin':
urlPart = 'pin.json';
method = 'POST';
break;
default:
return handleError(new Error('Invalid action'), sendResponse);
}
const requests = allMessagesId.map((messageId) => {
const url = `https://3.basecampapi.com${currentAccountSlug}/buckets/${bucketId}/recordings/${messageId}/${urlPart}`;
return fetch(url, {
method,
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiToken}`
}
})
.then(handleFetchResponse)
.catch(error => handleError(error, sendResponse));
});
Promise.all(requests)
.then(() => sendResponse({ status: 'success' }))
.catch(error => handleError(error, sendResponse));
return true;
}
function handleUpdateCardTitle(message, sendResponse) {
const { cardId, newTitle, apiToken, bucketId, currentAccountSlug } = message;
const url = `https://3.basecampapi.com${currentAccountSlug}/buckets/${bucketId}/card_tables/cards/${cardId}.json`;
fetch(url, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiToken}`
},
body: JSON.stringify({
title: newTitle
})
})
.then(handleFetchResponse)
.then(data => sendResponse({ status: 'success', data }))
.catch(error => handleError(error, sendResponse));
}
function handleFetchResponse(response) {
if (!response.ok) throw new Error(`Error: ${response.statusText}`);
return response.text().then(text => text ? JSON.parse(text) : {});
}
function handleError(error, sendResponse) {
console.error(error);
sendResponse({ status: 'error', error: error.message });
}