From 12837ed19ef29b0afdc2eb227c51a881600c3848 Mon Sep 17 00:00:00 2001 From: Hariharan Date: Tue, 20 Aug 2019 15:02:21 -0500 Subject: [PATCH] moved getting semesters into background, message calling in popup --- js/background.js | 82 +++++++++++++++++++++++++++++++++++------------- js/popup.js | 34 ++++++-------------- manifest.json | 3 +- 3 files changed, 72 insertions(+), 47 deletions(-) diff --git a/js/background.js b/js/background.js index 65bdd6c2..e5ebdeca 100644 --- a/js/background.js +++ b/js/background.js @@ -1,6 +1,12 @@ -updateBadge(true); var grades; +var current_semesters = {}; + +updateBadge(true); loadDataBase() +getCurrentSemesters(); + + + /* Handle messages and their commands from content and popup scripts*/ chrome.runtime.onMessage.addListener(function (request, sender, response) { switch (request.command) { @@ -32,6 +38,8 @@ chrome.runtime.onMessage.addListener(function (request, sender, response) { break; case "gradesQuery": executeQuery(request.query, response); + case "currentSemesters": + response({ semesters: current_semesters}); default: const xhr = new XMLHttpRequest(); const method = request.method ? request.method.toUpperCase() : "GET"; @@ -107,27 +115,37 @@ function executeQuery(query, sendResponse) { }); } -/* Load the database*/ -function loadDataBase() { - sql = window.SQL; - loadBinaryFile('grades.db', function (data) { - var sqldb = new SQL.Database(data); - grades = sqldb; + +function getCurrentSemesters(){ + $.get('https://registrar.utexas.edu/schedules', 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(``); + current_semesters[sem_name] = "code"; + $.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(); + if(current_semesters[name] != sem_num){ + current_semesters[name] = sem_num; + } + }); + } + }); + } + } + }); + } }); } -/* 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, new_changes) { if (new_changes) { @@ -345,4 +363,26 @@ function updateStatus(sendResponse) { console.log('updated status'); } }); -} \ No newline at end of file +} + +/* 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(); +}; \ No newline at end of file diff --git a/js/popup.js b/js/popup.js index 47bd44d4..4dcf638c 100644 --- a/js/popup.js +++ b/js/popup.js @@ -378,31 +378,15 @@ function showImportExportPopup() { } 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(``); - $.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); - }) - }); - } - }); - } - } - }); + chrome.runtime.sendMessage({ + command: "currentSemesters" + }, function(response){ + let { semesters } = response; + console.log(semesters); + let semester_names = Object.keys(semesters); + for(let i = 0; i${name}`); } }); } \ No newline at end of file diff --git a/manifest.json b/manifest.json index fbc0cb42..bee03f1f 100644 --- a/manifest.json +++ b/manifest.json @@ -35,7 +35,8 @@ "grades.db", "images/disticon.png" ], "background": { - "scripts": ["js/lib/jquery-3.3.1.min.js", "js/lib/sql-memory-growth.js", "js/lib/moment.min.js", "js/config.js", "js/background.js"], + "scripts": ["js/lib/jquery-3.3.1.min.js", "js/lib/sql-memory-growth.js", "js/lib/moment.min.js", "js/config.js", "js/util.js", + "js/background.js"], "persistent": true }, "browser_action": {