-
Notifications
You must be signed in to change notification settings - Fork 10
/
popup.js
229 lines (199 loc) · 6.51 KB
/
popup.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
'use strict';
/*
* Based on:
* Identity example in Chrome Apps Samples
* https://github.com/GoogleChrome/chrome-app-samples/tree/master/samples/identity
* https://github.com/GoogleChrome/chrome-app-samples/blob/master/LICENSE
*
* GDE Sample: Chrome extension Google APIs by Abraham's
* https://github.com/GoogleDeveloperExperts/chrome-extension-google-apis
* https://github.com/GoogleDeveloperExperts/chrome-extension-google-apis/blob/master/LICENSE
*/
var executionAPIExample = (function() {
var SCRIPT_ID='AKfycbw0_ri8sPMpPC-KZObgtTw45HtLfevDlMsm-kb6JQ3hFWHZMOjX'; // Apps Script script id
var STATE_START=1;
var STATE_ACQUIRING_AUTHTOKEN=2;
var STATE_AUTHTOKEN_ACQUIRED=3;
var state = STATE_START;
var signin_button, xhr_button, revoke_button, exec_info_div, exec_data, exec_result;
function disableButton(button) {
button.setAttribute('disabled', 'disabled');
}
function enableButton(button) {
button.removeAttribute('disabled');
}
function changeState(newState) {
state = newState;
switch (state) {
case STATE_START:
enableButton(signin_button);
disableButton(xhr_button);
disableButton(revoke_button);
break;
case STATE_ACQUIRING_AUTHTOKEN:
sampleSupport.log('Acquiring token...');
disableButton(signin_button);
disableButton(xhr_button);
disableButton(revoke_button);
break;
case STATE_AUTHTOKEN_ACQUIRED:
disableButton(signin_button);
enableButton(xhr_button);
enableButton(revoke_button);
break;
}
}
/**
* Get users access_token.
*
* @param {object} options
* @value {boolean} interactive - If user is not authorized ext, should auth UI be displayed.
* @value {function} callback - Async function to receive getAuthToken result.
*/
function getAuthToken(options) {
chrome.identity.getAuthToken({ 'interactive': options.interactive }, options.callback);
}
/**
* Get users access_token in background with now UI prompts.
*/
function getAuthTokenSilent() {
getAuthToken({
'interactive': false,
'callback': getAuthTokenCallback,
});
}
/**
* Get users access_token or show authorize UI if access has not been granted.
*/
function getAuthTokenInteractive() {
getAuthToken({
'interactive': true,
'callback': getAuthTokenCallback,
});
}
/**
* Handle response from authorization server.
*
* @param {string} token - Google access_token to authenticate request with.
*/
function getAuthTokenCallback(token) {
// Catch chrome error if user is not authorized.
if (chrome.runtime.lastError) {
sampleSupport.log('No token aquired');
changeState(STATE_START);
} else {
sampleSupport.log('Token acquired');
changeState(STATE_AUTHTOKEN_ACQUIRED);
}
}
/**
* Calling the Execution API script.
*/
function sendDataToExecutionAPI() {
disableButton(xhr_button);
xhr_button.className = 'loading';
getAuthToken({
'interactive': false,
'callback': sendDataToExecutionAPICallback,
});
}
/**
* Calling the Execution API script callback.
* @param {string} token - Google access_token to authenticate request with.
*/
function sendDataToExecutionAPICallback(token) {
sampleSupport.log('Sending data to Execution API script');
post({ 'url': 'https://script.googleapis.com/v1/scripts/' + SCRIPT_ID + ':run',
'callback': executionAPIResponse,
'token': token,
'request': {'function': 'setData',
'parameters': {'data':JSON.parse(exec_data.value)}}
});
}
/**
* Handling response from the Execution API script.
* @param {Object} response - response object from API call
*/
function executionAPIResponse(response){
sampleSupport.log(response);
enableButton(xhr_button);
xhr_button.classList.remove('loading');
var info;
if (response.response.result.status == 'ok'){
info = 'Data has been entered into <a href="'+response.response.result.doc+'" target="_blank"><strong>this sheet</strong></a>';
} else {
info = 'Error...';
}
exec_result.innerHTML = info;
}
/**
* Revoking the access token.
*/
function revokeToken() {
exec_result.innerHTML='';
getAuthToken({
'interactive': false,
'callback': revokeAuthTokenCallback,
});
}
/**
* Revoking the access token callback
*/
function revokeAuthTokenCallback(current_token) {
if (!chrome.runtime.lastError) {
// Remove the local cached token
chrome.identity.removeCachedAuthToken({ token: current_token }, function() {});
// Make a request to revoke token in the server
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://accounts.google.com/o/oauth2/revoke?token=' +
current_token);
xhr.send();
// Update the user interface accordingly
changeState(STATE_START);
sampleSupport.log('Token revoked and removed from cache. '+
'Check chrome://identity-internals to confirm.');
}
}
/**
* Make an authenticated HTTP POST request.
*
* @param {object} options
* @value {string} url - URL to make the request to. Must be whitelisted in manifest.json
* @value {object} request - Execution API request object
* @value {string} token - Google access_token to authenticate request with.
* @value {function} callback - Function to receive response.
*/
function post(options) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
// JSON response assumed. Other APIs may have different responses.
options.callback(JSON.parse(xhr.responseText));
} else if(xhr.readyState === 4 && xhr.status !== 200) {
sampleSupport.log('post', xhr.readyState, xhr.status, xhr.responseText);
}
};
xhr.open('POST', options.url, true);
// Set standard Google APIs authentication header.
xhr.setRequestHeader('Authorization', 'Bearer ' + options.token);
xhr.send(JSON.stringify(options.request));
}
return {
onload: function () {
signin_button = document.querySelector('#signin');
signin_button.addEventListener('click', getAuthTokenInteractive);
xhr_button = document.querySelector('#getxhr');
xhr_button.addEventListener('click', sendDataToExecutionAPI.bind(xhr_button, true));
revoke_button = document.querySelector('#revoke');
revoke_button.addEventListener('click', revokeToken);
exec_info_div = document.querySelector('#exec_info');
exec_data = document.querySelector('#exec_data');
exec_result = document.querySelector('#exec_result')
// Trying to get access token without signing in,
// it will work if the application was previously
// authorized by the user.
getAuthTokenSilent();
}
};
})();
window.onload = executionAPIExample.onload;