-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgcal.js
More file actions
70 lines (67 loc) · 2.62 KB
/
gcal.js
File metadata and controls
70 lines (67 loc) · 2.62 KB
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
const REGEX = /((?:https?:\/\/)(?:(?:(?:\w+\.)?zoom\.us)|(?:hangouts\.google.com)|(?:meet\.google.com)|(?:teams\.microsoft.com)|(?:(?:\w+\.)?gotomeeting.com|(?:(?:\w+\.)?gotomeet.me))|(?:(?:\w+\.)?chime.aws)|(?:(?:\w+\.)?bluejeans.com)|(?:(?:\w+\.)?webex.com)|(?:(?:\w+\.)?whereby.com))[A-Za-z0-9~#=_\-\/\?]*)/;
function getDate(data) {
if (data.dateTime) {
return new Date(data.dateTime);
}
return undefined;
}
function login(interactive = false) {
return new Promise((resolve, reject) => {
gapi.load('client:auth2', () => {
gapi.client.load('calendar', 'v3', () => {
chrome.identity.getAuthToken({ interactive }, token => {
if (!token) {
reject();
} else {
gapi.client.setToken({ access_token: token });
resolve(token);
}
});
});
});
});
}
function loadEvents() {
return gapi.client.calendar.events
.list({
calendarId: 'primary',
timeMin: new Date().toISOString(),
showDeleted: false,
singleEvents: true,
maxResults: 10,
orderBy: 'startTime',
})
.then(response => {
return response.result.items.map(item => ({
summary: item.summary || '',
location: item.location || '',
description: item.description || '',
hangoutLink: item.hangoutLink || '',
start: item.start,
end: item.end,
}));
})
.then(events => {
return events
.map(event => {
const start = getDate(event.start);
const end = getDate(event.end);
if (!start || !end) return undefined;
const descriptionMatch = event.description.match(REGEX);
const locationMatch = event.location.match(REGEX);
const summaryMatch = event.summary.match(REGEX);
let url;
if (event.hangoutLink) url = event.hangoutLink;
else if (descriptionMatch) url = descriptionMatch[1];
else if (locationMatch) url = locationMatch[1];
else if (summaryMatch) url = summaryMatch[1];
return {
...event,
start,
end,
url,
};
})
.filter(event => Boolean(event));
});
}