refactored import

This commit is contained in:
Sriram Hariharan
2019-08-13 18:20:57 -05:00
parent e4e3898a67
commit fffb49bcdf
5 changed files with 146 additions and 126 deletions

View File

@@ -144,4 +144,13 @@ class Template {
} }
} }
static Import = class Import {
static button_text_default = "<span style='font-size:small'>Import to </span><b>UT Reg Plus<b>";
static import_button() {
return `<button class='matbut' id='import' style='margin:20px 0px 20px 0px;'>${this.button_text_default}</button><br>`;
}
}
} }

View File

@@ -413,7 +413,8 @@ function getDescription(course_info) {
url: course_info["individual"], url: course_info["individual"],
success: function (response) { success: function (response) {
if (response) { if (response) {
description_lines = htmlToNode(response).find('#details > p').toArray().map(x => $(x).text()); let response_node = htmlToNode(response);
description_lines = response_node.find('#details > p').toArray().map(x => $(x).text());
displayDescription(buildFormattedDescription(description_lines)); displayDescription(buildFormattedDescription(description_lines));
let first_name = extractFirstName(response_node); let first_name = extractFirstName(response_node);
updateLinks(course_info, first_name); updateLinks(course_info, first_name);

View File

@@ -1,119 +1,128 @@
var link;
var waitlist; var waitlist;
var sem; var sem;
$(function () { $(function () {
//template https://utdirect.utexas.edu/apps/registrar/course_schedule/20189/51475/
console.log(window.location.href);
var importbutton = "<button class='matbut' id='import' style='margin:20px 0px 20px 0px;'><span style='font-size:small'>Import into </span><b>UT Reg Plus<b></h2></button><br>";
waitlist = !(window.location.href.includes('https://utdirect.utexas.edu/registration/classlist.WBX')); waitlist = !(window.location.href.includes('https://utdirect.utexas.edu/registration/classlist.WBX'));
if (waitlist) { sem = waitlist ? $('[name="s_ccyys"]').val() : $("option[selected='selected']").val();
sem = $('[name="s_ccyys"]').val(); if (waitlist)
$("[href='#top']").before(importbutton); $("[href='#top']").before(Template.Import.import_button());
} else { else
sem = $("option[selected='selected']").val(); $("table").after(Template.Import.import_button());
$("table").after(importbutton); $("#import").prepend("<div id='snackbar'>import snackbar..</div>");
}
$("#import").prepend("<div id='snackbar'>defaultmessage..</div>");
$("#import").click(function () {
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 () {
let unique = $(this).text().replace(/\s/g, '');
link = `https://utdirect.utexas.edu/apps/registrar/course_schedule/${sem}/${unique}/`;
getInfo();
});
}
$("#import").text("Courses Saved!").css("background-color", "#4CAF50");
setTimeout(function () {
$("#import").html("<span style='font-size:small'>Import into </span><b>UT Reg Plus<b></h2>").css("background-color", "#FF9800");
}, 1000);
});
$("#import").click(function () {
search_nodes = waitlist ? $(".tbg").last().find(".tbon>td:first-child") : $("tr>td:first-child");
$(search_nodes).each(function () {
importCourse($(this));
})
importButtonAnimation();
});
}); });
/*Course object for passing to background*/
function Course(coursename, unique, profname, datetimearr, status, link, registerlink) { function importButtonAnimation() {
this.coursename = coursename; $("#import").text("Courses Saved!").css("background-color", Colors.open);
this.unique = unique; setTimeout(function () {
this.profname = profname; $("#import").html(Template.Import.button_text_default).css('background-color', Colors.waitlisted);
this.datetimearr = datetimearr; }, 1000);
this.status = status; }
this.link = link;
this.registerlink = registerlink; function importCourse(unique_node) {
let unique = $(unique_node).text().replace(/\s/g, '');
link = `https://utdirect.utexas.edu/apps/registrar/course_schedule/${sem}/${unique}/`;
buildAddCourse(link);
} }
function getInfo(classurl) { function buildAddCourse(link) {
var xhr = new XMLHttpRequest(); $.get(link, function (response) {
xhr.open("GET", link, false); if (response) {
xhr.send(); let simp_course = buildSimplifiedCourseObject(response);
var response = xhr.responseText;
if (response) {
var output = "";
var object = $('<div/>').html(response).contents();
var c = getCourseObject(object);
console.log(c);
chrome.runtime.sendMessage({
command: "courseStorage",
course: c,
action: "add"
}, function () {
chrome.runtime.sendMessage({ chrome.runtime.sendMessage({
command: "updateCourseList" command: "courseStorage",
course: simp_course,
action: "add"
}, function () {
chrome.runtime.sendMessage({
command: "updateCourseList"
});
}); });
}); }
} })
} }
function buildSimplifiedCourseObject(response) {
let imported_course = getCourseObject(htmlToNode(response));
let {
full_name,
unique,
prof_name,
status,
individual,
register
} = curr_course;
let dtarr = getDayTimeArray(undefined, curr_course);
return new Course(full_name, unique, prof_name, dtarr, status, individual, register);
}
/*For a row, get all the course information and add the date-time-lines*/ /*For a row, get all the course information and add the date-time-lines*/
function getCourseObject(object) { function getCourseObject(response_node) {
let coursename = object.find("#details h2").text(); let course_name = $(response_node).find("#details h2").text();
let uniquenum = object.find('td[data-th="Unique"]').text(); let course_row = $(response_node).find('table');
let profname = object.find("td[data-th='Instructor']").text().split(', ')[0]; let individual = document.URL;
if (profname.indexOf(" ") == 0) { curr_course = buildBasicCourseInfo(course_row, course_name, individual);
profname = profname.substring(1); }
function buildBasicCourseInfo(row, course_name, individual) {
let {
name,
department,
number
} = seperateCourseNameParts(course_name);
let instructor_text = $(row).find('td[data-th="Instructor"]').text();
let has_initial = instructor_text.indexOf(',') > 0;
course_info = {
"full_name": course_name,
"name": name,
"department": department,
"number": number,
"individual": individual ? individual : $(row).find('td[data-th="Unique"] a').prop('href'),
"register": $(row).find('td[data-th="Add"] a').prop('href'),
"unique": $(row).find('td[data-th="Unique"]').text(),
"status": $(row).find('td[data-th="Status"]').text(),
"prof_name": instructor_text ? has_initial ? capitalizeString(instructor_text.split(', ')[0]) : capitalizeString(instructor_text) : "Undecided",
"initial": instructor_text && has_initial ? instructor_text.split(', ')[1].substring(0, 1) : "",
"time_data": {
"days": $(row).find('td[data-th="Days"]>span').toArray().map(x => $(x).text().trim()),
"times": $(row).find('td[data-th="Hour"]>span').toArray().map(x => $(x).text().trim()),
"places": $(row).find('td[data-th="Room"]>span').toArray().map(x => $(x).text().trim())
},
"links": {}
} }
let datetimearr = getDtarr(object); return course_info;
let status = object.find('td[data-th="Status"]').text();
let indlink = link;
let registerlink = object.find('td[data-th="Add"] a').prop('href');
return new Course(coursename, uniquenum, profname, datetimearr, status, indlink, registerlink);
} }
/* For a row, get the date-time-array for checking conflicts*/ /* For a row, get the date-time-array for checking conflicts*/
function getDtarr(object) { function getDayTimeArray(row, course_info) {
var numlines = object.find('td[data-th="Days"]>span').length; var day_time_array = []
var dtarr = []; let days = course_info ? course_info["time_data"]["days"] : $(row).find('td[data-th="Days"]>span').toArray().map(x => $(x).text().trim());
for (var i = 0; i < numlines; i++) { let times = course_info ? course_info["time_data"]["times"] : $(row).find('td[data-th="Hour"]>span').toArray().map(x => $(x).text().trim());
var date = object.find('td[data-th="Days"]>span:eq(' + i + ')').text(); let places = course_info ? course_info["time_data"]["places"] : $(row).find('td[data-th="Room"]>span').toArray().map(x => $(x).text().trim());
var time = object.find('td[data-th="Hour"]>span:eq(' + i + ')').text(); for (var i = 0; i < days.length; i++) {
var place = object.find('td[data-th="Room"]>span:eq(' + i + ')').text(); let date = days[i];
let time = times[i];
let place = places[i];
for (var j = 0; j < date.length; j++) { for (var j = 0; j < date.length; j++) {
var letter = date.charAt(j); let letter = date.charAt(j);
var day = "";
if (letter == "T" && j < date.length - 1 && date.charAt(j + 1) == "H") { if (letter == "T" && j < date.length - 1 && date.charAt(j + 1) == "H") {
dtarr.push(["TH", convertTime(time), place]); day_time_array.push(["TH", convertTime(time), place]);
} else { } else {
if (letter != "H") { if (letter != "H")
dtarr.push([letter, convertTime(time), place]); day_time_array.push([letter, convertTime(time), place]);
}
} }
} }
} }
return dtarr; return day_time_array;
}
/*Convert time to 24hour format*/
function convertTime(time) {
var converted = time.replace(/\./g, '').split("-");
for (var i = 0; i < 2; i++) {
converted[i] = moment(converted[i], ["h:mm A"]).format("HH:mm");
}
return converted;
} }

View File

@@ -84,8 +84,8 @@ $("#clear").click(function () {
chrome.storage.sync.set({ chrome.storage.sync.set({
savedCourses: [] savedCourses: []
}); });
$("#courseList").empty();
updateAllTabsCourseList(); updateAllTabsCourseList();
$("#courseList").empty()
showEmpty(); showEmpty();
}); });
@@ -291,37 +291,6 @@ function handleMoreInfo(clicked_item, curr_course) {
}); });
} }
function getSemesters() {
var schedule_list = 'https://registrar.utexas.edu/schedules';
$.get(schedule_list, function (response) {
if (response) {
htmlToNode(response).find('.callout2>ul>li>a').each(function (i) {
if (i < Popup.num_semesters) {
let sem_name = $(this).text().trim();
if (sem_name != "Course Schedule Archive") {
$("#semesters").append(`<option>${sem_name}</option>`);
$.get($(this).attr('href'), function (response) {
if (response) {
let response_node = htmlToNode(response);
let name = response_node.find(".page-title").text().substring(17).trim();
response_node.find('.gobutton>a').each(function () {
let link = $(this).attr('href');
var sem_num = link.substring(link.lastIndexOf('/') + 1).trim();
$("option").each(function () {
if ($(this).text() == name)
$(this).val(sem_num);
})
});
}
});
}
}
});
}
});
}
function handleEmpty() { function handleEmpty() {
if (courses.length != 0) { if (courses.length != 0) {
$("#empty").hide(); $("#empty").hide();
@@ -392,3 +361,35 @@ function showImportExportPopup() {
$("#impexp>i").text('close'); $("#impexp>i").text('close');
$("#import-export-popup").removeClass('hide'); $("#import-export-popup").removeClass('hide');
} }
function getSemesters() {
var schedule_list = 'https://registrar.utexas.edu/schedules';
$.get(schedule_list, function (response) {
if (response) {
htmlToNode(response).find('.callout2>ul>li>a').each(function (i) {
if (i < Popup.num_semesters) {
let sem_name = $(this).text().trim();
if (sem_name != "Course Schedule Archive") {
$("#semesters").append(`<option>${sem_name}</option>`);
$.get($(this).attr('href'), function (response) {
if (response) {
let response_node = htmlToNode(response);
let name = response_node.find(".page-title").text().substring(17).trim();
response_node.find('.gobutton>a').each(function () {
let link = $(this).attr('href');
var sem_num = link.substring(link.lastIndexOf('/') + 1).trim();
$("option").each(function () {
if ($(this).text() == name)
$(this).val(sem_num);
})
});
}
});
}
}
});
}
});
}

View File

@@ -28,7 +28,7 @@
"matches": ["https://utexas.collegescheduler.com/*"] "matches": ["https://utexas.collegescheduler.com/*"]
}, { }, {
"css": ["css/styles.css"], "css": ["css/styles.css"],
"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"], "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/Template.js", "js/util.js", "js/import.js"],
"matches": ["https://utdirect.utexas.edu/registrar/waitlist/wl_see_my_waitlists.WBX", "https://utdirect.utexas.edu/registration/classlist.WBX*"] "matches": ["https://utdirect.utexas.edu/registrar/waitlist/wl_see_my_waitlists.WBX", "https://utdirect.utexas.edu/registration/classlist.WBX*"]
}], }],
"web_accessible_resources": [ "web_accessible_resources": [