From 893e88237715d43cc93ce2c4e7d06f9486ad0087 Mon Sep 17 00:00:00 2001 From: Sriram Hariharan Date: Sun, 11 Aug 2019 10:09:59 -0500 Subject: [PATCH] cleaned up code --- calendar.html | 2 +- js/background.js | 11 +--- js/calendar.js | 25 ++++---- js/config.js | 13 +++- js/courseCatalog.js | 150 ++++++++++++++++++++++---------------------- js/import.js | 7 +-- js/util.js | 31 +++++---- js/utplanner.js | 24 ------- manifest.json | 6 +- 9 files changed, 127 insertions(+), 142 deletions(-) diff --git a/calendar.html b/calendar.html index 99c9ab02..6e3cfbd4 100644 --- a/calendar.html +++ b/calendar.html @@ -34,9 +34,9 @@ + - \ No newline at end of file diff --git a/js/background.js b/js/background.js index c0440845..a0da10b0 100644 --- a/js/background.js +++ b/js/background.js @@ -308,21 +308,17 @@ function updateStatus(sendResponse) { var object = $('
').html(result).contents(); let newstatus = object.find('[data-th="Status"]').text(); let registerlink = object.find('td[data-th="Add"] a'); - if (registerlink) { + if (registerlink) registerlink = registerlink.attr('href'); - } var haschanged = (newstatus == oldstatus && registerlink == oldlink); - if (!haschanged) { + if (!haschanged) console.log(c.unique + ' updated from ' + oldstatus + " to " + newstatus + " and " + oldlink + " to " + registerlink); - } nochange &= haschanged; c.registerlink = registerlink; c.status = newstatus; } } }); - - } catch (e) { console.log(e); console.log('Not logged into UT Coursebook. Could not update class statuses.'); @@ -333,9 +329,6 @@ function updateStatus(sendResponse) { savedCourses: courses }); console.log('updated status'); - } else { - // console.log('no change'); } - // console.log("updated status' and registerlinks"); }); } \ No newline at end of file diff --git a/js/calendar.js b/js/calendar.js index a80f3af5..801baabe 100644 --- a/js/calendar.js +++ b/js/calendar.js @@ -11,8 +11,6 @@ var curr_course = {} $("#calendar").after(Template.calendarModal()); - - chrome.storage.sync.get("savedCourses", function (data) { // Iterate through each saved course and add to 'event' saved_courses = data.savedCourses; @@ -75,14 +73,13 @@ function setUpModal() { function setRegisterButton(status, registerlink) { if (canNotRegister(status, registerlink)) - $("#register").text("Can't Register").css("background-color", "#FF5722"); + $("#register").text("Can't Register").css("background-color", Colors.closed); else if (status.includes("waitlisted")) - $("#register").text("Join Waitlist").css("background-color", "#FF9800"); + $("#register").text("Join Waitlist").css("background-color", Colors.waitlisted); else - $("#register").text("Register").css("background-color", "#4CAF50"); + $("#register").text("Register").css("background-color", Colors.open); } - function canNotRegister(status, registerlink) { return status.includes("closed") || status.includes("cancelled") || !status || !registerlink } @@ -100,15 +97,15 @@ function buildTimeTitle(datetimearr) { // Iterate through each saved course and add to 'event' -function buildEventSource(savedCourses) { +function buildEventSource(saved_courses) { color_counter = 0; let event_source = []; var hours = 0; - for (let i = 0; i < savedCourses.length; i++) { + for (let i = 0; i < saved_courses.length; i++) { let { coursename, datetimearr - } = savedCourses[i]; + } = saved_courses[i]; let number = seperateCourseNameParts(coursename).number; hours += parseInt(number.charAt(0)); for (let j = 0; j < datetimearr.length; j++) { @@ -118,11 +115,15 @@ function buildEventSource(savedCourses) { } color_counter++; } - $("#hours").text(hours + " Hours"); - $("#num").text(savedCourses.length + " Courses"); + displayMetaData(hours, saved_courses); return event_source; } +function displayMetaData(hours, saved_courses) { + $("#hours").text(hours + " Hours"); + $("#num").text(saved_courses.length + " Courses"); +} + //create the event object for every section function setEventForSection(session, colorCounter, i) { let full_day = days.get(session[0]); @@ -178,8 +179,6 @@ chrome.runtime.onMessage.addListener( ); - - $("#info").click(() => { setTimeout(() => { window.open(curr_course.link); diff --git a/js/config.js b/js/config.js index b792fba0..115c5305 100644 --- a/js/config.js +++ b/js/config.js @@ -11,8 +11,14 @@ class Colors { '#FF5252', '#E91E63', '#009688', '#00BCD4', '#4E342E', '#424242', '#9E9E9E' ]; -} + static open = "#4CAF50"; + static waitlisted = "#FF9800"; + static closed = "#FF5722"; + static highlight_conflict = "#F44336"; + static highlight_default = "#333333"; + static highlight_saved = "#4CAF50"; +} class Export { static png_options = { @@ -21,4 +27,9 @@ class Export { removeContainer: true, async: true, } +} + + +class Text { + } \ No newline at end of file diff --git a/js/courseCatalog.js b/js/courseCatalog.js index bfccfff0..8b282d0f 100644 --- a/js/courseCatalog.js +++ b/js/courseCatalog.js @@ -1,8 +1,10 @@ console.log(`UT Registration Plus is running on this page: ${window.location.href}`); var curr_course = {} -var semesterCode = new URL(window.location.href).pathname.split('/')[4]; -var doneLoading = true; + + +var semester_code = new URL(window.location.href).pathname.split('/')[4]; +var done_loading = true; updateListConflictHighlighting(); @@ -64,45 +66,6 @@ $("body").on('click', '#distButton', function () { getDistribution(curr_course); }); -$("#myModal").on('click', '#saveCourse', function () { - setTimeout(function () { - saveCourse(); - }, 0); -}); - -$("#Syllabi").click(function () { - setTimeout(function () { - window.open(curr_course["links"]["syllabi"]); - }, Timing.button_delay); -}); -$("#rateMyProf").click(function () { - setTimeout(function () { - window.open(curr_course["links"]["rate_my_prof"]); - }, Timing.button_delay); -}); -$("#eCIS").click(function () { - setTimeout(function () { - window.open(curr_course["links"]["ecis"]); - }, Timing.button_delay); -}); -$("#textbook").click(function () { - setTimeout(function () { - window.open(curr_course["links"]["textbook"]); - }, Timing.button_delay); -}); -$("#semesters").on('change', function () { - let sem = $(this).val(); - sem = sem == "Aggregate" ? undefined : sem; - getDistribution(curr_course, sem); -}); - -$("#retry").click(function () { - $("#retrylabel").hide(); - $(this).hide(); - loadNextPages(); -}); - - function updateLinks(course_info, first_name) { let { @@ -124,7 +87,7 @@ function buildCourseLinks(course_info) { prof_name } = course_info links = { - "textbook": `https://www.universitycoop.com/adoption-search-results?sn=${semesterCode}__${department}__${number}__${unique}`, + "textbook": `https://www.universitycoop.com/adoption-search-results?sn=${semester_code}__${department}__${number}__${unique}`, "syllabi": `https://utdirect.utexas.edu/apps/student/coursedocs/nlogon/?semester=&department=${department}&course_number=${number}&course_title=&unique=&instructor_first=&instructor_last=${prof_name}&course_type=In+Residence&search=Search`, //default ones (before first name can be used) "rate_my_prof": "http://www.ratemyprofessors.com/campusRatings.jsp?sid=1255", @@ -221,18 +184,18 @@ function updateListConflictHighlighting(start = 0) { $('table').find('tr').each(function (i) { if (i >= start) { if (!($(this).find('td').hasClass("course_header")) && $(this).has('th').length == 0) { - var thisForm = this; - var uniquenum = $(this).find('td[data-th="Unique"]').text(); + var this_form = this; + var unique = $(this).find('td[data-th="Unique"]').text(); chrome.runtime.sendMessage({ command: "isSingleConflict", dtarr: getDayTimeArray(this), - unique: uniquenum + unique: unique }, function (response) { let { isConflict, alreadyContains } = response - updateTextHighlighting($(thisForm).find('td'), canHighlight, isConflict, alreadyContains); + updateTextHighlighting($(this_form).find('td'), canHighlight, isConflict, alreadyContains); }); } } @@ -241,23 +204,24 @@ function updateListConflictHighlighting(start = 0) { } function updateTextHighlighting(tds, canHighlight, isConflict, alreadyContains) { + let current_color = rgb2hex(tds.css('color')); if (isConflict && canHighlight && !alreadyContains) { - if (tds.css('color') != 'rgb(244, 67, 54)') - tds.css('color', '#F44336').css('text-decoration', 'line-through').css('font-weight', 'normal'); + if (current_color != Colors.highlight_conflict) + tds.css('color', Colors.highlight_conflict).css('text-decoration', 'line-through').css('font-weight', 'normal'); } else if (!alreadyContains) { - if (tds.css('color') != 'rgb(51, 51, 51)') - tds.css('color', 'black').css('text-decoration', 'none').css('font-weight', 'normal'); + if (tds.css('color') != Colors.highlight_default) + tds.css('color', Colors.highlight_default).css('text-decoration', 'none').css('font-weight', 'normal'); } if (alreadyContains) { - if (tds.css('color') != 'rgb(76, 175, 80)') - tds.css('color', '#4CAF50').css('text-decoration', 'none').css('font-weight', 'bold'); + if (tds.css('color') != Colors.highlight_saved) + tds.css('color', Colors.highlight_saved).css('text-decoration', 'none').css('font-weight', 'bold'); } } /* For a row, get the date-time-array for checking conflicts*/ function getDayTimeArray(row, course_info) { - var daytimearray = [] + var day_time_array = [] let days = course_info ? course_info["time_data"]["days"] : $(row).find('td[data-th="Days"]>span').toArray().map(x => $(x).text().trim()); let times = course_info ? course_info["time_data"]["times"] : $(row).find('td[data-th="Hour"]>span').toArray().map(x => $(x).text().trim()); let places = course_info ? course_info["time_data"]["places"] : $(row).find('td[data-th="Room"]>span').toArray().map(x => $(x).text().trim()); @@ -268,20 +232,20 @@ function getDayTimeArray(row, course_info) { for (var j = 0; j < date.length; j++) { let letter = date.charAt(j); if (letter == "T" && j < date.length - 1 && date.charAt(j + 1) == "H") { - daytimearray.push(["TH", convertTime(time), place]); + day_time_array.push(["TH", convertTime(time), place]); } else { if (letter != "H") - daytimearray.push([letter, convertTime(time), place]); + day_time_array.push([letter, convertTime(time), place]); } } } - return daytimearray; + return day_time_array; } function convertDateTimeArrToLine(date, time, place) { - var arr = seperateDays(date) - var output = prettifyDaysText(arr) - var building = place.substring(0, place.search(/\d/) - 1); + let arr = seperateDays(date) + let output = prettifyDaysText(arr) + let building = place.substring(0, place.search(/\d/) - 1); building = building == "" ? "Undecided Location" : building; return `${output} at ${time.replace(/\./g, '').replace(/\-/g, ' to ')} in ${building}`; } @@ -319,9 +283,8 @@ function buildTimeTitle(course_info) { times, places } = course_info["time_data"] - datetimearr = []; var lines = []; - for (var i = 0; i < days.length; i++) { + for (let i = 0; i < days.length; i++) { var date = days[i]; var time = times[i]; var place = places[i]; @@ -382,7 +345,6 @@ function openDialog(course_info, res) { //close button allowClosing(); setChart(data); - } function setChart(data) { @@ -468,26 +430,26 @@ function loadNextPages() { chrome.storage.sync.get('loadAll', function (data) { if (data.loadAll) { let link = next.prop('href'); - if (doneLoading && next && link) { + if (done_loading && next && link) { toggleLoadingPage(true); $.get(link, function (response) { if (response) { - var nextpage = $('
').html(response).contents(); + var next_page = $('
').html(response).contents(); var current = $('tbody'); - var oldlength = $('tbody tr').length; + var old_length = $('tbody tr').length; var last = current.find('.course_header>h2:last').text(); - next = nextpage.find("#next_nav_link"); + next = next_page.find("#next_nav_link"); toggleLoadingPage(false); - var newrows = []; - nextpage.find('tbody>tr').each(function () { - let hasCourseHead = $(this).find('td').hasClass("course_header"); - if (!(hasCourseHead && $(this).has('th').length == 0)) + var new_rows = []; + next_page.find('tbody>tr').each(function () { + let has_course_header = $(this).find('td').hasClass("course_header"); + if (!(has_course_header && $(this).has('th').length == 0)) $(this).append(Template.extensionButton()); - if (!(hasCourseHead && last == $(this).find('td').text())) - newrows.push($(this)); + if (!(has_course_header && last == $(this).find('td').text())) + new_rows.push($(this)); }); - current.append(newrows); - updateListConflictHighlighting(oldlength + 1) + current.append(new_rows); + updateListConflictHighlighting(old_length + 1) } }).fail(function () { toggleLoadingPage(false); @@ -499,7 +461,43 @@ function loadNextPages() { }); } +$("#myModal").on('click', '#saveCourse', function () { + setTimeout(function () { + saveCourse(); + }, 0); +}); +$("#Syllabi").click(function () { + setTimeout(function () { + window.open(curr_course["links"]["syllabi"]); + }, Timing.button_delay); +}); +$("#rateMyProf").click(function () { + setTimeout(function () { + window.open(curr_course["links"]["rate_my_prof"]); + }, Timing.button_delay); +}); +$("#eCIS").click(function () { + setTimeout(function () { + window.open(curr_course["links"]["ecis"]); + }, Timing.button_delay); +}); +$("#textbook").click(function () { + setTimeout(function () { + window.open(curr_course["links"]["textbook"]); + }, Timing.button_delay); +}); +$("#semesters").on('change', function () { + let sem = $(this).val(); + sem = sem == "Aggregate" ? undefined : sem; + getDistribution(curr_course, sem); +}); + +$("#retry").click(function () { + $("#retrylabel").hide(); + $(this).hide(); + loadNextPages(); +}); /*Listen for update mssage coming from popup*/ chrome.runtime.onMessage.addListener( @@ -513,11 +511,11 @@ chrome.runtime.onMessage.addListener( function toggleLoadingPage(loading) { if (loading) { - doneLoading = false; + done_loading = false; $('#loader').css('display', 'inline-block'); $("#nextlabel").css('display', 'inline-block'); } else { - doneLoading = true; + done_loading = true; $('#loader').hide(); $("#nextlabel").hide(); } diff --git a/js/import.js b/js/import.js index fe61fc2d..ee976313 100644 --- a/js/import.js +++ b/js/import.js @@ -6,24 +6,23 @@ $(function () { console.log(window.location.href); var importbutton = "
"; waitlist = !(window.location.href.includes('https://utdirect.utexas.edu/registration/classlist.WBX')); - if(waitlist){ + if (waitlist) { sem = $('[name="s_ccyys"]').val(); $("[href='#top']").before(importbutton); } else { sem = $("option[selected='selected']").val(); $("table").after(importbutton); } - console.log(sem); $("#import").prepend("
defaultmessage..
"); $("#import").click(function () { - if(waitlist){ + if (waitlist) { $(".tbg").last().find(".tbon>td:first-child").each(function () { let unique = $(this).text().replace(/\s/g, ''); link = `https://utdirect.utexas.edu/apps/registrar/course_schedule/${sem}/${unique}/`; getInfo(); }); } else { - $("tr>td:first-child").each(function(){ + $("tr>td:first-child").each(function () { let unique = $(this).text().replace(/\s/g, ''); link = `https://utdirect.utexas.edu/apps/registrar/course_schedule/${sem}/${unique}/`; getInfo(); diff --git a/js/util.js b/js/util.js index 66af3a48..f60745a7 100644 --- a/js/util.js +++ b/js/util.js @@ -6,26 +6,20 @@ const days = new Map([ ["F", "Friday"] ]); -const semOrder = { - "Spring": 0, - "Fall": 1, - "Summer": 2, - "Winter": 3 -} - - function getStatusColor(status) { let color = "black"; if (status.includes("open")) { - color = "#4CAF50"; + color = Colors.open; } else if (status.includes("waitlisted")) { - color = "#FF9800" + color = Colors.waitlisted; } else if (status.includes("closed") || status.includes("cancelled")) { - color = "#FF5722"; + color = Colors.closed; } return color; } + + function buildQuery(course_data, sem) { let query = !sem ? "select * from agg" : "select * from grades"; query += " where dept like '%" + course_data["department"] + "%'"; @@ -106,6 +100,12 @@ function prettifyDaysText(arr) { function semesterSort(semA, semB) { + let semOrder = { + "Spring": 0, + "Fall": 1, + "Summer": 2, + "Winter": 3 + } let aName = semA.split(' ')[0]; let aYear = parseInt(semA.split(' ')[1]); let bName = semB.split(' ')[0]; @@ -298,4 +298,13 @@ function reformatDateTime(dtl1) { } } return output; +} + +function rgb2hex(rgb) { + rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/); + + function hex(x) { + return ("0" + parseInt(x).toString(16)).slice(-2); + } + return "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]); } \ No newline at end of file diff --git a/js/utplanner.js b/js/utplanner.js index b941124a..baf8a081 100644 --- a/js/utplanner.js +++ b/js/utplanner.js @@ -144,8 +144,6 @@ function buildTimeTitle(times) { return lines } - - function makeLine(date, time, place) { var arr = seperateDays(date) var output = prettifyDaysText(arr) @@ -154,28 +152,6 @@ function makeLine(date, time, place) { return `${output} at ${time.replace(/\./g, '').replace(/\-/g, ' to ')} in
${building}`; } - - - -function prettifyDaysText(arr) { - var output = ""; - if (arr.length > 2) { - for (var i = 0; i < arr.length; i++) { - if (i < arr.length - 1) - output += arr[i] + ", " - if (i == arr.length - 2) - output += "and "; - if (i == arr.length - 1) - output += arr[i]; - } - } else if (arr.length == 2) { - output = arr[0] + " and " + arr[1]; - } else { - output = arr[0]; - } - return output -} - function setChart(data) { //set up the chart toggleChartLoading(false); diff --git a/manifest.json b/manifest.json index a7ce7f04..7b12f4bc 100644 --- a/manifest.json +++ b/manifest.json @@ -16,19 +16,19 @@ ], "content_scripts": [{ "css": ["css/styles.css"], - "js": ["js/lib/moment.min.js", "js/lib/highcharts.js", "js/lib/jquery-3.3.1.min.js", "js/lib/jquery.initialize.min.js", "js/config.js", "js/util.js", + "js": ["js/config.js", "js/lib/moment.min.js", "js/lib/highcharts.js", "js/lib/jquery-3.3.1.min.js", "js/lib/jquery.initialize.min.js", "js/util.js", "js/Template.js", "js/courseCatalog.js" ], "matches": ["https://utdirect.utexas.edu/apps/registrar/course_schedule/*"] }, { "css": ["css/styles.css"], - "js": ["js/lib/moment.min.js", "js/lib/highcharts.js", "js/lib/jquery-3.3.1.min.js", "js/lib/jquery.initialize.min.js", "js/config.js", "js/util.js", + "js": ["js/config.js", "js/lib/moment.min.js", "js/lib/highcharts.js", "js/lib/jquery-3.3.1.min.js", "js/lib/jquery.initialize.min.js", "js/util.js", "js/Template.js", "js/utPlanner.js" ], "matches": ["https://utexas.collegescheduler.com/*"] }, { "css": ["css/styles.css"], - "js": ["js/lib/moment.min.js", "js/lib/sql-memory-growth.js", "js/lib/highcharts.js", "js/lib/jquery-3.3.1.min.js", "js/import.js"], + "js": ["js/config.js", "js/lib/moment.min.js", "js/lib/sql-memory-growth.js", "js/lib/highcharts.js", "js/lib/jquery-3.3.1.min.js", "js/import.js"], "matches": ["https://utdirect.utexas.edu/registrar/waitlist/wl_see_my_waitlists.WBX", "https://utdirect.utexas.edu/registration/classlist.WBX*"] }], "web_accessible_resources": [