Created
September 27, 2016 15:38
-
-
Save ZhangYiJiang/7785d7e74fc6c8cf1b90ba6a56de3231 to your computer and use it in GitHub Desktop.
Scrapes IVLE for module CourseIDs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==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