-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
200 lines (176 loc) · 7.68 KB
/
index.html
File metadata and controls
200 lines (176 loc) · 7.68 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
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
<html>
<head>
<script type="text/javascript">
// Your Client ID can be retrieved from your project in the Google
// Developer Console, https://console.developers.google.com
// var CLIENT_ID = '<YOUR_CLIENT_ID>';
var CLIENT_ID = '123456789-lisobijblahmoehahamamama.apps.googleusercontent.com';
// var SCRIPT_ID = "ENTER_YOUR_SCRIPT_ID_HERE";
var SCRIPT_ID = "MSFOXmoehahahablafoobar-GxFoobar123";
var SCOPES = ['https://www.googleapis.com/auth/drive'];
/**
* Check if current user has authorized this application.
*/
function checkAuth() {
console.log("function checkAuth()");
gapi.auth.authorize(
{
'client_id': CLIENT_ID,
'scope': SCOPES.join(' '),
'immediate': true
}, handleAuthResult);
}
/**
* Handle response from authorization server.
*
* @param {Object} authResult Authorization result.
*/
function handleAuthResult(authResult) {
console.log("function handleAuthResult(authResult)");
var authorizeDiv = document.getElementById('authorize-div');
if (authResult && !authResult.error) {
// Hide auth UI, then load client library.
authorizeDiv.style.display = 'none';
} else {
// Show auth UI, allowing the user to initiate authorization by
// clicking authorize button.
authorizeDiv.style.display = 'inline';
}
}
/**
* Initiate auth flow in response to user clicking authorize button.
*
* @param {Event} event Button click event.
*/
function handleAuthClick(event) {
console.log("function handleAuthClick(event)");
gapi.auth.authorize(
{client_id: CLIENT_ID, scope: SCOPES, immediate: false},
handleAuthResult);
return false;
}
/**
* Calls an Apps Script function
*/
function callScriptFunction() {
console.log("function callScriptFunction()");
// Create an execution request object.
var request = {
'function': 'myFunction'
};
// Make the API request.
var op = gapi.client.request({
'root': 'https://script.googleapis.com', 'path': 'v1/scripts/' + SCRIPT_ID + ':run',
'method': 'POST', 'body': request
});
op.execute(function(resp) {
if (resp.error && resp.error.status) {
// The API encountered a problem before the script
// started executing.
appendPre('Error calling API:');
appendPre(JSON.stringify(resp, null, 2));
} else if (resp.error) {
// The API executed, but the script returned an error.
// Extract the first (and only) set of error details.
// The values of this object are the script's 'errorMessage' and
// 'errorType', and an array of stack trace elements.
var error = resp.error.details[0];
appendPre('Script error message: ' + error.errorMessage);
if (error.scriptStackTraceElements) {
// There may not be a stacktrace if the script didn't start
// executing.
appendPre('Script error stacktrace:');
for (var i = 0; i < error.scriptStackTraceElements.length; i++) {
var trace = error.scriptStackTraceElements[i];
appendPre('\t' + trace.function + ':' + trace.lineNumber);
}
}
} else {
// The structure of the result will depend upon what the Apps
// Script function returns.
console.log(result);
var result = resp.response.result;
if (Object.keys(result ).length == 0) {
appendPre('Nothing returned!');
} else {
appendPre('Result:');
appendPre('\t' + result);
}
}
});
}
/**
* Calls an Apps Script function
* https://developers.google.com/api-client-library/javascript/reference/referencedocs#gapiclientrequest
*/
function handleClickMe() {
console.log("function handleClickMe()");
//https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D'https%3A%2F%2Fru.insider.pro%2Fanalytics%2F2016-09-24%2F5-provalov-facebook%2F'%20and%20xpath%3D'%2F%2F*%5B%40itemprop%3D%22articleBody%22%5D%2F%2Fp%2Ftext()'&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=
var url = 'https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%27https%3A%2F%2Fru.insider.pro%2Fanalytics%2F2016-09-24%2F5-provalov-facebook%2F%27%20and%20xpath%3D%27%2F%2F*%5B%40itemprop%3D%22articleBody%22%5D%2F%2Fp%2Ftext()%27&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=';
// Create an execution request object.
var headers = {
'Content-Type': 'application/json'
};
var request = {
'function': 'myYQLFunction',
'parameters': [ url ]
};
// Make the API request.
var op = gapi.client.request({
'root': 'https://script.googleapis.com', 'path': 'v1/scripts/' + SCRIPT_ID + ':run',
'method': 'POST', 'headers' : headers, 'body': request
});
op.execute(function(resp) {
if (resp.error && resp.error.status) {
// The API encountered a problem before the script started executing.
appendPre('Error calling API:');
appendPre(JSON.stringify(resp, null, 2));
} else if (resp.error) {
// The API executed, but the script returned an error.
// Extract the first (and only) set of error details.
var error = resp.error.details[0];
appendPre('Script error message: ' + error.errorMessage);
} else {
// The structure of the result will depend upon what the Apps Script function returns.
var result = resp.response;
if (Object.keys(result ).length == 0) {
appendPre('Nothing returned!');
} else {
appendPre('Result:');
appendPre('\t' + result);
}
}
});
}
/**
* Append a pre element to the body containing the given message
* as its text node.
*
* @param {string} message Text to be placed in pre element.
*/
function appendPre(message) {
console.log("function appendPre(message)");
var pre = document.getElementById('output');
var textContent = document.createTextNode(message + '\n');
pre.appendChild(textContent);
}
</script>
<script src="https://apis.google.com/js/client.js?onload=checkAuth"></script>
</head>
<body>
<div id="authorize-div" style="display: none">
<span>Authorize access to Google Apps Script Execution API</span>
<!--Button for the user to click to initiate auth sequence -->
<button id="authorize-button" onclick="handleAuthClick(event)">
Authorize
</button>
</div>
<div id="clickme" style="display: inline">
<span>Click Me</span>
<!--Button for the user to click to call a function -->
<button id="clickme-button" onclick="handleClickMe(event)"> here </button>
<button id="handecall-button" onclick="callScriptFunction(event)"> here </button>
</div>
<pre id="output"></pre>
</body>
</html>