|
/* run this in the console on the ClassDojo page */ |
|
|
|
function download(url, prefix) { |
|
fetch(url).then(function(t) { |
|
return t.blob().then((b)=> { |
|
var a = document.createElement("a"); |
|
a.href = URL.createObjectURL(b); |
|
var n = url.lastIndexOf('/'); |
|
var filename = url.substring(n + 1); |
|
a.setAttribute("download", prefix+filename); |
|
a.click(); |
|
} |
|
); |
|
}); |
|
} |
|
|
|
function eventFire(el, etype){ |
|
if (el.fireEvent) { |
|
el.fireEvent('on' + etype); |
|
} else { |
|
var evObj = document.createEvent('Events'); |
|
evObj.initEvent(etype, true, false); |
|
el.dispatchEvent(evObj); |
|
} |
|
} |
|
|
|
function parseDojoDate(postDate) { |
|
|
|
if(postDate.includes('ago')) { |
|
dateStringOut = new Date().toLocaleDateString('en-ca'); |
|
} else { |
|
|
|
if(postDate.split(' ').length == 3) { |
|
postYear = postDate.split(' ')[2]; |
|
} else { |
|
postYear = new Date().getFullYear() |
|
} |
|
|
|
switch (postDate.split(' ')[0]) { |
|
case 'Jan': |
|
postMonth = '01'; |
|
break; |
|
case 'Feb': |
|
postMonth = '02'; |
|
break; |
|
case 'Mar': |
|
postMonth = '03'; |
|
break; |
|
case 'Apr': |
|
postMonth = '04'; |
|
break; |
|
case 'May': |
|
postMonth = '05'; |
|
break; |
|
case 'Jun': |
|
postMonth = '06'; |
|
break; |
|
case 'Jul': |
|
postMonth = '07'; |
|
break; |
|
case 'Aug': |
|
postMonth = '08'; |
|
break; |
|
case 'Sep': |
|
postMonth = '09'; |
|
break; |
|
case 'Oct': |
|
postMonth = '10'; |
|
break; |
|
case 'Nov': |
|
postMonth = '11'; |
|
break; |
|
case 'Dec': |
|
postMonth = '12'; |
|
break; |
|
} |
|
|
|
postDay = ('00' + postDate.split(' ')[1].replace(',', '')).slice(-2); |
|
dateStringOut = postYear + '-' + postMonth + '-' + postDay; |
|
} |
|
|
|
return dateStringOut; |
|
} |
|
|
|
var els = document.querySelectorAll('[data-test-name="storyPostContents"]'); |
|
|
|
els.forEach( |
|
function(currentValue) { |
|
|
|
firstImage = true; |
|
lastImage = false; |
|
|
|
// loop in order to catch each image in a set |
|
while(firstImage == true || lastImage == false) { |
|
|
|
// Get content node |
|
contentNode = currentValue.childNodes[0].childNodes[0]; |
|
|
|
// Get content for file name |
|
var headerNode = currentValue.parentNode.querySelector('[data-test-name="storyPostHeader"]'); |
|
var metadata = headerNode.querySelectorAll('.nessie-text') |
|
var teacher = metadata[0].innerText; |
|
var className = metadata[1].innerText; |
|
var postDate = metadata[2].innerText; |
|
|
|
var filePrefix = parseDojoDate(postDate) + ' - ' + className + ' - ' + teacher + " "; |
|
|
|
console.log("Processing: " + filePrefix); |
|
|
|
// download image and video |
|
var video_els = contentNode.querySelector('[type="video/mp4"]'); |
|
if(video_els !== null) { |
|
download(video_els.src, filePrefix); |
|
} |
|
|
|
url = window.getComputedStyle(contentNode).getPropertyValue("background-image"); |
|
if(url != 'none') { |
|
download(url.slice(5, -8), filePrefix); |
|
} |
|
|
|
// check if there are more images |
|
numDivs = contentNode.parentNode.childNodes.length; |
|
|
|
// click or set lastImage to true as necessary |
|
switch(numDivs) { |
|
case 1: |
|
lastImage = true; |
|
break; |
|
case 3: |
|
if (firstImage == true) { |
|
lastImage = false; |
|
eventFire(contentNode.nextSibling, 'click'); |
|
} else { |
|
lastImage = true; |
|
} |
|
break; |
|
case 4: |
|
eventFire(contentNode.nextSibling.nextSibling, 'click'); |
|
break; |
|
default: |
|
console.log('Unexpected post structure. ' + numDivs + ' divs found. Check for missing content.'); |
|
lastImage = true; |
|
break; |
|
} |
|
|
|
firstImage = false; |
|
} |
|
|
|
// remove the node to simplify the page; this is useful for debugging because it allows you run a few items at a time. |
|
//currentValue.remove() |
|
}); |
|
|
When I run this code in my Mac Chrome's console, the fails with the error
Undefined
.I really dont know what is going wrong bcoz I know nothing about JS.
I am using this https://gist.github.com/Patrick330/aa5d16efaacaee9ffb76e52ba4fb86f6.
Can @Patrick330, @travishorn or anyone else help me here??