Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Scrapes IVLE for module CourseIDs
// ==UserScript==
// @name Scrapes IVLE Course ID
// @namespace yijiang
// @version 1
// @match https://ivle.nus.edu.sg/v1/lms/list_course.aspx?src=bm&code=&title=&lecName=&acadyear=2016%2f2017&semester=Semester+1&ModTitleExact=N&LecNameExact=N
// @grant none
// ==/UserScript==
function switchPage(page){
$('#GV_Page2').val(page).change();
}
function getCourseId(url){
let query = url.split('?')[1];
let queryParts = query.split('&');
for (let i = 0; i < queryParts.length; i++) {
let [key, value] = queryParts[i].split('=');
if (key === 'CourseID') return value;
}
}
function scrapePage(){
let timeBetweenPage = 10 * 1000; // 5 seconds
let codes = JSON.parse(localStorage.getItem('course-code-id') || '{}');
let lastScrapedPage = +localStorage.getItem('last-scraped') || 0;
let currentPage = +$('#GV_Page2').val();
if (currentPage != lastScrapedPage+1)
switchPage(lastScrapedPage+1);
$('a[href^="/V1/Module/Student/default.aspx?CourseID="]').each((i, e) => {
let courseId = getCourseId(e.href);
if (courseId) {
codes[e.textContent.trim()] = courseId;
} else {
console.error(e.textContent.trim() + ' has no course ID');
}
});
localStorage.setItem('course-code-id', JSON.stringify(codes));
localStorage.setItem('last-scraped', currentPage);
setTimeout(() => switchPage(lastScrapedPage+1), timeBetweenPage);
}
setTimeout(scrapePage, 10 * 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.