From 030169dd8c06d2964a543401088e8bf10738e8da Mon Sep 17 00:00:00 2001 From: Alex Tomala Date: Thu, 23 Jul 2015 20:35:24 -0400 Subject: [PATCH] Uses Regex to find links Regex needs work done still but it works better then looking at links Also added error handeling and simplified internal HTML code --- html/hoverwindow.html | 15 +---- manifest.json | 12 +++- scripts/background.js | 7 ++- scripts/script.js | 110 ++++++++++++++++++++--------------- styles/extension-styling.css | 4 ++ 5 files changed, 85 insertions(+), 63 deletions(-) diff --git a/html/hoverwindow.html b/html/hoverwindow.html index 944bfa5..fa80a49 100644 --- a/html/hoverwindow.html +++ b/html/hoverwindow.html @@ -1,15 +1,6 @@ - - - - - - - -
-

-
-

-
+ +

+

diff --git a/manifest.json b/manifest.json index 0875a10..7896f86 100644 --- a/manifest.json +++ b/manifest.json @@ -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 @@ -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/" + ] -} \ No newline at end of file +} diff --git a/scripts/background.js b/scripts/background.js index c03896d..eb30ddd 100644 --- a/scripts/background.js +++ b/scripts/background.js @@ -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(); } }); diff --git a/scripts/script.js b/scripts/script.js index abee567..8cd8f3f 100644 --- a/scripts/script.js +++ b/scripts/script.js @@ -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('
'); $('#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, "$&")); + + $('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); @@ -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)+'%'); -} \ No newline at end of file + $('#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'); +} diff --git a/styles/extension-styling.css b/styles/extension-styling.css index 836fb3f..0aa9262 100644 --- a/styles/extension-styling.css +++ b/styles/extension-styling.css @@ -69,6 +69,10 @@ background-image: linear-gradient(to bottom, #62c462,#57a957); } +.flow-link { + color: #C40000; +} + #overall{ width:100%; text-align:center;