Skip to content

Instantly share code, notes, and snippets.

@iamsortiz
Created November 11, 2017 11:25
Show Gist options
  • Select an option

  • Save iamsortiz/66a7113fdde2207b78a524a2b720d3c3 to your computer and use it in GitHub Desktop.

Select an option

Save iamsortiz/66a7113fdde2207b78a524a2b720d3c3 to your computer and use it in GitHub Desktop.
Download an edX course as Markdown a file (edX - Course - Web browser console scraping)
// Download an edX course as Markdown a file
//
// Usage:
// * Go to edX course webpage
// * Enroll
// * Get in (audit course, for example)
// * Open web browser console (tested on Chromium)
// * Paste code
// * Press intro
function extracttion() {
extracted = {}
// Extract URL
extracted.url = window.location.href
// Extract title
extracted.title = jQuery('.page-title').text()
// Extract content
extracted.content = []
jQuery('.block-tree').find('div').each(function () {
text = jQuery(this).text()
text = text.replace(/(\r\n|\n|\r)/gm," ")
text = text.replace(/\s+/g,' ')
text = text.trim()
if (jQuery(this).hasClass('section-name')) text = '## ' + text
if (text) extracted.content.push(text)
})
return extracted
}
function presentation(data) {
return `# ${data.title}
url: ${data.url}
${data.content.join('\n')}`
}
function download(name, contents, mime_type) {
mime_type = mime_type || "text/plain";
var blob = new Blob([contents], {type: mime_type});
var dlink = document.createElement('a');
dlink.download = name;
dlink.href = window.URL.createObjectURL(blob);
dlink.onclick = function(e) {
// revokeObjectURL needs a delay to work properly
var that = this;
setTimeout(function() {
window.URL.revokeObjectURL(that.href);
}, 1500);
};
dlink.click();
dlink.remove();
}
function main() {
data = extracttion()
md = presentation(data)
download(data.title + '.md', md)
}
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment