refactored database loading and querying to background script

This commit is contained in:
Sriram Hariharan
2019-08-04 13:02:51 -05:00
parent c1cf6dd730
commit 21ca2d4c38
4 changed files with 160 additions and 137 deletions

View File

@@ -1,4 +1,6 @@
updateBadge(true);
var grades;
loadDataBase()
/* Handle messages and their commands from content and popup scripts*/
chrome.runtime.onMessage.addListener(function (request, sender, response) {
switch (request.command) {
@@ -31,6 +33,8 @@ chrome.runtime.onMessage.addListener(function(request, sender, response) {
case "updateCourseList":
updateTabs();
break;
case "gradesQuery":
executeQuery(request.query, response);
default:
const xhr = new XMLHttpRequest();
const method = request.method ? request.method.toUpperCase() : "GET";
@@ -88,6 +92,37 @@ chrome.runtime.onInstalled.addListener(function(details) {
});
function executeQuery(query, sendResponse) {
console.log(grades)
var res = grades.exec(query)[0];
sendResponse({
data: res,
});
}
/* Load the database*/
function loadDataBase() {
sql = window.SQL;
loadBinaryFile('grades.db', function (data) {
var sqldb = new SQL.Database(data);
grades = sqldb;
});
}
/* load the database from file */
function loadBinaryFile(path, success) {
var xhr = new XMLHttpRequest();
xhr.open("GET", chrome.extension.getURL(path), true);
xhr.responseType = "arraybuffer";
xhr.onload = function () {
var data = new Uint8Array(xhr.response);
var arr = new Array();
for (var i = 0; i != data.length; ++i) arr[i] = String.fromCharCode(data[i]);
success(arr.join(""));
};
xhr.send();
};
function updateBadge(first) {
chrome.storage.sync.get('savedCourses', function (data) {
if (data.savedCourses) {
@@ -268,7 +303,9 @@ function updateStatus(sendResponse) {
let c = courses[i];
let oldstatus = c.status;
let oldlink = c.link;
$.ajax({url: oldlink, success: function(result){
$.ajax({
url: oldlink,
success: function (result) {
if (result) {
console.log(result);
var object = $('<div/>').html(result).contents();
@@ -284,7 +321,9 @@ function updateStatus(sendResponse) {
nochange &= haschanged;
c.registerlink = registerlink;
c.status = newstatus;
}}});
}
}
});
} catch (e) {

View File

@@ -34,9 +34,6 @@ const butdelay = 75;
console.log('UT Registration Plus is running on this page.');
var utplanner = false;
if (document.querySelector('#fos_fl')) {
let params = (new URL(document.location)).searchParams;
let dep = params.get("fos_fl");
@@ -58,18 +55,7 @@ if(next){
}
});
}
if($('html').hasClass('gr__utexas_collegescheduler_com')){
utplanner = true;
$.initialize("table.section-detail-grid", function() {
$(this).find('thead>tr').append('<th> Plus</th')
$(this).find('tbody>tr').each(function(){
$(this).append(`<td data-th="Plus"><input type="image" class="distButton" id="distButton" style="vertical-align: bottom; display:block;" width="20" height="20" src='${chrome.extension.getURL('images/disticon.png')}'/></td>`);
})
});
}
loadDataBase();
//make heading and modal
if (!$("#kw_results_table").length) {
$("table thead th:last-child").after('<th scope=col>Plus</th>');
@@ -470,8 +456,12 @@ function getDistribution(sem) {
query += "and sem like '%" + sem + "%'";
}
query += "order by a1+a2+a3+b1+b2+b3+c1+c2+c3+d1+d2+d3+f desc";
var res = grades.exec(query)[0];
var output = "";
alert(query)
chrome.runtime.sendMessage({
command: "gradesQuery",
query: query
}, function (response) {
var res = response.data;
if (!sem) {
openDialog(department, coursename, "aggregate", profname, res);
} else {
@@ -483,14 +473,13 @@ function getDistribution(sem) {
}
setChart(data);
}
});
}
/*Open the modal and show all the data*/
function openDialog(dep, cls, sem, professor, res) {
$("#myModal").fadeIn(fadetime);
//initial text on the "save course button"
chrome.runtime.sendMessage({
command: "alreadyContains",
unique: uniquenum
@@ -734,7 +723,9 @@ function getDescription() {
// console.log(window.location.href);
// console.log(profurl);
console.log('hello');
$.ajax({url: profurl, success: function(response){
$.ajax({
url: profurl,
success: function (response) {
if (response) {
console.log(profurl);
var output = "";
@@ -785,26 +776,6 @@ function getDescription() {
rmpLink = "http://www.ratemyprofessors.com/campusRatings.jsp?sid=1255";
eCISLink = "http://utdirect.utexas.edu/ctl/ecis/results/index.WBX?";
}
}});
}
/* Load the database*/
function loadDataBase() {
sql = window.SQL;
loadBinaryFile('grades.db', function (data) {
var sqldb = new SQL.Database(data);
grades = sqldb;
});
}
/* load the database from file */
function loadBinaryFile(path, success) {
var xhr = new XMLHttpRequest();
xhr.open("GET", chrome.extension.getURL(path), true);
xhr.responseType = "arraybuffer";
xhr.onload = function () {
var data = new Uint8Array(xhr.response);
var arr = new Array();
for (var i = 0; i != data.length; ++i) arr[i] = String.fromCharCode(data[i]);
success(arr.join(""));
};
xhr.send();
};

9
js/utplanner.js Normal file
View File

@@ -0,0 +1,9 @@
if ($('html').hasClass('gr__utexas_collegescheduler_com')) {
$.initialize("table.section-detail-grid", function () {
console.log('hello')
$(this).find('thead>tr').append('<th> Plus</th')
$(this).find('tbody>tr').each(function () {
$(this).append(`<td data-th="Plus"><input type="image" class="distButton" id="distButton" style="vertical-align: bottom;" width="20" height="20" src='${chrome.extension.getURL('images/disticon.png')}'/></td>`);
})
});
}

View File

@@ -9,15 +9,19 @@
"declarativeContent",
"storage",
"*://*.utdirect.utexas.edu/apps/registrar/course_schedule/*",
"*://*.utexas.collegescheduler.com/terms/*/schedules/*",
"*://*.utexas.collegescheduler.com/*",
"*://*.catalog.utexas.edu/ribbit/",
"*://*.registrar.utexas.edu/schedules/*",
"*://*.login.utexas.edu/login/*"
],
"content_scripts": [{
"css": ["css/styles.css"],
"js": ["js/moment.min.js", "js/sql-memory-growth.js", "js/highcharts.js", "js/jquery-3.3.1.min.js", "js/jquery.initialize.min.js", "js/content.js"],
"matches": ["https://utdirect.utexas.edu/apps/registrar/course_schedule/*", "https://utexas.collegescheduler.com/terms/*"]
"js": ["js/moment.min.js", "js/highcharts.js", "js/jquery-3.3.1.min.js", "js/jquery.initialize.min.js", "js/content.js"],
"matches": ["https://utdirect.utexas.edu/apps/registrar/course_schedule/*"]
}, {
"css": ["css/styles.css"],
"js": ["js/moment.min.js", "js/highcharts.js", "js/jquery-3.3.1.min.js", "js/jquery.initialize.min.js", "js/utplanner.js"],
"matches": ["https://utexas.collegescheduler.com/*"]
}, {
"css": ["css/styles.css"],
"js": ["js/moment.min.js", "js/sql-memory-growth.js", "js/highcharts.js", "js/jquery-3.3.1.min.js", "js/import.js"],
@@ -27,7 +31,7 @@
"grades.db", "images/disticon.png"
],
"background": {
"scripts": ["js/jquery-3.3.1.min.js", "js/background.js", "js/moment.min.js"],
"scripts": ["js/jquery-3.3.1.min.js", "js/sql-memory-growth.js", "js/background.js", "js/moment.min.js"],
"persistent": true
},
"browser_action": {