background.js comments

This commit is contained in:
sghsri
2020-08-26 15:39:47 -05:00
parent b0188437fb
commit fefc3516c5
5 changed files with 82 additions and 83 deletions

View File

@@ -1,5 +1,5 @@
<!DOCTYPE html> <!DOCTYPE html>
<!-- This file is the html file serving the "My Schedule / Calendar" page. -->
<head> <head>
<link rel='stylesheet' href='css/fullcalendar.min.css' /> <link rel='stylesheet' href='css/fullcalendar.min.css' />
<link rel='stylesheet' href='css/_materialFullCalendar.css' /> <link rel='stylesheet' href='css/_materialFullCalendar.css' />

View File

@@ -1,8 +1,10 @@
var grades; console.log(`UT Registration Plus background page: ${window.location.href}`);
var grades; // caching the grades database in memory for faster queries
var current_semesters = {}; var current_semesters = {};
var departments = []; var departments = [];
var should_open = false; var should_open = false; // toggled flag for automatically opening popup on new pages when 'more info' hit
// these are the default options that the extension currently supports
const default_options = { const default_options = {
loadAll: true, loadAll: true,
courseConflictHighlight: true, courseConflictHighlight: true,
@@ -11,6 +13,13 @@ const default_options = {
onStartup(); onStartup();
function onStartup() {
updateBadge(true);
loadDataBase();
getCurrentSemesters();
getCurrentDepartments();
}
/* Handle messages and their commands from content and popup scripts*/ /* Handle messages and their commands from content and popup scripts*/
chrome.runtime.onMessage.addListener(function (request, sender, response) { chrome.runtime.onMessage.addListener(function (request, sender, response) {
switch (request.command) { switch (request.command) {
@@ -89,38 +98,27 @@ chrome.runtime.onInstalled.addListener(function (details) {
setDefaultOptions(); setDefaultOptions();
chrome.storage.sync.get("savedCourses", function (data) { chrome.storage.sync.get("savedCourses", function (data) {
if (!data.savedCourses) { if (!data.savedCourses) {
chrome.storage.sync.set( chrome.storage.sync.set({
{ savedCourses: [],
savedCourses: new Array(), });
},
function () {
console.log("initial course list");
}
);
} }
}); });
} else if (details.reason == "update") { } else if (details.reason == "update") {
console.log("updated"); // if there's been an update, call setDefaultOptions in case their settings have gotten wiped
setDefaultOptions(); setDefaultOptions();
console.log("updated");
} }
}); });
chrome.storage.onChanged.addListener(function (changes) { chrome.storage.onChanged.addListener(function (changes) {
for (key in changes) { for (key in changes) {
console.log(changes);
if (key === "savedCourses") { if (key === "savedCourses") {
updateBadge(false, changes.savedCourses.newValue); updateBadge(false, changes.savedCourses.newValue); // update the extension popup badge whenever the savedCourses have been changed
} }
} }
}); });
function onStartup() { // get the value of an option if it exists
updateBadge(true);
loadDataBase();
getCurrentSemesters();
getCurrentDepartments();
}
function getOptionsValue(key, sendResponse) { function getOptionsValue(key, sendResponse) {
chrome.storage.sync.get("options", function (data) { chrome.storage.sync.get("options", function (data) {
if (!data.options) { if (!data.options) {
@@ -133,10 +131,12 @@ function getOptionsValue(key, sendResponse) {
}); });
} }
// set the value of an option if it exists
function setOptionsValue(key, value, sendResponse) { function setOptionsValue(key, value, sendResponse) {
chrome.storage.sync.get("options", function (data) { chrome.storage.sync.get("options", function (data) {
let new_options = data.options; let new_options = data.options;
if (!data.options) { if (!data.options) {
// if there are no options set, set the defaults
setDefaultOptions(); setDefaultOptions();
new_options = default_options; new_options = default_options;
} }
@@ -146,8 +146,6 @@ function setOptionsValue(key, value, sendResponse) {
options: new_options, options: new_options,
}, },
function () { function () {
console.log(key);
console.log(new_options);
sendResponse({ sendResponse({
value: new_options[key], value: new_options[key],
}); });
@@ -156,6 +154,7 @@ function setOptionsValue(key, value, sendResponse) {
}); });
} }
// set the default options if the options haven't been set before
function setDefaultOptions() { function setDefaultOptions() {
chrome.storage.sync.get("options", function (data) { chrome.storage.sync.get("options", function (data) {
if (!data.options) { if (!data.options) {
@@ -164,20 +163,21 @@ function setDefaultOptions() {
options: default_options, options: default_options,
}, },
function () { function () {
console.log("default options:"); console.log("default options:", default_options);
console.log(default_options);
} }
); );
} }
}); });
} }
// scrape the registrar schedules page for caching the current active semesters
function getCurrentSemesters() { function getCurrentSemesters() {
$.get("https://registrar.utexas.edu/schedules", function (response) { $.get("https://registrar.utexas.edu/schedules", function (response) {
if (response) { if (response) {
htmlToNode(response) htmlToNode(response)
.find(".callout2>ul>li>a") .find(".callout2>ul>li>a")
.each(function (i) { .each(function (i) {
// only show as many semesters as we want to display
if (i < Popup.num_semesters) { if (i < Popup.num_semesters) {
let sem_name = $(this).text().trim(); let sem_name = $(this).text().trim();
if (sem_name != "Course Schedule Archive") { if (sem_name != "Course Schedule Archive") {
@@ -203,10 +203,10 @@ function getCurrentSemesters() {
}); });
} }
// use the utexas review api for getting the list of departments
function getCurrentDepartments() { function getCurrentDepartments() {
$.get("https://www.utexasreview.com/api/get_major", function (response) { $.get("https://www.utexasreview.com/api/get_major", function (response) {
if (response) { if (response) {
console.log("getCurrentDepartments -> response", response);
let { majors } = response; let { majors } = response;
let indices = Object.keys(majors); let indices = Object.keys(majors);
let new_departments = []; let new_departments = [];
@@ -218,6 +218,7 @@ function getCurrentDepartments() {
}); });
} }
// update the badge text to reflect the new changes
function updateBadge(first, new_changes) { function updateBadge(first, new_changes) {
if (new_changes) { if (new_changes) {
updateBadgeText(first, new_changes); updateBadgeText(first, new_changes);
@@ -229,6 +230,7 @@ function updateBadge(first, new_changes) {
} }
} }
// update the badge text to show the number of courses that have been saved by the user
function updateBadgeText(first, courses) { function updateBadgeText(first, courses) {
let badge_text = courses.length > 0 ? `${courses.length}` : ""; let badge_text = courses.length > 0 ? `${courses.length}` : "";
let flash_time = !first ? 200 : 0; let flash_time = !first ? 200 : 0;
@@ -236,6 +238,7 @@ function updateBadgeText(first, courses) {
text: badge_text, text: badge_text,
}); });
if (!first) { if (!first) {
// if isn't the first install of the extension, flash the badge to bring attention to it
chrome.browserAction.setBadgeBackgroundColor({ chrome.browserAction.setBadgeBackgroundColor({
color: Colors.badge_flash, color: Colors.badge_flash,
}); });
@@ -259,16 +262,10 @@ function checkConflicts(sendResponse) {
if (isConflict(course_a.datetimearr, course_b.datetimearr)) conflicts.push([course_a, course_b]); if (isConflict(course_a.datetimearr, course_b.datetimearr)) conflicts.push([course_a, course_b]);
} }
} }
if (conflicts.length == 0) {
sendResponse({ sendResponse({
isConflict: false, isConflict: conflicts.length === 0,
between: conflicts.length ? conflicts : undefined,
}); });
} else {
sendResponse({
isConflict: true,
between: conflicts,
});
}
}); });
} }
@@ -364,6 +361,7 @@ function alreadyContains(unique, sendResponse) {
}); });
} }
// find if a course with the current unique number exists in the user's saved courses
function contains(courses, unique) { function contains(courses, unique) {
var i = 0; var i = 0;
while (i < courses.length) { while (i < courses.length) {
@@ -375,10 +373,12 @@ function contains(courses, unique) {
return false; return false;
} }
// does it have the same unique number as provided
function isSameCourse(course, unique) { function isSameCourse(course, unique) {
return course.unique == unique; return course.unique == unique;
} }
// send a message to every tab open to updateit's course list (and thus recalculate its conflicts highlighting)
function updateTabs() { function updateTabs() {
chrome.tabs.query({}, function (tabs) { chrome.tabs.query({}, function (tabs) {
for (var i = 0; i < tabs.length; i++) { for (var i = 0; i < tabs.length; i++) {
@@ -389,52 +389,52 @@ function updateTabs() {
}); });
} }
const UPDATE_INTERVAL = 1000 * 60 * 16; // const UPDATE_INTERVAL = 1000 * 60 * 16;
setInterval(updateStatus, UPDATE_INTERVAL); // setInterval(updateStatus, UPDATE_INTERVAL);
// updateStatus(); // // updateStatus();
function updateStatus(sendResponse) { // function updateStatus(sendResponse) {
chrome.storage.sync.get("savedCourses", function (data) { // chrome.storage.sync.get("savedCourses", function (data) {
var courses = data.savedCourses; // var courses = data.savedCourses;
var no_change = true; // var no_change = true;
for (let i = 0; i < courses.length; i++) { // for (let i = 0; i < courses.length; i++) {
try { // try {
let c = courses[i]; // let c = courses[i];
let old_status = c.status; // let old_status = c.status;
let old_link = c.link; // let old_link = c.link;
$.ajax({ // $.ajax({
url: old_link, // url: old_link,
success: function (result) { // success: function (result) {
if (result) { // if (result) {
console.log(result); // console.log(result);
var object = $("<div/>").html(result).contents(); // var object = $("<div/>").html(result).contents();
let new_status = object.find('[data-th="Status"]').text(); // let new_status = object.find('[data-th="Status"]').text();
let register_link = object.find('td[data-th="Add"] a'); // let register_link = object.find('td[data-th="Add"] a');
if (register_link) register_link = register_link.attr("href"); // if (register_link) register_link = register_link.attr("href");
var haschanged = new_status == old_status && register_link == old_link; // var haschanged = new_status == old_status && register_link == old_link;
if (!haschanged) console.log(c.unique + " updated from " + old_status + " to " + new_status + " and " + old_link + " to " + register_link); // if (!haschanged) console.log(c.unique + " updated from " + old_status + " to " + new_status + " and " + old_link + " to " + register_link);
no_change &= haschanged; // no_change &= haschanged;
c.registerlink = register_link; // c.registerlink = register_link;
c.status = new_status; // c.status = new_status;
} // }
}, // },
}); // });
} catch (e) { // } catch (e) {
console.log(e); // console.log(e);
console.log("Not logged into UT Coursebook. Could not update class statuses."); // console.log("Not logged into UT Coursebook. Could not update class statuses.");
} // }
} // }
if (!no_change) { // if (!no_change) {
chrome.storage.sync.set({ // chrome.storage.sync.set({
savedCourses: courses, // savedCourses: courses,
}); // });
console.log("updated status"); // console.log("updated status");
} // }
}); // });
} // }
// execute a query on the grades database
function executeQuery(query, sendResponse) { function executeQuery(query, sendResponse) {
console.log(grades);
var res = grades.exec(query)[0]; var res = grades.exec(query)[0];
sendResponse({ sendResponse({
data: res, data: res,

View File

@@ -1,6 +1,5 @@
{ {
"manifest_version": 2, "manifest_version": 2,
"name": "UT Registration Plus", "name": "UT Registration Plus",
"version": "1.2.2.6", "version": "1.2.2.6",
"options_page": "options.html", "options_page": "options.html",

View File

@@ -1,5 +1,6 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<!-- This file is serving as the template for the options page -->
<head> <head>
<meta name="viewport" content="width=device-width, initial-scale=1" /> <meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" href="css/styles.css" /> <link rel="stylesheet" href="css/styles.css" />
@@ -15,7 +16,6 @@
<div id="options_container"></div> <div id="options_container"></div>
<p class="creator-tag"><a href="https://sghsri.github.io">Sriram Hariharan</a> (2018)</p> <p class="creator-tag"><a href="https://sghsri.github.io">Sriram Hariharan</a> (2018)</p>
</div> </div>
<div class="card options-card" id="contributors_container"> <div class="card options-card" id="contributors_container">
<h3 class="contributor-title">Amazing people who've contributed to the extension!</h3> <h3 class="contributor-title">Amazing people who've contributed to the extension!</h3>
<p class="creator-tag open-source-tag">Code is open source here <a href="https://github.com/sghsri/UT-Registration-Plus">here</a> :)</p> <p class="creator-tag open-source-tag">Code is open source here <a href="https://github.com/sghsri/UT-Registration-Plus">here</a> :)</p>

View File

@@ -1,6 +1,6 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<!-- This file is serving as the page for the browser action popup -->
<head> <head>
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="css/popup.css"> <link rel="stylesheet" href="css/popup.css">