Skip to content

Instantly share code, notes, and snippets.

@samuelkordik
Last active June 29, 2017 20:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samuelkordik/08d8c6da8b583c21e6fef2d84ee87ae5 to your computer and use it in GitHub Desktop.
Save samuelkordik/08d8c6da8b583c21e6fef2d84ee87ae5 to your computer and use it in GitHub Desktop.
Bookmarklets
javascript:(function(){
// select active modal's quiz key view
var e = document.querySelectorAll('.modal.fade.in .quiz-key')[0];
// set up object model to save to
var q = {};
q.titl = e.querySelector('h3').innerHTML;
q.contents = [];
var qi_c = 0;
var imagesList = [];
// set up individual answer object
var qa = {};
qa.answers = [];
var n = e.querySelectorAll('dt, dd');
for (var i = 0; i < n.length; i++) {
// loop through all questions and answers
if ( n[i].matches('dt') ) {
// match question stems
if (qa.question != null && qa.answers.length > 0) {
//move correct answer to front
qa.answers.sort(function(a,b){
return (a.correct === 'Correct') ? -1 : ( ( b.correct === 'Correct') ? 1 : 0);
});
q.contents.push(qa);
}
qa = {};
qa.answers = [];
qa.question = n[i].querySelector('p').innerHTML;
} else if ( n[i].matches('dd') ) {
// match question answers
var an = {};
an.txt = n[i].querySelector('p').innerHTML;
qa.answers.push(an);
an = {};
// } else if ( n[i].matches('.quiz-question-meta') ) {
// // match question feedback and remediation text
// switch (n[i].querySelector('strong').innerHTML) {
// case 'Feedback:':
// qa.feedback = n[i].querySelector('p');
// break;
// case 'Remediation:':
// qa.remediation = n[i].querySelector('p');
// break;
// }
// } else {
// // match question images
// qa.question_image = get_src(n[i]);
}
}
q.contents.push(qa)
/*
// Copy and paste JSON text
var j = JSON.stringify(q);
window.prompt("Copy to clipboard: Ctrl+C, Enter", j);
*/
// add download section
var s = document.createElement('div');
s.setAttribute('id', 'CLDownloadLinks');
s.innerHTML='<h3>Downloads</h3>';
e.insertBefore(s, e.querySelector('dt'));
//download all images
for (var i = 0; i < imagesList.length; i++) {
download(imagesList[i].URI, imagesList[i].note + ".jpg")
}
//download JSON output
download(JSON_Link(q), 'Export.json');
function get_src(e) {
// images get saved to a specific list with an assigned number; the number gets inserted in the appropriate question or answer spot.
if (e.querySelector('img') !== null) {
var img = {};
img.note = String(imagesList.length+1);
img.URI = e.querySelector('img').getAttribute('src');
imagesList.push(img);
return img.note;
} else {
return 'No Image';
}
}
function JSON_Link(q) {
return 'data:text/plain;charset=utf-8,' + encodeURIComponent(JSON.stringify(q));
};
function download(url, filename) {
// Create download link
var a = document.createElement('a');
a.href=url;
a.download = filename;
a.innerText= filename;
var p = document.createElement('p');
p.appendChild(a);
document.getElementById('CLDownloadLinks').appendChild(p);
}
})();
javascript:(function(){
var f = document.querySelector(".modal.fade.in #embed");
var a = document.createElement('a');
a.href = 'data:text/plain;charset=utf-8,' + encodeURIComponent(f.innerHTML);
a.download = 'Content.html';
a.innerText = "Download HTML";
var p = document.createElement('p');
p.appendChild(a);
f.parentNode.appendChild(p);
})();
javascript:(function(){
var f = document.querySelector(".modal.fade.in iframe");
var a = document.createElement('a');
a.href = f.getAttribute("src");
a.download = 'Course PDF.pdf';
a.innerText = "Download PDF";
var p = document.createElement('p');
p.appendChild(a);
f.parentNode.appendChild(p);
})();
javascript:(function(){
var f = document.querySelector(".modal.fade.in iframe");
var a = document.createElement('a');
a.href = 'data:text/plain;charset=utf-8,' + encodeURIComponent(f.contentDocument.documentElement.innerHTML);
a.download = 'Content.html';
a.innerText = "Download HTML";
var p = document.createElement('p');
p.appendChild(a);
f.parentNode.appendChild(p);
})();
javascript:(function(){
// select active modal's quiz key view
var e = document.querySelectorAll('.modal.fade.in .quiz-key')[0];
// set up object model to save to
var q = {};
q.titl = e.querySelector('h3').innerHTML;
q.contents = [];
var qi_c = 0;
var imagesList = [];
// set up individual answer object
var qa = {};
qa.answers = [];
var n = e.querySelectorAll('dt, dd');
for (var i = 0; i < n.length; i++) {
// loop through all questions and answers
if ( n[i].matches('dt') ) {
// match question stems
if (qa.question != null && qa.answers.length > 0) {
//move correct answer to front
qa.answers.sort(function(a,b){
return (a.correct === 'Correct') ? -1 : ( ( b.correct === 'Correct') ? 1 : 0);
});
q.contents.push(qa);
}
qa = {};
qa.answers = [];
qa.question = n[i].querySelector('p').innerHTML;
} else if ( n[i].matches('.quiz-answer') ) {
// match question answers
var an = {};
an.txt = n[i].querySelector('.answerTitle p').innerHTML;
an.image = get_src(n[i]);
an.correct = n[i].querySelector('.icn-check-green') !== null ? 'Correct' : 'Incorrect';
qa.answers.push(an);
an = {};
} else if ( n[i].matches('.quiz-question-meta') ) {
// match question feedback and remediation text
switch (n[i].querySelector('strong').innerHTML) {
case 'Feedback:':
qa.feedback = n[i].querySelector('p');
break;
case 'Remediation:':
qa.remediation = n[i].querySelector('p');
break;
}
} else {
// match question images
qa.question_image = get_src(n[i]);
}
}
q.contents.push(qa)
/*
// Copy and paste JSON text
var j = JSON.stringify(q);
window.prompt("Copy to clipboard: Ctrl+C, Enter", j);
*/
// add download section
var s = document.createElement('div');
s.setAttribute('id', 'CLDownloadLinks');
s.innerHTML='<h3>Downloads</h3>';
e.insertBefore(s, e.querySelector('dt'));
//download all images
for (var i = 0; i < imagesList.length; i++) {
download(imagesList[i].URI, imagesList[i].note + ".jpg")
}
//download JSON output
download(JSON_Link(q), 'Export.json');
function get_src(e) {
// images get saved to a specific list with an assigned number; the number gets inserted in the appropriate question or answer spot.
if (e.querySelector('img') !== null) {
var img = {};
img.note = String(imagesList.length+1);
img.URI = e.querySelector('img').getAttribute('src');
imagesList.push(img);
return img.note;
} else {
return 'No Image';
}
}
function JSON_Link(q) {
return 'data:text/plain;charset=utf-8,' + encodeURIComponent(JSON.stringify(q));
};
function download(url, filename) {
// Create download link
var a = document.createElement('a');
a.href=url;
a.download = filename;
a.innerText= filename;
var p = document.createElement('p');
p.appendChild(a);
document.getElementById('CLDownloadLinks').appendChild(p);
}
})();
javascript:(function(){var e=document.querySelectorAll('.modal.fade.in .quiz-key')[0];var q={};q.titl=e.querySelector('h3').innerHTML;q.contents=[];var qi_c=0;var imagesList=[];var qa={};qa.answers=[];var n=e.querySelectorAll('dt, dd');for(var i=0;i<n.length;i+=1){if(n[i].matches('dt')){if(qa.question!=null&&qa.answers.length>0){qa.answers.sort(function(a,b){return(a.correct==='Correct')?-1:((b.correct==='Correct')?1:0)});q.contents.push(qa)}qa={};qa.answers=[];qa.question=n[i].querySelector('p').innerHTML}else if(n[i].matches('.quiz-answer')){var an={};an.txt=n[i].querySelector('.answerTitle p').innerHTML;an.image=get_src(n[i]);an.correct=n[i].querySelector('.icn-check-green')!==null?'Correct':'Incorrect';qa.answers.push(an);an={}}else if(n[i].matches('.quiz-question-meta')){switch(n[i].querySelector('strong').innerHTML){case 'Feedback:':qa.feedback=n[i].querySelector('p');break;case 'Remediation:':qa.remediation=n[i].querySelector('p');break}}else{qa.question_image=get_src(n[i])}}q.contents.push(qa);var s=document.createElement('div');s.setAttribute('id','CLDownloadLinks');s.innerHTML='<h3>Downloads</h3>';e.insertBefore(s,e.querySelector('dt'));for(var i=0;i<imagesList.length;i+=1){download(imagesList[i].URI,imagesList[i].note+".jpg")}download(JSON_Link(q),'Export.json');function get_src(e){if(e.querySelector('img')!==null){var img={};img.note=String(imagesList.length+1);img.URI=e.querySelector('img').getAttribute('src');imagesList.push(img);return img.note}else{return 'No Image'}}function JSON_Link(q){return 'data:text/plain;charset=utf-8,'+encodeURIComponent(JSON.stringify(q))};function download(url,filename){var a=document.createElement('a');a.href=url;a.download=filename;a.innerText=filename;var p=document.createElement('p');p.appendChild(a);document.getElementById('CLDownloadLinks').appendChild(p)}})();
javascript:(function(){
var e = document.getElementsByClassName("modal fade in")[0];var j = mapDOM(e, true);window.prompt("Copy to clipboard: Ctrl+C, Enter", j);
function mapDOM(element, json) {
var treeObject = {};
// If string convert to document Node
if (typeof element === "string") {
if (window.DOMParser) {
parser = new DOMParser();
docNode = parser.parseFromString(element,"text/xml");
} else { // Microsoft strikes again
docNode = new ActiveXObject("Microsoft.XMLDOM");
docNode.async = false;
docNode.loadXML(element);
}
element = docNode.firstChild;
}
//Recursively loop through DOM elements and assign properties to object
function treeHTML(element, object) {
object["type"] = element.nodeName;
var nodeList = element.childNodes;
if (nodeList != null) {
if (nodeList.length) {
object["content"] = [];
for (var i = 0; i < nodeList.length; i++) {
if (nodeList[i].nodeType == 3) {
object["content"].push(nodeList[i].nodeValue);
} else {
object["content"].push({});
treeHTML(nodeList[i], object["content"][object["content"].length -1]);
}
}
}
}
if (element.attributes != null) {
if (element.attributes.length) {
object["attributes"] = {};
for (var i = 0; i < element.attributes.length; i++) {
object["attributes"][element.attributes[i].nodeName] = element.attributes[i].nodeValue;
}
}
}
}
treeHTML(element, treeObject);
return (json) ? JSON.stringify(treeObject) : treeObject;
}
})();
set theFile to choose file with prompt text "Select JSON export file."
set theJSON to text of (read theFile as «class utf8»)
tell application "JSON Helper"
set quizObject to read JSON from theJSON
end tell
set maxAnswers to 0
repeat with quizItem in |contents| of quizObject
if (number of items in answers of quizItem > maxAnswers) then
set maxAnswers to number of items in answers of quizItem
end if
end repeat
set recTitle to text ((offset of ":" in (titl of quizObject)) + 2) thru -1 of titl of quizObject
set theTestname to text returned of (display dialog "What is test name?" default answer (recTitle as text))
tell application "Microsoft Excel"
set oBook to active workbook
tell oBook
set oSheet to active sheet
set theSheet to make new worksheet at end with properties {name:("CL_" & theTestname)}
activate theSheet
tell active sheet
set value of range "A1" to titl of quizObject
set rowCounter to 3
tell row 2
set value of cell 1 to "Question"
set value of cell 2 to "Question Image"
set value of cell 3 to "Feedback"
set value of cell 4 to "Remediation"
set ansCounter to 0
repeat (maxAnswers) times
set AnswerNumber to (ansCounter) div 3
set value of cell (ansCounter + 5) to "Answer " & (AnswerNumber + 1)
set value of cell (ansCounter + 6) to "Answer " & (AnswerNumber + 1) & " Image"
set value of cell (ansCounter + 7) to "Answer " & (AnswerNumber + 1) & " Correct?"
set ansCounter to ansCounter + 3
end repeat
end tell
repeat with quizItem in |contents| of quizObject
tell row (rowCounter)
try
set value of cell 1 to question of quizItem
set value of cell 2 to question_image of quizItem
set value of cell 3 to feedback of quizItem
set value of cell 4 to remediation of quizItem
set ansCounter to 0
repeat with quizAnswer in answers of quizItem
set value of cell (ansCounter + 5) to txt of quizAnswer
set value of cell (ansCounter + 6) to image of quizAnswer
set value of cell (ansCounter + 7) to correct of quizAnswer
set ansCounter to ansCounter + 3
end repeat
set rowCounter to rowCounter + 1
on error number -1728
end try
end tell
end repeat
end tell
end tell
set theBook to make new workbook
activate theBook
set theFilename to choose file name with prompt "Save VAIRKKO Import file" default name (theTestname & ".xls")
tell theBook
tell active sheet
tell row 1
set value of cell 1 to "Question"
set value of cell 2 to "Correct_Answer"
set ansCounter to 1
repeat 5 times
set value of cell (ansCounter + 2) to "Wrong_Answer" & ansCounter
set ansCounter to ansCounter + 1
end repeat
end tell
set rowCounter to 2
repeat with quizItem in |contents| of quizObject
tell row (rowCounter)
try
set value of cell 1 to question of quizItem
set ansCounter to 0
repeat with quizAnswer in answers of quizItem
set value of cell (ansCounter + 2) to txt of quizAnswer
set ansCounter to ansCounter + 1
end repeat
set rowCounter to rowCounter + 1
on error number -1728
end try
end tell
end repeat
end tell
end tell
save workbook as theBook filename (POSIX path of theFilename) file format Excel98to2004 file format
close active workbook
activate oBook
activate object worksheet "Courses"
end tell
@samuelkordik
Copy link
Author

Simple bookmarklet to scrape test bank questions out of site format and copy in JSON in order to import into different vendor's site.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment