Skip to content

Instantly share code, notes, and snippets.

@Synvox
Created January 21, 2016 05:20
Show Gist options
  • Save Synvox/59132c27c3684bc9cf7e to your computer and use it in GitHub Desktop.
Save Synvox/59132c27c3684bc9cf7e to your computer and use it in GitHub Desktop.
An I-Learn webscraper
var scriptElement = document.body.appendChild(document.createElement('script'))
scriptElement.type='text/javascript'
scriptElement.src='jquery-2.2.0.js'
var errors = []
var items = []
function Item(that) {
for(var i in that)
this[i] = that[i]
// no prototype for now
}
var ilearn2 = {
host: 'https://byui.brainhoney.com',
url: '/Frame/Component/Home',
fail: function(){
errors.push({
title: 'Log in to I-Learn 2',
href: ilearn2.host + ilearn2.url
})
},
getUserId: function(cb){
$.get(ilearn2.host + ilearn2.url,function(html){
html = html.split('\n')
var triggerword = "userId: '"
html.forEach(function(line){
line = line.trim()
if (line.startsWith(triggerword)) {
line = line.substring(triggerword.length);
line = line.substring(0, line.indexOf("'"))
cb(line)
}
})
}).fail(function(){
ilearn2.fail();
cb()
})
},
crawl: function(callback){
ilearn2.getUserId(function(userid){
console.log(userid)
if (!userid) { // there's probably a better way to do this.
callback();
return;
}
var url = ilearn2.host + '/Admin/Admin.ashx?cmd=getusergradebook2&_format=json&userid='+userid+'&itemid=**'
console.log(url)
$.getJSON(url,function(data){
var classes = null
try {
classes = data.response.enrollments.enrollment
} catch(e) {
ilearn2.fail();
callback()
}
console.log(data);
ilearn2.readClasses(classes,callback)
callback()
})
})
},
readClasses: function(classes){
classes.forEach(function(course){
ilearn2.readClass(course)
})
},
readClass: function(course){
var day = 86400000
var week = 604800000
var hour = 3600000
var courseTitle = course.entity.title
var now = Date.now()
var courseId = course.id
course.grades.items.item.forEach(function(grade){
var date = Date.parse(grade.duedate)
date += hour * (new Date()).getTimezoneOffset()/60 // Reset timezone
if (date && now + week * 2 > date && now < date){
items.push(new Item({
date: new Date(date),
course: courseTitle,
title: grade.title,
href: ilearn2.host + '/Frame/Component/CoursePlayer?enrollmentid=' + courseId + '&itemid=' + grade.itemid
}))
}
})
}
}
var ilearn3 = {
host: 'https://byui.brightspace.com',
url: '/d2l/home',
crawl: function(callback){
$.get(ilearn3.host + ilearn3.url,function(data){
data = data.replace(/<iframe.*?\/iframe>/i,'')
if ($(data).find('.d2l-datalist-container').length === 0) {
errors.push({
title: 'Log in to I-Learn 3',
href: ilearn3.host + ilearn3.url
})
callback()
}
$(data).find('.d2l-datalist-container:first .d2l-datalist-item')
.each(function(index,ele){
$ele = $(ele)
var dateobj ={
month: $ele.find('.d2l-textblock.d2l-textblock-strong').eq(0).text(),
date: $ele.find('.d2l-textblock.d2l-textblock-strong').eq(1).text(),
time: $ele.find('.d2l-textblock.d2l-textblock-strong').eq(2).text(),
}
var date = new Date(Date.parse(dateobj.month + ' ' + dateobj.date + ' ' + ((new Date()).getUTCFullYear()) + ' ' + dateobj.time))
var title = $ele.find('.d2l-offscreen').text()
items.push(new Item({
date: date,
course: $ele.find('.d2l-textblock.d2l-textblock-secondary').text(),
title: title.split(' - ')[1],
href: ilearn3.host + $ele.find('a').attr('href')
}))
})
callback()
})
}
}
var crawl = function(callback){
errors.length = 0
items.length = 0
ilearn2.crawl(function(){
ilearn3.crawl(function(){
callback()
})
})
}
var deposit = function(){
var numUrgent = 0
var day = 86400000
for(var i in localStorage)
delete localStorage[i]
items.forEach(function(item){
if (item.date - Date.now() < day * 2)
numUrgent++
localStorage[item.href] = JSON.stringify(item)
})
chrome.browserAction.setBadgeText({text: ''+numUrgent})
localStorage.meta = JSON.stringify({
updatedOn: new Date(),
errors: errors
})
}
var port = null;
var actions = {
refresh: function(){
crawl(function(){
deposit()
port.postMessage('refresh')
})
}
}
chrome.extension.onConnect.addListener(function(newport) {
port = newport
port.onMessage.addListener(function(msg) {
actions[msg]()
})
})
scriptElement.onload = function(){
crawl(function(){
deposit()
})
chrome.alarms.create('iknow-alarm',{
periodInMinutes: 20
})
chrome.alarms.onAlarm.addListener(function(){
crawl(function(){
deposit()
port.postMessage('refresh')
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment