-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremote.js
362 lines (327 loc) · 9.72 KB
/
remote.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
// primitive encryption functions. this'll get 'em!
String.prototype.rot13 = function (){
return this.replace(/[a-zA-Z]/g, function(c){
return String.fromCharCode((c <= "Z" ? 90 : 122) >= (c = c.charCodeAt(0) + 13) ? c : c - 26);
});
};
String.prototype.b64enc = function () {
return Base64.encode(this);
};
String.prototype.b64dec = function () {
return Base64.decode(this);
}
String.prototype.encrypt = function () {
return this.b64enc().rot13();
};
String.prototype.decrypt = function () {
return this.rot13().b64dec();
}
/**
* A queue of XHRs
* @namespace
*/
Queuer = (function () {
/** @constructor */
function Queuer() {
this.queue = [];
};
/**
* Adds an XHR to the queue
* @param {XMLHttpRequest} xhr - the request to add
* @returns the Queuer
* @namespace Queuer
*/
Queuer.prototype.addRequest = function (xhr) {
this.queue.push(xhr);
return this;
};
/**
* Aborts all of the XHRs in the queue
* @returns the Queuer
* @namespace Queuer
*/
Queuer.prototype.abortAll = function () {
while (this.queue.length) {
this.queue[0].abort();
this.queue.splice(0, 1);
}
return this;
};
return Queuer;
})();
/** @type Queuer */
var XHR_Queue = new Queuer();
/**
* Logs an error to the console
* @param {XMLHttpRequest} jqXHR - the request that threw the error
* @param {string} textStatus - the type of error thrown
* @param {string} errorThrown - a description of the error
*/
function default_error_handler(jqXHR, textStatus, errorThrown) {
console.error(jqXHR, textStatus, errorThrown)
}
/**
* Manages the time of the last update
* @namespace
*/
var Updater = {
/**
* Saves the current time to localStorage as the last updated time
*/
set_updated: function () {
localStorage.setItem("lastupdated", (new Date()).getTime());
},
/**
* Gets the relative time of the last update based on localStorage
* @returns {string} relative time of the last update
*/
get_update_text: function () {
return moment(parseInt(localStorage["lastupdated"])).fromNow();
}
};
/**
* Generates useful links for the user
*/
var Ad = {
host: "https://hacaccess.herokuapp.com",
/**
* Creates the most relevant link
* @returns {Element} a link to append to the marking period grades table
*/
generate_ad: function () {
// update from server if necessary
var curr_time = +new Date;
var get_time = parseInt(localStorage["ad_updatetime"]);
if (isNaN(get_time) || (curr_time - get_time > 24 * 60 * 60 * 1000))
Ad.fetch_ad();
// create the ad
var ad = localStorage["ad_" + localStorage["ad"]];
if (ad == "no") return document.createDocumentFragment();
else {
try {
ad_json = JSON.parse(ad);
} catch (e) {
return document.createDocumentFragment();
}
return Ad.generate_ad_inner(ad_json.text, ad_json.url, ad_json.id);
}
},
/**
* Generates a formatted link
* @param {string} text - the text of the link
* @param {string} url - the destination of the link
* @param {string} id - a unique identifier to send to Google Analytics when
* the link is clicked
*/
generate_ad_inner: function (text, url, id) {
var wrapper = document.createElement("div");
wrapper.setAttribute("id", "ad_wrapper");
// create main ad
var ad;
if (url == "") {
ad = document.createElement("span");
$(ad).attr("id", "ad").html(text);
} else {
ad = document.createElement("a");
$(ad).attr("id", "ad").data("label", id)
.click(function () {
chrome.tabs.create({"url": url});
var label = $(this).data("label");
if (label != "")
_gaq.push(['_trackEvent', 'Ad', 'Follow ' + label]);
})
.html(text);
}
wrapper.appendChild(ad);
// create link to hide ad
var hideAd = document.createElement("a");
$(hideAd).attr({"id": "hide_ad", "href": "#"}).data("label", id)
.html("×").click(function () {
var label = $(this).data("label");
if (label != "")
_gaq.push(['_trackEvent', 'Ad', 'Hide ' + label]);
$("#ad_wrapper").remove();
localStorage.setItem("ad_" + label, "no");
});
wrapper.appendChild(hideAd);
return wrapper;
},
/**
* Fetches ad content from the qHAC server and caches it
*/
fetch_ad: function() {
$.get(Ad.host + "/api/ad/latest", function(data) {
var ad = JSON.parse(data);
if (localStorage["ad_" + ad.id] == "no") return;
localStorage["ad"] = ad.id;
localStorage["ad_" + ad.id] = data;
localStorage["ad_updatetime"] = +new Date;
});
}
};
/**
* Interfaces with the Round Rock ISD Home Access Center
* @namespace
*/
var RRISD_HAC = {
/**
* Requests a session ID from Home Access by logging in
* @param {string} login - username
* @param {string} pass - password
* @param {string} id - 6-digit district-provided student ID
* @param {function} callback - a function to call if the request succeeds
* @param {function} on_error a funciton to call if the request fails
*/
get_session: function (login, pass, id, callback, on_error) {
GradeRetriever.login(
Districts.roundrock,
login,
pass,
function (doc, $dom, choices, state) {
window.session_id = true;
if (choices == null)
callback(doc);
else
GradeRetriever.disambiguate(
Districts.roundrock,
id,
state,
callback)
},
on_error
);
},
load_session: function (callback) {
if (window.session_id == true || typeof localStorage["url"] != "undefined")
callback();
else
RRISD_HAC.get_session(
localStorage["login"].decrypt().decrypt(),
localStorage["rrisd_password"].decrypt().decrypt(),
localStorage["studentid"].decrypt().decrypt(),
callback);
},
/**
* Retrieves grades from RRISD
* @param {string} url - the unique student URL that accesses grades
* @param {function} callback - a function to call when the request succeeds
* @param {function} on_error - a function to call when the request fails
*/
get_gradesHTML: function (url, callback, on_error) {
if (typeof url == "undefined")
GradeRetriever.getAverages(Districts.roundrock, function(doc) {
window.doc = doc;
callback(doc);
}, on_error);
else {
var jqXHR = $.ajax({
url: "https://gradebook.roundrockisd.org/pc/displaygrades.aspx?studentid=" + url,
type: "GET"
// dataType: "text gjson"
}).done(callback).fail(on_error || default_error_handler);
XHR_Queue.abortAll().addRequest(jqXHR);
}
},
/**
* Retrieves class grades from RRISD
* @param {string} sID - the unique student URL that accesses grades
* @param {string} data - the class and marking period identifier
* @param {function} callback - a function to call when the request succeeds
* @param {function} on_error - a function to call when the request fails
*/
get_classGradeHTML: function (sID, data, callback, on_error) {
if (typeof sID == "undefined")
// overall grades need to be loaded first; load those if haven't already
if (typeof window.doc == "undefined")
RRISD_HAC.get_gradesHTML(undefined, function() {
RRISD_HAC.get_classGradeHTML(sID, data, callback, on_error);
}, on_error);
else
GradeRetriever.getClassGrades(Districts.roundrock, decodeURIComponent(data), window.doc, callback);
else {
var jqXHR = $.ajax({
url: "https://gradebook.roundrockisd.org/pc/displaygrades.aspx?data=" + data + "&studentid=" + sID,
type: "GET"
// dataType: "text cjson",
// data: {
// "data": data,
// "studentid": sID
// }
}).done(function (doc) {
if (doc == "Could not decode student id.") {
console.log("Error while fetching class grades (could not decode student ID)");
on_error(new Error("Unable to decode student ID"));
return false;
}
callback(doc);
}).fail(on_error || default_error_handler);
XHR_Queue.abortAll().addRequest(jqXHR);
}
}
};
/**
* Interfaces with the Austin ISD Gradespeed (Home Access Center)
*/
var AISD_HAC = {
/**
* Requests a session ID from Home Access by logging in
* @param {string} login - username
* @param {string} pass - password
* @param {string} id - 6-digit district-provided student ID
* @param {function} callback - a function to call if the request succeeds
* @param {function} on_error a funciton to call if the request fails
*/
get_session: function (login, pass, id, callback, on_error) {
GradeRetriever.login(
Districts.austin,
login,
pass,
function (doc, $dom, choices, state) {
window.session_id = true;
if (choices == null)
callback(doc);
else
GradeRetriever.disambiguate(
Districts.austin,
id,
state,
callback)
},
on_error
);
},
/**
* Calls a function, ensuring that window.session_id has been set to an
* AISD session ID.
* @param {function} callback - the function to call once the session
* has been loaded
*/
load_session: function (callback) {
if (window.session_id == true)
callback();
else
AISD_HAC.get_session(
localStorage["login"].decrypt().decrypt(),
localStorage["aisd_password"].decrypt().decrypt(),
localStorage["studentid"].decrypt().decrypt(),
callback);
},
/**
* Retrieves grades from AISD
* @param {string} id - a valid AISD session ID
* @param {function} callback - a function to call when the request succeeds
*/
get_gradesHTML: function (id, studentid, callback, on_error) {
GradeRetriever.getAverages(Districts.austin, callback, on_error);
},
/**
* Retrieves class grades from AISD
* @param {string} id - a valid AISD session ID
* @param {string} studentid - the 7-digit district-assigned student ID
* @param {string} data - the class and marking period identifier
* @param {function} callback - a function to call when the request succeeds
*/
get_classGradeHTML: function (id, studentid, data, callback, on_error) {
GradeRetriever.getClassGrades(Districts.austin, decodeURIComponent(data), undefined, callback, on_error);
}
};