-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.js
More file actions
112 lines (101 loc) · 3.13 KB
/
options.js
File metadata and controls
112 lines (101 loc) · 3.13 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
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
const loggedOutDiv = document.getElementById('logged_out');
const loggedInDiv = document.getElementById('logged_in');
const connectButton = document.getElementById('connect');
const disconnectButton = document.getElementById('disconnect');
const refreshButton = document.getElementById('refresh');
connectButton.onclick = connect;
disconnectButton.onclick = disconnect;
refreshButton.onclick = refresh;
const script = document.createElement('script');
script.src = 'https://apis.google.com/js/api.js?onload=init';
document.documentElement.appendChild(script);
function init() {
login(false)
.then(token => {
load();
showLoggedOut();
})
.catch(() => {
showLoggedIn();
});
}
function showLoggedOut() {
loggedOutDiv.style.display = 'none';
loggedInDiv.style.display = 'inline';
}
function showLoggedIn() {
loggedOutDiv.style.display = 'inline';
loggedInDiv.style.display = 'none';
}
function updateActionButton() {
chrome.runtime.sendMessage({ command: 'update' });
}
function load() {
loadEvents().then(events => {
display(events);
showLoggedOut();
});
}
function connect() {
login(true)
.then(() => load())
.then(() => updateActionButton())
.catch(() => {});
}
function disconnect() {
chrome.identity.getAuthToken({ interactive: false }, token => {
if (token) {
const url =
'https://accounts.google.com/o/oauth2/revoke?token=' + token;
fetch(url).then(() => {
chrome.identity.removeCachedAuthToken({ token: token }, () => {
display(undefined);
showLoggedIn();
updateActionButton();
});
});
}
});
}
function refresh() {
display(undefined);
load();
updateActionButton();
}
function display(events) {
let html = '';
if (events != null) {
if (!events.length) {
html = '<h3>No meetings found</h3>';
} else {
const count = Math.min(events.length, 10);
for (let i = 0; i < count; i++) {
const event = events[i];
const title = event.url
? '<a target="_blank" href="' +
event.url +
'">' +
event.summary +
'</a>'
: event.summary;
html +=
'<div class="tr"><div class="th">' +
event.start.toLocaleString([], {
month: 'short',
day: 'numeric',
hour: '2-digit',
minute: '2-digit',
}) +
'</div><div class="td">' +
title +
'</div></div>';
}
html =
'<div><h4>Upcoming meetings</h4><div class="table">' +
html +
'</div></div>';
}
}
document.getElementById('meetings').innerHTML = html;
document.getElementById('meetings').style.display = 'block';
}