Last active
October 10, 2016 23:53
-
-
Save notyal/7d54b74e537183fa9a12ba27ff3cb2d4 to your computer and use it in GitHub Desktop.
Improve INTST 235 class calendar
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 INTST 235 Calendar Improved | |
// @namespace https://gist.github.com/notyal/7d54b74e537183fa9a12ba27ff3cb2d4 | |
// @version 1.0 | |
// @description Scroll to next assignment based on current date. | |
// @author Layton Nelson | |
// @match https://dl.dropboxusercontent.com/s/q39fy232p8li9na/calendar_235_f16.html?dl=0 | |
// @grant none | |
// @downloadURL https://gist.github.com/notyal/7d54b74e537183fa9a12ba27ff3cb2d4/raw/intst235-calendar-improved.user.js | |
// @updateURL https://gist.github.com/notyal/7d54b74e537183fa9a12ba27ff3cb2d4/raw/intst235-calendar-improved.user.js | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
// allow adding days to date function | |
// from: http://stackoverflow.com/a/563442 | |
Date.prototype.addDays=function(d){return new Date(this.valueOf()+864E5*d);}; | |
// get current date | |
// exploit week days, such that only valid class days will be used | |
var days = ["M","M","W","W","F","F","M"]; | |
var months = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sept","Oct","Nov","Dec"]; | |
var d = new Date(); | |
var classDay = days[d.getDay()]; | |
var classMonth = months[d.getMonth()]; | |
var classDate = d.getDate(); | |
console.log("Pre-adjusted date is: '" + classDay + " " + classMonth + " " + classDate + "'."); | |
// add 1 day to classDate if class day is not (1,3,5) | |
// also compute new classMonth | |
switch (d.getDay()) { | |
case 0: case 2: case 4: case 6: | |
var d1 = d.addDays(1); | |
// classDay doesn't need to be computed, since it has already been hacked | |
classMonth = months[d1.getMonth()]; | |
classDate = d1.getDate(); | |
break; | |
} | |
// search for next class date | |
var calString = classDay + " " + classMonth + " " + classDate + ":"; | |
// modified from: http://stackoverflow.com/a/3813334 | |
var tags = document.getElementsByTagName("h2"); | |
var found; | |
console.log("Looking for date: '"+calString+"' ..."); | |
for (var i = 0; i < tags.length; i++) { | |
if (tags[i].textContent == calString) { | |
found = tags[i]; | |
break; | |
} | |
} | |
// scroll and highlight element if it was found | |
if (found === undefined){ | |
console.log("Error: could not find date."); | |
} else { | |
// scroll to found date | |
console.log("Scrolling to date."); | |
found.scrollIntoView(true); // positions element to top of view | |
// set background color to a light yellow | |
found.style.backgroundColor = '#ffc'; | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment