-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
371 lines (324 loc) · 13.6 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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
var comment_sheet_id = "1-Xo0TcHNoSnbxtkGgiRcbnxcMip1R0PQFsrIOsiYevU";
var event_sheet_id = "1pSgzEcft13sB3Lee_zNMkYD_WcDIUqKnjyP12XtLjzc";
var user_id;
var always_show;
var on_grading_page;
function loadSpreadsheet() {
if (chrome.identity === undefined) {
console.log("Please sign-in to Chrome from its top-right menu.");
return;
}
console.log("getting token");
console.log(chrome.identity);
chrome.identity.getAuthToken({interactive: true}, function(token) {
console.log("token request done");
if (token) {
console.log("got the token");
// use that access token to set an http header when calling the Drive API.
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if(xhr.readyState === XMLHttpRequest.DONE && xhr.status == 200) {
// request done, we got the data
console.log("got the data");
console.log({comments: xhr.response.values});
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {comments: xhr.response.values}, function(response) {
console.log("sent comments: " + response);
});
});
}
};
xhr.open("GET",
"https://sheets.googleapis.com/v4/spreadsheets/" + comment_sheet_id + "/values/W16-A10!A2:K10000",
true);
xhr.setRequestHeader('Authorization','Bearer ' + token);
xhr.responseType = "json";
xhr.send();
} else {
var message = "";
if (chrome.runtime.lastError)
message = chrome.runtime.lastError.message;
console.log("Not signed into Chrome, network error or no permission.\n" + message);
}
});
}
function updateSheets(action, rubric_question, rubric_item, comment_info, comment) {
//console.log("updating sheets");
// a comment was just inserted, update the google sheets to keep count & log this
chrome.identity.getAuthToken({interactive: true}, function(token) {
if (token) {
console.log("got the token");
// use that access token to set an http header when calling the Drive API.
if (action == "comment") {
// send request to update frequency count in the comments google sheet
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if(xhr.readyState === XMLHttpRequest.DONE && xhr.status == 200) {
// request done, we sent the data
console.log("Updated frequency count")
} else if (xhr.readyState === XMLHttpRequest.DONE) {
console.log(xhr.responseText);
}
};
var row = parseInt(comment_info[0]) + 2;
var cur_frequency = parseInt(comment_info[8]);
console.log("comment was from row " + row);
console.log("frequency was " + cur_frequency);
xhr.open("PUT",
"https://sheets.googleapis.com/v4/spreadsheets/" + comment_sheet_id + "/values/W16-A10!I" + row + "?valueInputOption=RAW",
true);
xhr.setRequestHeader('Authorization','Bearer ' + token);
xhr.setRequestHeader("Content-type", "application/json");
//xhr.responseType = "json";
xhr.send('{' +
'"range": "W16-A10!I' + row + '",' +
'"values": [[' + (cur_frequency + 1) + ']]' +
'}');
}
// send event log to event log sheet
chrome.storage.local.get(null, function(items) {
if (!items.user_id) {
user_id = Math.random().toString(36) + new Date().getTime();
always_show = (Math.random() < 0.5);
console.log("always show setting: " + always_show);
chrome.storage.local.set({user_id: user_id, always_show: always_show});
} else if (items.always_show == undefined) {
user_id = items.user_id;
always_show = (Math.random() < 0.5);
console.log("always show setting: " + always_show);
chrome.storage.local.set({always_show: always_show});
} else {
user_id = items.user_id;
always_show = items.always_show;
}
console.log("user id: " + user_id);
console.log("always show: " + always_show);
if (rubric_item != undefined) {
rubric_item = rubric_item.replace(/"/g, '\\"').replace(/'/g, "\\'");
}
var xhr2 = new XMLHttpRequest();
xhr2.onreadystatechange = function () {
if(xhr2.readyState === XMLHttpRequest.DONE && xhr2.status == 200) {
// request done, we sent the data
console.log("Updated event log")
} else if (xhr2.readyState === XMLHttpRequest.DONE) {
console.log(xhr2.responseText);
}
};
xhr2.open("POST",
"https://sheets.googleapis.com/v4/spreadsheets/" + event_sheet_id +
"/values/A10!A2:H100000:append?valueInputOption=RAW",
true);
xhr2.setRequestHeader('Authorization','Bearer ' + token);
xhr2.setRequestHeader("Content-type", "application/json");
if (action == "comment") {
xhr2.send('{' +
'"range": "A10!A2:H100000",' +
'"values": [[ "' + new Date().toString() + '", "comment", "' + comment_info[0] + '", "' + user_id + '", "' +
rubric_question + '", "' + rubric_item + '", "' + always_show + '", "' + comment +
'" ]]' +
'}');
} else if (action == "change setting") {
xhr2.send('{' +
'"range": "A10!A2:H100000",' +
'"values": [[ "' + new Date().toString() + '", "change setting", "", "' + user_id + '", "", "", "' + always_show +
'", "" ]]' +
'}');
} else if (action == "show suggestions" || action == "hide suggestions" || action == "focus") {
xhr2.send('{' +
'"range": "A10!A2:H100000",' +
'"values": [[ "' + new Date().toString() + '", "' + action + '", "", "' + user_id + '", "' +
rubric_question + '", "' + rubric_item + '", "' + always_show +
'", "" ]]' +
'}');
} else if (action == "gradescope focus") {
xhr2.send('{' +
'"range": "A10!A2:H100000",' +
'"values": [[ "' + new Date().toString() + '", "' + action + '", "", "' + user_id + '", "' +
rubric_question + '", "", "' + always_show +
'", "" ]]' +
'}');
}
});
} else {
var message = "";
if (chrome.runtime.lastError)
message = chrome.runtime.lastError.message;
console.log("Not signed into Chrome, network error or no permission.\n" + message);
}
});
}
function saveNewComment() {
chrome.storage.local.get(null, function(items) {
if (items.saved != undefined && !items.saved) {
if (!items.user_id) {
user_id = Math.random().toString(36) + new Date().getTime();
always_show = (Math.random() < 0.5);
console.log("always show setting: " + always_show);
chrome.storage.local.set({user_id: user_id, always_show: always_show});
} else if (items.always_show == undefined) {
user_id = items.user_id;
always_show = (Math.random() < 0.5);
console.log("always show setting: " + always_show);
chrome.storage.local.set({always_show: always_show});
} else {
user_id = items.user_id;
always_show = items.always_show;
}
console.log("user id: " + user_id);
console.log("always show: " + always_show);
var values = '"values": [';
// these have not been saved yet
// save them
// split comment text by newlines
var comments = items.comment_text.split("\n");
console.log("comments:");
console.log(comments);
var rubric_number = items.comments_rubric_number;
var inserted_comments = items.comments_inserted;
console.log("inserted commentS:");
console.log(inserted_comments);
// for each comment, append it to spreadsheet:
for (var i = 0; i < comments.length; i++) {
var already_there = false;
var comment = comments[i];
// ignore empty lines
if (comment != "") {
// delete -'s at the start of lines
if (comment[0] == "-") {
if (comment[1] == " ") {
comment = comment.substr(2);
} else {
comment = comment.substr(1);
}
}
console.log("comment:")
console.log(comment);
// check if comment == any of the inserted comments
for (var comment_id in inserted_comments) {
console.log("comparing:")
console.log(comment);
console.log(inserted_comments[comment_id]);
if (comment == inserted_comments[comment_id]) {
console.log("equal!");
inserted_comments[comment_id] = "";
already_there = true;
}
}
if (already_there) continue;
var comment_length = comment.split(" ").length;
// rubric question number (b) = items.rubric_number
// if comment = one of the inserted comments, original id (e) = that inserted comment's id
// then remove that comment from inserted comments
// comments (f) = comment
// length (g) = comment.split(" ").length
// category (h) = 0
// frequency (i) = 1
// frequency orig (j) = 1
values += '[ "", "' + rubric_number + '", "", "", "", "' +
comment + '", "' + comment_length + '", "0", "1", "1", "", "' + user_id + '"' +
' ],'
}
}
// if any comments left in inserted comments, just append them and figure it out manually
// be sure to include original comment id
for (var comment_id in inserted_comments) {
if (inserted_comments[comment_id] != "") {
values += '[ "", "' + rubric_number + '", "", "", "' + comment_id + '", "' +
inserted_comments[comment_id] + '", "' + inserted_comments[comment_id].split(" ").length +
'", "0", "1", "1", "", "' + user_id + '"' +
' ],'
}
}
values = values.slice(0, values.length-1) + ']';
if (values != '"values": ]') {
appendCommentsToSheet(values);
}
// set saved to true so they don't get saved again
chrome.storage.local.set({saved: true});
}
});
}
function appendCommentsToSheet(values) {
console.log(values);
// save to spreadsheet
chrome.identity.getAuthToken({interactive: true}, function(token) {
if (token) {
console.log("got the token");
// use that access token to set an http header when calling the Drive API.
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if(xhr.readyState === XMLHttpRequest.DONE && xhr.status == 200) {
// request done, we sent the data
console.log("Added new comments")
} else if (xhr.readyState === XMLHttpRequest.DONE) {
console.log(xhr.responseText);
}
};
xhr.open("POST",
"https://sheets.googleapis.com/v4/spreadsheets/" + comment_sheet_id +
"/values/W16-A10!A2:L10000:append?valueInputOption=RAW",
true);
xhr.setRequestHeader('Authorization','Bearer ' + token);
xhr.setRequestHeader("Content-type", "application/json");
xhr.send('{' +
'"range": "W16-A10!A2:L10000",' +
values +
'}');
}
});
}
// keyboard shortcut to modify always show setting
chrome.commands.onCommand.addListener(function(command) {
if (command == "toggle-suggestion-view") {
// get current always show setting
chrome.storage.local.get(null, function(items) {
always_show = items.always_show;
// now change it
always_show = !always_show;
chrome.storage.local.set({always_show: always_show});
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, "always show changed", function(response) {
console.log("sent always show message: " + response);
});
});
console.log("always show: " + always_show);
});
}
});
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
if (changeInfo.status == "complete") {
if (on_grading_page) {
// get the text they entered for this submission and send it to spreadsheet
saveNewComment();
}
}
});
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.action == "loadSpreadsheet") {
loadSpreadsheet();
sendResponse("done");
} else if (request.action == "logEvent") {
updateSheets("comment", request.rubric_question, request.rubric_item, request.comment_info, request.comment);
sendResponse("event logged");
} else if (request.action == "logShowSetting") {
updateSheets("change setting");
sendResponse("event logged");
} else if (request.action == "logSuggestionSee") {
updateSheets("show suggestions", request.rubric_question, request.rubric_item);
sendResponse("event logged");
} else if (request.action == "logSuggestionHide") {
updateSheets("hide suggestions", request.rubric_question, request.rubric_item);
sendResponse("event logged");
} else if (request.action == "logFocus") {
updateSheets("focus", request.rubric_question, request.rubric_item);
sendResponse("event logged");
} else if (request.action == "logGradescopeFocus") {
updateSheets("gradescope focus", request.rubric_question);
sendResponse("event logged");
} else if (request.action == "onGradingPage") {
on_grading_page = true;
} else if (request.action == "onOtherPage") {
on_grading_page = false;
}
});