Skip to content

Instantly share code, notes, and snippets.

@iarigby
Created November 24, 2019 15:56
Show Gist options
  • Save iarigby/3e23ca0f521687ede03f3545c68f4459 to your computer and use it in GitHub Desktop.
Save iarigby/3e23ca0f521687ede03f3545c68f4459 to your computer and use it in GitHub Desktop.
submit grades to Google Classroom
// ew vars but otherwise not easy to copy paste to console while changing
var resultsListElement="DC55n"
var nameDiv="YVvGBb"
var gradeSpan="cL8LOd" // [0]
// declare const students, csv string
function main() {
const marks = parseCsv(studentsCSV)
const results = getStudents()
.filter(needsChecking)
.map(e => createResult(e, marks))
// cant use for of, map because need to break on first occurence
// this is technically submitNextGrade
for (let i = 0; i < results.length; i++) {
if (submitGrade(results[i])) {
i = results.len
}
}
// executeAtInterval(results,
// (results, index) => submitGrade(results[index]))
}
function createResult(element, results) {
const name = getStudentName(element)
const grade = getGrade(results, name)
return {
"element": element,
"grade": grade
}
}
function getNthElement(element, className, index) {
return Array.from(element.getElementsByClassName(className))[index]
}
function getFirstElement(element, className) {
return getNthElement(element, className, 0)
}
function submitGrade(resultsObject) {
const elem = resultsObject.element
const grade = resultsObject.grade
const gradeElem = getNthElement(elem, gradeSpan, 0)
if (gradeElem) gradeElem.click()
const inputElem = getFirstElement(elem, "whsOnd zHQkBf")
if (inputElem) {
inputElem.value = grade
return true
}
// getFirstElement(elem, "uHMk6b fsHoPb").click()
}
function needsChecking(studentElem) {
const lateDiv = getFirstElement(studentElem, "ppMo6b YVvGBb")
const missingDiv = getFirstElement(studentElem, "YVvGBb lYU7F foVBo")
const draftDiv = getFirstElement(studentElem, "ppMo6b w3SmKe YVvGBb")
return lateDiv.innerHTML!="Done late" && !missingDiv && !draftDiv
}
function getStudents() {
return Array.from(document.getElementsByClassName(resultsListElement))
}
function getStudentName(element) {
const nameElement=Array.from(element.getElementsByClassName(nameDiv))[0]
return nameElement.innerHTML.toLowerCase()
}
function parseCsv(resultsCSV) {
const lines = resultsCSV.split("\n")
return lines
.map(createStudentObject)
}
function createStudentObject(line) {
const array = line.split(",")
return {
"id": array[0].toLowerCase(),
"grade": array[1]
}
}
function getGrade(results, name) {
let result = results.find(e => isName(e, name, 0, 1))
if (!result)
result = results.find(e => isName(e, name, 1, 0))
if (result)
return result.grade
else {
console.log(name, " no grade found")
return null
}
}
function isName(e, name, nameIndex, surnameIndex) {
const arr = name.split(" ")
if (arr.length < 2) return false
const firstname = arr[nameIndex]
const surname = arr[surnameIndex]
const surnamePrefix = e => e.id.includes(surname.substr(0,3))
return e.id[0] == firstname[0] && surnamePrefix(e)
}
main()
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment