dynamic options, now store options as object rather than individually, waitlist scraping base

This commit is contained in:
sghsri
2019-11-10 17:30:57 -06:00
parent 78d8be4c99
commit 5aa5aca3d6
7 changed files with 154 additions and 94 deletions

View File

@@ -178,4 +178,25 @@ Template.Import = class {
static waitlist_import_button() {
return `<button class='material-button' id='import_waitlist' style='margin:0px'>${Text.waitlist_button_text_default}</button><br>`;
}
static store_waitlist_message(){
return `<h1 id="nextlabel"style="color: #FF9800;display:none;"></h1>`
}
}
Template.Options = class {
static options_row(key, enabled){
let button_text = enabled ? "Turn Off" : "Turn On";
let button_color = enabled ? Colors.closed : Colors.open;
let label_text = capitalizeString(key.replace(/([A-Z]+)*([A-Z][a-z])/g, "$1 $2"));
return `<h2 style="padding: 5px 16px 5px 16px; font-weight: normal;display: inline-block;text-align:left;">
${label_text}
</h2>
<button id="${key}" value=${enabled} class="material-button" style="display:inline-block;font-size:medium; float:right; background:${button_color}">
${button_text}
</button>
<br>`
}
}

View File

@@ -2,6 +2,13 @@ var grades;
var current_semesters = {};
var departments = [];
var should_open = false;
const default_options = {
"loadAll": true,
"courseConflictHighlight": true,
"storeWaitlist": true,
}
onStartup();
/* Handle messages and their commands from content and popup scripts*/
@@ -51,6 +58,12 @@ chrome.runtime.onMessage.addListener(function (request, sender, response) {
response({open : should_open});
should_open = false;
break;
case "getOptionsValue":
getOptionsValue(request.key, response);
break;
case "setOptionsValue":
setOptionsValue(request.key, request.value, response);
break;
default:
const xhr = new XMLHttpRequest();
const method = request.method ? request.method.toUpperCase() : "GET";
@@ -73,37 +86,19 @@ chrome.runtime.onMessage.addListener(function (request, sender, response) {
/* Initially set the course data in storage */
chrome.runtime.onInstalled.addListener(function (details) {
if (details.reason == "install") {
setDefaultOptions();
chrome.storage.sync.get('savedCourses', function (data) {
if (!data.savedCourses) {
var arr = new Array();
chrome.storage.sync.set({
savedCourses: arr
savedCourses: new Array()
}, function () {
console.log('initial course list');
});
chrome.storage.sync.set({
courseConflictHighlight: true
}, function () {
console.log('initial highlighting: true');
});
chrome.storage.sync.set({
loadAll: true
}, function () {
console.log('initial loadAll: true');
});
}
});
} else if (details.reason == "update") {
console.log("updated");
chrome.storage.sync.get('loadAll', function (data) {
if (data.loadAll == undefined) {
chrome.storage.sync.set({
loadAll: true
}, function () {
console.log('initial loadAll: true');
});
}
});
setDefaultOptions();
}
});
@@ -124,6 +119,51 @@ function onStartup(){
getCurrentDepartments();
}
function getOptionsValue(key, sendResponse){
chrome.storage.sync.get('options', function (data) {
if (!data.options) {
setDefaultOptions();
} else {
sendResponse({
'value': data.options[key]
});
}
});
}
function setOptionsValue(key, value, sendResponse){
chrome.storage.sync.get('options', function (data) {
let new_options = data.options;
if (!data.options) {
setDefaultOptions();
new_options = default_options;
}
new_options[key] = value;
chrome.storage.sync.set({
options: new_options
}, function () {
console.log(key);
console.log(new_options);
sendResponse({
'value': new_options[key]
});
});
});
}
function setDefaultOptions(){
chrome.storage.sync.get('options', function (data) {
if (!data.options) {
chrome.storage.sync.set({
options: default_options
}, function () {
console.log('default options:');
console.log(default_options);
});
}
});
}
function getCurrentSemesters(){
$.get('https://registrar.utexas.edu/schedules', function (response) {
if (response) {

View File

@@ -7,9 +7,13 @@ var done_loading = true;
var next = $("#next_nav_link");
if (next) {
chrome.storage.sync.get('loadAll', function (data) {
if (data.loadAll)
chrome.runtime.sendMessage({
command: "getOptionsValue",
key: "loadAll",
}, function (response) {
if(response.value){
$('[title*="next listing"]').remove();
}
});
}
@@ -181,8 +185,11 @@ function saveCourse() {
/* Update the course list to show if the row contains a course that conflicts with the saved course is one of the saved courses */
function updateListConflictHighlighting(start = 0) {
chrome.storage.sync.get('courseConflictHighlight', function (data) {
let canHighlight = data.courseConflictHighlight;
chrome.runtime.sendMessage({
command: "getOptionsValue",
key: "courseConflictHighlight",
}, function (response) {
let canHighlight = response.value;
$('table').find('tr').each(function (i) {
if (i >= start) {
if (!($(this).find('td').hasClass("course_header")) && $(this).has('th').length == 0) {
@@ -459,8 +466,11 @@ function getDescription(course_info) {
}
function loadNextPages() {
chrome.storage.sync.get('loadAll', function (data) {
if (data.loadAll) {
chrome.runtime.sendMessage({
command: "getOptionsValue",
key: "loadAll",
}, function (response) {
if(response.value){
let link = next.prop('href');
if (done_loading && next && link) {
toggleLoadingPage(true);

View File

@@ -5,9 +5,12 @@ $(function () {
sem = waitlist ? $('[name="s_ccyys"]').val() : $("option[selected='selected']").val();
if (waitlist) {
$("[href='#top']").before(Template.Import.import_button());
$("[name='wl_see_my_waitlists']").before(Template.import.store_waitlist_message());
$("[name='wl_see_my_waitlists']").after(Template.Import.waitlist_import_button());
} else
extractWaitlistInfo();
} else {
$("table").after(Template.Import.import_button());
}
$("#import").prepend("<div id='snackbar'>import snackbar..</div>");
$("#import").click(function () {
@@ -28,6 +31,27 @@ $(function () {
});
function extractWaitlistInfo(){
let class_boxes = $("[name='wl_see_my_waitlists']>table");
let waitlist_info = [];
$(class_boxes).each(function(){
let data = $(this).find('tr.tb span');
let unique_num = $(data[0]).text().trim();
let class_name = $(data[1]).text().trim().split('\n').filter(part => part.trim() != '').map(part => part.trim()).join(' ');
let waitlist_size = $(this).find('tr.tbon:eq(2) td:eq(1)').text().trim().split(' of ')[1];
waitlist_info.push({
"id": unique_num,
"class": class_name,
"wait": waitlist_size,
"time": moment().format('DD-MM-YYYY HH:mm:ss')
});
});
console.log(waitlist_info);
return waitlist_info;
}
function importButtonAnimation(button) {
let is_waitlisted_button = $(button).attr('id') == "import_waitlist";
let return_text = is_waitlisted_button ? Text.waitlist_button_text_default : Text.button_text_default;

View File

@@ -1,64 +1,37 @@
var manifestData = chrome.runtime.getManifest();
$("#version").text(manifestData.version);
chrome.storage.sync.get('courseConflictHighlight', function (data) {
if (data.courseConflictHighlight) {
off('courseConflictHighlight');
} else {
on('courseConflictHighlight');
}
});
chrome.storage.sync.get('loadAll', function (data) {
if (data.loadAll) {
off('loadAll');
} else {
on('loadAll');
}
});
$("#togglecourseConflictHighlight").click(function () {
var action = $("#togglecourseConflictHighlight").text();
if (action == "Turn Off") {
chrome.storage.sync.set({
courseConflictHighlight: false
}, function () {
on('courseConflictHighlight');
});
} else {
chrome.storage.sync.set({
courseConflictHighlight: true
}, function () {
off('courseConflictHighlight');
});
}
updateAllTabsCourseList();
});
$("#toggleloadAll").click(function () {
var action = $("#toggleloadAll").text();
if (action == "Turn Off") {
chrome.storage.sync.set({
loadAll: false
}, function () {
on('loadAll');
});
} else {
chrome.storage.sync.set({
loadAll: true
}, function () {
off('loadAll');
chrome.storage.sync.get('options', function(data){
if(data.options){
console.log(data.options);
Object.keys(data.options).forEach(key => {
let enabled = data.options[key];
$('#options_container').append(Template.Options.options_row(key, enabled));
});
}
});
$("body").on("click","button",function(){
let key = $(this).attr('id');
let old_status = $(this).val() === 'true';
let new_status = !old_status;
chrome.runtime.sendMessage({
command: "setOptionsValue",
key: key,
value: new_status
}, function (response) {
console.log(response.value);
toggle(key, response.value)
updateAllTabsCourseList();
});
});
function on(setting) {
$("#toggle" + setting).text("Turn On");
$("#toggle" + setting).css("background", "#4CAF50");
function toggle(key, value) {
let button_text = value ? "Turn Off": "Turn On";
let button_color = value ? Colors.closed : Colors.open ;
$(`#${key}`).text(button_text);
$(`#${key}`).css("background", button_color);
$(`#${key}`).val(value);
}
function off(setting) {
$("#toggle" + setting).text("Turn Off");
$("#toggle" + setting).css("background", "#F44336");
}

View File

@@ -28,7 +28,7 @@
"matches": ["https://utexas.collegescheduler.com/*"]
}, {
"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/Template.js", "js/util.js", "js/import.js"],
"js": ["js/config.js", "js/lib/moment.min.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*"]
}],
"web_accessible_resources": [

View File

@@ -9,22 +9,14 @@
<body>
<div class="card" style="width: 400px; margin-left:auto;margin-right: auto; height:auto;" id="header">
<h2 style="padding:16px 16px 0px 16px;font-size: 20px"> <u>Options</u> </h2>
<div>
<h2 style="padding: 5px 16px 5px 16px; font-weight: normal;display: inline-block;text-align:left;">Conflict
Highlighting</h2>
<button id="togglecourseConflictHighlight" class="material-button"
style="float:right; display:inline-block;font-size:medium;background:#F44336;">Turn
Off</button>
<h2 style="padding: 5px 16px 16px 16px; font-weight: normal;display: inline-block;text-align:left;">Scroll To
Load More Courses</h2>
<button id="toggleloadAll" class="material-button"
style="float: right;display:inline-block;font-size:medium;background:#F44336;">Turn
Off</button>
<div id="options_container">
</div>
<p style="padding:0px 5px 5px 0px; float: right">(v<span id="version"></span>), Sriram Hariharan, 2018</p>
<script src="js/config.js"></script>
<script src="js/lib/jquery-3.3.1.min.js"></script>
<script src="js/util.js"></script>
<script src="js/Template.js"></script>
<script src="js/options.js"></script>
</body>