Skip to content

Commit

Permalink
Merge pull request #172 from aopell/develop
Browse files Browse the repository at this point in the history
Fixed some issues that broke what-if grades
  • Loading branch information
aopell authored Mar 4, 2020
2 parents 1ea6395 + a406e57 commit 903965a
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 18 deletions.
19 changes: 17 additions & 2 deletions js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ chrome.runtime.onMessage.addListener(
}

return finalResponse;
})().then(x => sendResponse(x)).catch(err => sendResponse({success: false, error: err}));
})().then(x => sendResponse(x)).catch(err => sendResponse({ success: false, error: err }));

return true;
}
Expand Down Expand Up @@ -298,4 +298,19 @@ function getBrowser() {

function createLogPrefix(color) {
return `color:${color};border:1px solid #2A2A2A;border-radius:100%;font-size:14px;font-weight:bold;padding: 0 4px 0 4px;background-color:#2A2A2A`;
}
}

chrome.webRequest.onHeadersReceived.addListener(details => {
let exists = false;
details.responseHeaders.map(item => {
if (item.name.toLowerCase() === 'access-control-allow-origin') {
item.value = '*';
exists = true;
}
});
if(!exists) {
details.responseHeaders.push({name: "access-control-allow-origin", value: "*"});
details.responseHeaders.push({name: "access-control-allow-headers", value: "Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers, Authorization"});
}
return { responseHeaders: details.responseHeaders };
}, { urls: ['*://*.schoology.com/*'] }, ['blocking', 'responseHeaders', 'extraHeaders']);
59 changes: 49 additions & 10 deletions js/grades.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ const timeout = ms => new Promise(res => setTimeout(res, ms));
const BUG_REPORT_FORM_LINK = "https://docs.google.com/forms/d/e/1FAIpQLScF1_MZofOWT9pkWp3EfKSvzCPpyevYtqbAucp1K5WKGlckiA/viewform?entry.118199430=";
const SINGLE_COURSE = window.location.href.includes("/course/");
var editDisableReason = null;
var invalidCategories = [];

function addEditDisableReason(err = "Unknown Error") {
function addEditDisableReason(err = "Unknown Error", causedBy403 = false) {
if (!editDisableReason) {
editDisableReason = { version: chrome.runtime.getManifest().version, errors: [] };
editDisableReason = { version: chrome.runtime.getManifest().version, errors: [], allCausedBy403: causedBy403 };
}
editDisableReason.errors.push(err);
editDisableReason.allCausedBy403 = editDisableReason.allCausedBy403 && causedBy403;
}

$.contextMenu({
Expand Down Expand Up @@ -127,6 +129,7 @@ var fetchQueue = [];
p.textContent = "0%";
p.title = "Assignment missing";
Logger.log(`Fetching max points for assignment ${assignment.dataset.id.substr(2)}`);

let json = await fetchApiJson(`/sections/${courseId}/assignments/${assignment.dataset.id.substr(2)}`);

let pts = Number.parseFloat(json.max_points);
Expand All @@ -146,6 +149,7 @@ var fetchQueue = [];
$(assignment).contextMenu({ x: event.pageX, y: event.pageY });
}
});
kabobMenuButton.dataset.parentId = assignment.dataset.parentId;

let editEnableCheckbox = document.getElementById("enable-modify");

Expand Down Expand Up @@ -184,6 +188,7 @@ var fetchQueue = [];
width: 12,
style: `display: ${checkbox && checkbox.checked ? "unset" : "none"};`
});
editGradeImg.dataset.parentId = assignment.dataset.parentId;
let gradeAddEditHandler = null;
if (assignment.classList.contains("grade-add-indicator")) {
// when this is clicked, if the edit was successful, we don't have to worry about making our changes reversible cleanly
Expand Down Expand Up @@ -230,11 +235,26 @@ var fetchQueue = [];
try {
await processAssignment(assignment);
} catch (err) {
addEditDisableReason({ error: { message: err.message, name: err.name, stack: err.stack, full: err.toString() }, courseId, course: title.textContent, assignment: assignment.textContent });
if (!assignment.classList.contains("dropped") && assignment.querySelector(".missing")) {
// consequential failure: our denominator is invalid
invalidateCatTotal = true;
invalidCategories.push(category.dataset.id);

if ("status" in err && err.status === 403) {
addEditDisableReason({
error: {
message: err.error,
status: err.status
},
courseId,
course: title.textContent,
assignment: assignment.textContent
}, true);
continue;
}
}

addEditDisableReason({ error: { message: err.message, name: err.name, stack: err.stack, full: err.toString() }, courseId, course: title.textContent, assignment: assignment.textContent });
Logger.error("Error loading assignment for " + courseId + ": ", assignment, err);
}
}
Expand Down Expand Up @@ -453,22 +473,31 @@ var fetchQueue = [];
let droppedAssignRClickSelector = ".item-row.dropped:not(.grade-add-indicator)";

