Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 3 additions & 12 deletions html/hoverwindow.html
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
<table>
<tr>
<td>
<h1 id="code"></h1>
</td>
</tr>
<tr>
<td>
<h3 id="course-name"></h3>
</td>
</tr>
</table>

<h1 id="code"></h1>
<h3 id="course-name"></h3>
<p id="description"></p>

<table>
Expand Down
12 changes: 9 additions & 3 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
"web_accessible_resources": ["res/loading.gif", "html/hoverwindow.html"],
"content_scripts": [
{
"matches": ["http://ugradcalendar.uwaterloo.ca/*","https://ugradcalendar.uwaterloo.ca/*"],
"matches": ["http://ugradcalendar.uwaterloo.ca/*",
"https://ugradcalendar.uwaterloo.ca/*",
"http://www.ucalendar.uwaterloo.ca/*",
"https://www.ucalendar.uwaterloo.ca/*"],
"css": ["styles/extension-styling.css"],
"js": ["scripts/jquery-1.11.3.js","scripts/script.js","scripts/background.js"],
"all_frames": true
Expand All @@ -32,6 +35,9 @@
"http://uwflow.com/",
"https://uwflow.com/",
"http://ugradcalendar.uwaterloo.ca/",
"https://ugradcalendar.uwaterloo.ca/"
"https://ugradcalendar.uwaterloo.ca/",
"http://www.ucalendar.uwaterloo.ca/",
"https://www.ucalendar.uwaterloo.ca/"

]
}
}
7 changes: 5 additions & 2 deletions scripts/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
chrome.extension.onMessage.addListener(function(message) {
if (message.action == 'createContextMenuItem') {
chrome.contextMenus.removeAll();
chrome.contextMenus.create({title:'Go to Flow', contexts:['link'], targetUrlPatterns:['*://ugradcalendar.uwaterloo.ca/courses/*', '*://ugradcalendar.uwaterloo.ca/courses*Number=*'], onclick:function(info) {
chrome.contextMenus.create({title:'Go to Flow', contexts:['page'], onclick:function(info) {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {action: 'openPage', url:info.linkUrl});
chrome.tabs.sendMessage(tabs[0].id, {action: 'openPage'});
});
}});
}
else if (message.action == 'deleteContextMenuItem') {
chrome.contextMenus.removeAll();
}
});
110 changes: 64 additions & 46 deletions scripts/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,71 +5,79 @@
*/

var courseObjs = []; //keeps track of courses to avoid repeated ajax calls
var courseObj = {}; // Current AJAX request
var lastClicked = {};

