-
Notifications
You must be signed in to change notification settings - Fork 0
/
getPagesSource.js
executable file
·212 lines (167 loc) · 6.81 KB
/
getPagesSource.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
/*
* This file is subject to the terms and conditions defined in
* file 'LICENSE', which is part of this source code package.
*/
function DOMtoString(document_root) {
var html = '',
node = document_root.firstChild;
while (node) {
switch (node.nodeType) {
case Node.ELEMENT_NODE:
html += node.outerHTML;
break;
case Node.TEXT_NODE:
html += node.nodeValue;
break;
case Node.CDATA_SECTION_NODE:
html += '<![CDATA[' + node.nodeValue + ']]>';
break;
case Node.COMMENT_NODE:
html += '<!--' + node.nodeValue + '-->';
break;
case Node.DOCUMENT_TYPE_NODE:
// (X)HTML documents are identified by public identifiers
html += "<!DOCTYPE " + node.name + (node.publicId ? ' PUBLIC "' + node.publicId + '"' : '') + (!node.publicId && node.systemId ? ' SYSTEM' : '') + (node.systemId ? ' "' + node.systemId + '"' : '') + '>\n';
break;
}
node = node.nextSibling;
}
//Get the content of the nested iframes
var iframe = document.querySelector('#mybbCanvas');
var iframe1Document = iframe.contentDocument;
var iframe2 = iframe1Document.querySelector('#right_stream_mygrades');
var iframe2Document = iframe2.contentDocument;
var elements = $(iframe2Document);
var header = $('#streamDetailHeaderRight', elements);
var title = header.find('.context').text();
//only take what in brackets
var ind1 = title.indexOf('(');
var ind2 = title.indexOf(')');
if(ind1 != -1 && ind2 != -1 && ind1 < ind2) {
//The title contains brackets
title = title.substr(ind1+1, ind2-ind1-1); //not including the brackets themselves
}
//console.log(title.text());
var gradesWrapper = $('#grades_wrapper', elements);
//Check for 'Total' row
var totalIsPresent = false;
var totalDiv = gradesWrapper.find("div[rowindex=2]");
var totalStr = totalDiv.find('.cell.gradable').text().trim();
if(totalStr == "Total") {
totalIsPresent = true;
}
//Get the overall grade (This will be displayed alongside the graph)
var overallPercentage;
if(totalIsPresent) {
var overallGrade = gradesWrapper.find("div[rowindex=2]");
var ovGrade = overallGrade.find('.grade[tabindex=0]');
var ovOutOf = overallGrade.find('.pointsPossible.clearfloats'); //This has a leading '/'
var ovGradeStr = $(ovGrade).text();
var ovOutOfStrS = $(ovOutOf).text();
var ovOutOfStr = ovOutOfStrS.substr(1); //remove '/'
var ovGradeFl = parseFloat(ovGradeStr);
var ovOutOfFl = parseFloat(ovOutOfStr);
var ovPercent = (ovGradeFl/ovOutOfFl) * 100;
overallPercentage = Math.round(ovPercent * 100) / 100; //Limit to 2 decimal places
console.log("Overall: " + ovGradeFl + "/" + ovOutOfFl + " =" + overallPercentage + "%");
//Add the total percentage to the title
// title = title + " Overall:" + ovPercentR + "%";
}
else {
console.log("Total not present.");
}
var subject = title;
var index = 0;
var objs = [];
//Loop through each singular result
$(gradesWrapper.find('.sortable_item_row.graded_item_row.row.expanded')).each(function() {
//console.log("index:" + $(this).attr("rowindex"));
//For each grade
var gradeTitle = "";
//The grade title can be contained in the immediate div or inside the div in an <a>
var gradeTitleTO = $(this).find(".cell.gradable");
var gradeTitleTOStr = $(gradeTitleTO).contents().get(0).nodeValue.trim();
var gradeTitleA = $(this).find("a[href='#']").text().trim();
if(gradeTitleTOStr != "") {
gradeTitle = gradeTitleTOStr;
}
else if(gradeTitleA != "") {
gradeTitle = gradeTitleA;
}
console.log(gradeTitle);
//get the status of the current grade
var status = $(this).find('span.activityType').text();
if(status == "Graded") {
//Only take results that have been graded
//Truncate a long assignment title
var label = gradeTitle.substr(0, 11);
if(label != gradeTitle) {
label = label + "..";
}
var date = $(this).find('.lastActivityDate').text();
var dateStr = date.substr(0, 11);
var grade = $(this).find('.grade[tabindex=0]');
var outOf = $(this).find('.pointsPossible.clearfloats'); //This has a leading '/'
var grade = parseFloat($(grade).text());
var outOfStr = $(outOf).text();
var outOf = parseFloat(outOfStr.substr(1)); //remove '/'
var percent = (grade/outOf) * 100;
//Check if % is > 100 --> Assignment may be worth extra marks
if(percent > 100) {
percent = 100;
}
percent = percent.toFixed(0);
//Check for any errors
if(isNaN(grade)) {
console.log("No grade available");
}
else if(isNaN(percent)) {
console.log("An error occured calculating the percentage. grade=" + grade + " outOf=" + outOf);
}
else {
console.log(grade + "/" + outOf + " = " + percent + "%");
}
console.log("Graded on: " + dateStr);
var gradeObj = {date:dateStr, percentage:percent, label:label};
objs[index] = gradeObj;
index++;
}
else {
console.log("Not graded");
}
});
//Sort the percentages, labels and dates arrays by date
objs.sort(function(a, b) {
return Date.parse(a.date) - Date.parse(b.date);
});
//console.log(objs);
console.log(iframe2Document);
//Create each array from the objs array
var dates = objs.map(function(a) {return a.date;});
var percentages = objs.map(function(a) {return a.percentage;});
var labels = objs.map(function(a) {return a.label;});
if(!totalIsPresent) {
//If the total is not present we can calculate overall percentage instead
var temp = 0;
for(var i in percentages) { temp += parseFloat(percentages[i]); }
overallPercentage = temp / percentages.length
//Round to 2 decimal places
overallPercentage = Math.round(overallPercentage * 100) / 100
}
var chartType = 'line';
//console.log(percentages.length);
if(percentages.length == 1){
chartType = 'bar';
}
console.log(chartType);
chrome.runtime.sendMessage({content: overallPercentage, type: "overallPercentage"});
chrome.runtime.sendMessage({content: percentages, type: "data"});
chrome.runtime.sendMessage({content: dates, type: "label"});
chrome.runtime.sendMessage({content: labels, type: "labsLabel"});
chrome.runtime.sendMessage({content: subject, type: "subject"});
chrome.runtime.sendMessage({content: chartType, type: "chartType"})
}
chrome.runtime.sendMessage({
action: "getSource",
source: DOMtoString(document)
});