// any state change when editing has been disabled
if (editDisableReason) {
if (editDisableReason && !editDisableReason.allCausedBy403) {
Logger.error("Editing disabled due to error", editDisableReason);
let formLink = `${BUG_REPORT_FORM_LINK}${encodeURI(JSON.stringify(editDisableReason))}`

if (confirm("Grade editing has been disabled due to an error. Would you like to report this issue? (It will help us fix it faster!)")) {
window.open(formLink, "_blank");
window.open(`${BUG_REPORT_FORM_LINK}${encodeURI(JSON.stringify(editDisableReason))}`, "_blank");
}

document.getElementById("enable-modify").checked = false;
}
// enabling editing
else if (document.getElementById("enable-modify").checked) {
if (editDisableReason && editDisableReason.allCausedBy403) {
if (confirm("WARNING!!!\n\nYou have one or more missing assignments for which the total points are unknown due to restrictions put in place by your teacher. Grade editing may work in some categories if this is a weighted gradebook, however it will be disabled in others. We are working on a fix for this issue, but until then please click 'OK' to submit a bug report so we can gague how large this problem is. Thank you!")) {
window.open(`${BUG_REPORT_FORM_LINK}${encodeURI(JSON.stringify(editDisableReason))}`, "_blank");
}
}

for (let edit of document.getElementsByClassName("grade-edit-indicator")) {
if(invalidCategories.includes(edit.dataset.parentId)) continue;

edit.style.display = "unset";
}
for (let edit of document.getElementsByClassName("grade-add-indicator")) {
if(invalidCategories.includes(edit.dataset.parentId)) continue;

edit.style.display = "table-row";
if (edit.previousElementSibling.classList.contains("item-row") && edit.previousElementSibling.classList.contains("last-row-of-tier")) {
edit.previousElementSibling.classList.remove("last-row-of-tier");
Expand Down Expand Up @@ -829,6 +858,8 @@ var fetchQueue = [];
});

for (let kabob of document.getElementsByClassName("kabob-menu")) {
if(invalidCategories.includes(kabob.dataset.parentId)) continue;

kabob.classList.remove("hidden");
}
// uncheck the grades modify box without having modified grades
Expand Down Expand Up @@ -919,7 +950,7 @@ var fetchQueue = [];
Logger.log(`Fetching max points for (nonentered) assignment ${assignment.dataset.id.substr(2)}`);
let response = await fetchApi(`/sections/${courseId}/assignments/${assignment.dataset.id.substr(2)}`);
if (!response.ok) {
throw new Error(response.statusText);
throw { status: response.status, error: response.statusText };
}
let json = await response.json();

Expand Down Expand Up @@ -1403,16 +1434,24 @@ function createAddAssignmentElement(category) {
return addAssignmentThing;
}

function processNonenteredAssignments(sleep) {
function processNonenteredAssignments(sleep, attempts = 0) {
if (attempts > 3) {
// Remove the first element
fetchQueue.shift();
attempts = 0;
}
if (fetchQueue.length > 0) {
setTimeout(() => {
fetchQueue[0]().then(x => {
fetchQueue.splice(0, 1);
processNonenteredAssignments();
}).catch(err => {
Logger.warn("Caught error: " + err);
Logger.warn("Caught error: ", err);
Logger.log("Waiting 3 seconds to avoid rate limit");
processNonenteredAssignments(true);
if ("status" in err && err.status === 403) {
attempts = 100;
}
processNonenteredAssignments(true, attempts + 1);
});
}, sleep ? 3000 : 0);
}
Expand Down
16 changes: 13 additions & 3 deletions js/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function backgroundPageFetch(url, init, bodyReadType) {
if (bodyReadError === true) {
readBodyReject();
} else {
readBodyReject(bodyReadError);
readBodyReject({ status: response.status, bodyReadError: bodyReadError });
}
} else {
readBodyResolve(bodyContent);
Expand Down Expand Up @@ -176,7 +176,17 @@ async function fetchWithApiAuthentication(url, baseObj, useRateLimit = true, bod
* @param {string} path The API path, e.g. "/sections/12345/assignments/12"
*/
async function fetchApiJson(path) {
return await (await fetchApi(path)).json();
let response;
try {
response = await fetchApi(path);
}
catch (err) {
throw err;
}
if (!response.ok) {
throw response;
}
return await response.json();
}

/**
Expand Down Expand Up @@ -615,7 +625,7 @@ function updateSettings(callback) {
value => {
setCSSVariable("overdue-assignments-display", "block");
setCSSVariable("upcoming-assignments-display", "block");
switch(value) {
switch (value) {
case "hideUpcoming":
setCSSVariable("upcoming-assignments-display", "none");
break;
Expand Down
7 changes: 7 additions & 0 deletions js/version-specific.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,13 @@ let migrationsTo = {
]
}
);
},
"5.8": function(currentVersion, previousVersion) {
showToast(
"What-If Grades Fixed",
"A few errors affecting what-if grades have been fixed and we're working on fixing more! Thanks for submitting bugs!",
"rgb(255,0,255)"
);
}
};

Expand Down
8 changes: 5 additions & 3 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"update_url": "https://aopell.me/SchoologyPlus/update.json"
}
},
"version": "5.7",
"version": "5.8",
"icons": {
"128": "imgs/[email protected]",
"64": "imgs/[email protected]",
Expand All @@ -34,7 +34,9 @@
"storage",
"alarms",
"notifications",
"cookies"
"cookies",
"webRequest",
"webRequestBlocking"
],
"web_accessible_resources": [
"imgs/*",
Expand All @@ -47,7 +49,7 @@
"scripts": [
"js/background.js"
],
"persistent": false
"persistent": true
},
"content_scripts": [
{
Expand Down

0 comments on commit 903965a

Please sign in to comment.