$(document).ready(function(){
chrome.extension.sendMessage({'action':'createContextMenuItem'}); // Sends message to background.js which creates right click menu option for courses
$('body').after('<div id=\"frame\" class=\"loading\"></div>');
$('#frame').load(chrome.extension.getURL("html/hoverwindow.html")); // Appends hover frame to bottom of document
var courseObj;
$('a').hover(function(event) { // Applies hover action to all valid courses
if(!isValidCourseURL(this.href))
return;
$('#frame').css('display','block');
$('#frame').removeClass('info');
$('#frame').addClass('loading');
$(this).mousemove(function(event) { // Allows hover frame to follow cursor
$('#frame').css('left',event.pageX+'px');
$('#frame').css('top',event.pageY+'px');
});
var code = getCourseCode($(this).attr('href'));
if ($.grep(courseObjs,function(e) {return e.code.replace(' ','').toLowerCase() == code}).length == 0) { // Searches if information on the course has been fetched and stored already
var url = $(this).attr('href');
courseObj = $.ajax({jsonp: false, dataType: 'json', url: getAPIURL(url)})
courseObj.done(function(flowInfo) {
courseObjs.push(flowInfo);
loadContent(code);
});

$('body').html($('body').html().replace(/\b[A-Z]{2,}\s*[0-9]{1,3}L?\b(?!.[0-9])(?=[^>]*(<|$))/g, "<span class=\"flow-link\">$&</span>"));

$('body .flow-link').hover(mouseOver, mouseOut); // Applies hover action to all courses

document.addEventListener("mousedown", function(event){
if ($(event.target).hasClass('flow-link')) {
lastClicked = $(event.target);
chrome.extension.sendMessage({'action':'createContextMenuItem'});
} else {
loadContent(code);
chrome.extension.sendMessage({'action':'deleteContextMenuItem'});
}
}, true);

}, function() {
$('#frame').css('display','none');
if ($.active > 0)
courseObj.abort(); // Terminates any ongoing AJAX request
});
});

function mouseOver(){
$('#frame').css('display','block');
$('#frame').removeClass('info');
$('#frame').addClass('loading');

$(this).mousemove(function(event) { // Allows hover frame to follow cursor
$('#frame').css('left',event.pageX+'px');
$('#frame').css('top',event.pageY+'px');
});

var courseCode = $(this).text().replace(' ','').toLowerCase();

if ($.grep(courseObjs,function(e) {return e.id == courseCode}).length == 0) { // Searches if information on the course has been fetched and stored already
courseObj = $.ajax({jsonp: false, dataType: 'json', url: getAPIURL(courseCode)})
courseObj.done(function(flowInfo) {
courseObjs.push(flowInfo);
loadContent(courseCode);
});

courseObj.error(errorContent);
} else {
loadContent(courseCode);
}
}

function mouseOut(){
$('#frame').css('display','none');
$('#frame > table').css('display', 'none');
if ($.active > 0)
courseObj.abort(); // Terminates any ongoing AJAX request
}

chrome.extension.onMessage.addListener(function(message) { // Receives reply message from background page in order to create contextMenu
if (message.action=='openPage'){
window.open(getUWFlowURL(message.url));
var courseCode = lastClicked.text().replace(' ','').toLowerCase();
window.open(getUWFlowURL(courseCode));
}
});
function isValidCourseURL(url) { // Verifies whether a URL directs to a valid course
return url.includes('ugradcalendar.uwaterloo.ca/courses/') || (url.includes('ugradcalendar.uwaterloo.ca/courses') && url.includes('Code') && url.includes('Number'));
}

function getCourseCode(url){ // Returns the course code indicated by a URL
if (url.indexOf('aspx') > 0) {
var beginOfCode = url.indexOf('Code=')+5;
var endOfCode = url.indexOf('&',beginOfCode);
var beginOfNumber = url.indexOf('Number=')+7;
var endOfNumber = url.indexOf('&',beginOfNumber);
return ((endOfCode > 0 ? url.substring(beginOfCode,endOfCode) : url.substring(beginOfCode)) + (endOfNumber > 0 ? url.substring(beginOfNumber,endOfNumber) : url.substring(beginOfNumber))).toLowerCase(); //determines code & number regardless of position in URL
} else {
return url.substring(url.indexOf('/courses/')+9).replace('/','').toLowerCase();
}
function getAPIURL(courseCode) { // Returns URL to the course info through the UWFlow API
return 'https://uwflow.com/api/v1/courses/' + courseCode;
}

function getAPIURL(url) { // Returns URL to the course info through the UWFlow API
return 'https://uwflow.com/api/v1/courses/' + getCourseCode(url);
}
function getUWFlowURL(url){ // Returns URL to the course info through regular UWFlow
return 'https://uwflow.com/course/' + getCourseCode(url);
function getUWFlowURL(courseCode){ // Returns URL to the course info through regular UWFlow
return 'https://uwflow.com/course/' + courseCode;
}

function loadContent(code){ // Loads information into hovering frame
var data = $.grep(courseObjs,function(e) {return e.code.replace(' ','').toLowerCase() == code})[0];
function loadContent(courseCode){ // Loads information into hovering frame
var data = $.grep(courseObjs,function(e) {return e.id == courseCode})[0];
$('#frame').removeClass('loading');
$('#frame').addClass('info');
$('#code').html(data.code);
Expand All @@ -80,4 +88,14 @@ function loadContent(code){ // Loads information into hovering frame
$('#easy-bar').css('width',(data.ratings[1].rating*100)+'%');
$('#easy').html(Math.round(data.ratings[1].rating*100)+'%');
$('#overall').html(Math.round(data.overall.rating*100)+'%');
}
$('#frame > table').css('display', 'block');
}

function errorContent(error){
$('#frame').removeClass('loading');
$('#frame').addClass('info');
$('#code').html("Error");
$('#course-name').html("Data can't be loaded");
$('#description').html(error);
$('#frame > table').css('display', 'none');
}
4 changes: 4 additions & 0 deletions styles/extension-styling.css
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@
background-image: linear-gradient(to bottom, #62c462,#57a957);
}

.flow-link {
color: #C40000;
}

#overall{
width:100%;
text-align:center;
Expand Down