Skip to content

Instantly share code, notes, and snippets.

@john-hix
Last active June 24, 2020 13:24
Show Gist options
  • Save john-hix/7ea4b59cf73940aff42139c16608ddcd to your computer and use it in GitHub Desktop.
Save john-hix/7ea4b59cf73940aff42139c16608ddcd to your computer and use it in GitHub Desktop.
Get IMathAS popup videos: Script to quickly scrape all the video helps for an IMathAS assignment. Copy the JSON response from startassess.php and assign it to the data variable. Works for sites like Lumen OHM, etc.
var data = {};
function normalizeUrl(/*string*/ mathIasPopupUrl) {
const url = new URL(mathIasPopupUrl);
// console.log(url.search);
const searchParams = new URLSearchParams(url.search);
// console.log(searchParams.get("url"));
return searchParams.get("url");
}
function dynamicallyLoadScript(url) {
var script = document.createElement("script"); // create a script DOM node
script.src = url; // set its src to the provided URL
document.head.appendChild(script); // add it to the end of the head section of the page (could change 'head' to 'body' to add it to the end of the body section instead)
}
dynamicallyLoadScript("https://cdn.jsdelivr.net/npm/url-polyfill@1.1.8/url-polyfill.min.js");
// lag enough to load the polyfill
setTimeout(function() {
let output = "\n"; // String to print
for (let q = 0; q < data.JSON.questions.length; q++) {
output += `Problem ${q+1}\n`;
let probVidCt = 0;
for (let i = 0; i < data.JSON.questions[q].jsparams.helps.length; i++) {
if (data.JSON.questions[q].jsparams.helps[i].label == "video") {
// console.log(data.JSON.questions[q].jsparams.helps[i].url); // DEBUG
output += normalizeUrl(data.JSON.questions[q].jsparams.helps[i].url);
output += "\n\n";
probVidCt++;
}
}
if (probVidCt == 0) {
output += `No help videos found for problem ${q+1}.\n\n`;
}
}
console.log(output);
}, 2000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment