From 5aa5aca3d672b5069457cd1ab0ffcfb73b344937 Mon Sep 17 00:00:00 2001 From: sghsri Date: Sun, 10 Nov 2019 17:30:57 -0600 Subject: [PATCH] dynamic options, now store options as object rather than individually, waitlist scraping base --- js/Template.js | 21 ++++++++++++ js/background.js | 82 +++++++++++++++++++++++++++++++++------------ js/courseCatalog.js | 22 ++++++++---- js/import.js | 26 +++++++++++++- js/options.js | 81 +++++++++++++++----------------------------- manifest.json | 2 +- options.html | 14 ++------ 7 files changed, 154 insertions(+), 94 deletions(-) diff --git a/js/Template.js b/js/Template.js index 954d8f19..8467cd9d 100644 --- a/js/Template.js +++ b/js/Template.js @@ -178,4 +178,25 @@ Template.Import = class { static waitlist_import_button() { return `
`; } + + static store_waitlist_message(){ + return `

` + } + +} + +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 `

+ ${label_text} +

+ +
` + + } } diff --git a/js/background.js b/js/background.js index 0387d339..f534ec19 100644 --- a/js/background.js +++ b/js/background.js @@ -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) { diff --git a/js/courseCatalog.js b/js/courseCatalog.js index b44ea910..5c0e6f5f 100644 --- a/js/courseCatalog.js +++ b/js/courseCatalog.js @@ -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); diff --git a/js/import.js b/js/import.js index a022589b..e095bf74 100644 --- a/js/import.js +++ b/js/import.js @@ -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("
import snackbar..
"); $("#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; diff --git a/js/options.js b/js/options.js index 3154838d..3092dcef 100644 --- a/js/options.js +++ b/js/options.js @@ -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"); -} \ No newline at end of file diff --git a/manifest.json b/manifest.json index 8884d8f4..6d4ffb76 100644 --- a/manifest.json +++ b/manifest.json @@ -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": [ diff --git a/options.html b/options.html index 915f99dc..1cfce2ae 100644 --- a/options.html +++ b/options.html @@ -9,22 +9,14 @@