code-sensei/sensei-grading-bookmarklet/app.js

135 lines
4 KiB
JavaScript
Raw Normal View History

2021-01-02 17:30:47 -05:00
const defaultIdleTimeout = 5e3,
2021-01-02 18:07:29 -05:00
assignmentLoadDelay = 1e3,
previewLoadDelay = 8e3,
submitLoadDelay = 5e3,
selectors = {
incompleteBtn: "#IsIncompleteYes",
notesTextarea:
"#gradingNode > div > div:nth-child(1) > table > tbody > tr:nth-child(8) > td:nth-child(2) > textarea",
star1:
"#gradingNode > div > div:nth-child(1) > table > tbody > tr:nth-child(7) > td:nth-child(2) > span.pull-left.ml-3.mt-1.stars > i:nth-child(1)",
star2:
"#gradingNode > div > div:nth-child(1) > table > tbody > tr:nth-child(7) > td:nth-child(2) > span.pull-left.ml-3.mt-1.stars > i:nth-child(2)",
star3:
"#gradingNode > div > div:nth-child(1) > table > tbody > tr:nth-child(7) > td:nth-child(2) > span.pull-left.ml-3.mt-1.stars > i:nth-child(3)",
submit: "#grade-form > button",
previewContainer: "#gradingNode > div > div:nth-child(2)",
assignmentBtn:
"body > div.body-content > div:nth-child(2) > div:nth-child(3) > table > tbody > tr > td:nth-child(7) > a",
assignmentLink: "#gradingNode > div > div:nth-child(2) > div > a",
selectedRow:
"body > div.body-content > div:nth-child(2) > div:nth-child(3) > table > tbody > tr.selected",
startGame:
"body > div.body-content > div:nth-child(2) > div > div.padding-left-3.padding-right-3 > div > div > div.col-xs-6.text-center > div:nth-child(3) > button:nth-child(1)",
};
2021-01-02 17:30:47 -05:00
const setIdleTimeout = (...args) => {
let timeoutId = setTimeout(...args);
const alertUserAction = () => {
clearTimeout(timeoutId);
timeoutId = setTimeout(...args);
};
return alertUserAction;
};
const actions = {
incomplete() {
// Sensei declares game incomplete.
document.querySelector(selectors.incompleteBtn).click();
const textarea = document.querySelector(selectors.notesTextarea);
textarea.focus();
2021-01-12 19:55:48 -05:00
isTypingFeedback = true;
2021-01-02 17:30:47 -05:00
const updateTextarea = setIdleTimeout(
2021-01-12 19:55:48 -05:00
() => {
document.querySelector(selectors.submit).focus();
isTypingFeedback = false;
},
2021-01-02 17:30:47 -05:00
defaultIdleTimeout
);
textarea.addEventListener("keypress", updateTextarea);
},
rate(stars) {
// Sensei gives a rating out of 3 stars.
const selectorKey = `star${stars}`;
if (selectorKey in selectors) {
document.querySelector(selectors[selectorKey]).click();
this.submit();
}
},
2021-01-02 18:07:29 -05:00
skip() {
document.querySelector(selectors.selectedRow).remove();
setTimeout(() => {
this.nextAssignment();
}, submitLoadDelay);
},
2021-01-02 17:30:47 -05:00
submit() {
document.querySelector(selectors.submit).click();
2021-01-02 18:07:29 -05:00
this.skip();
2021-01-02 17:30:47 -05:00
},
nextAssignment() {
2021-01-02 18:07:29 -05:00
document.querySelector(selectors.assignmentBtn).click();
2021-01-02 17:30:47 -05:00
setTimeout(() => {
const preview = document.createElement("iframe");
preview.width = "1600px";
preview.height = "1400px";
preview.src = document.querySelector(selectors.assignmentLink).href;
2021-01-02 18:07:29 -05:00
preview.addEventListener("load", () => {
setTimeout(() => {
preview.contentDocument.querySelector(selectors.startGame).click();
}, previewLoadDelay);
});
2021-01-02 17:30:47 -05:00
document.querySelector(selectors.previewContainer).innerHTML = "";
document.querySelector(selectors.previewContainer).appendChild(preview);
document
.querySelector(selectors.previewContainer)
.parentElement.scrollIntoView();
}, assignmentLoadDelay);
},
};
document.addEventListener("keypress", (event) => {
2021-01-12 19:55:48 -05:00
if (isTypingFeedback) {
return;
}
2021-01-02 17:30:47 -05:00
try {
switch (event.key) {
case "i":
actions.incomplete();
break;
case "1":
actions.rate(1);
break;
case "2":
actions.rate(2);
break;
case "3":
actions.rate(3);
break;
2021-01-02 18:07:29 -05:00
case "s":
actions.skip();
break;
2021-01-02 17:30:47 -05:00
case "enter":
actions.submit();
break;
default:
break;
}
} catch (error) {
console.log(`Sensei Error: ${error}`);
}
});
const main = () => {
actions.nextAssignment();
};
2021-01-12 19:55:48 -05:00
let isTypingFeedback = false;
2021-01-02 17:30:47 -05:00
main();