From 749b570e117f0f075d6b8324303c6cebae9525ad Mon Sep 17 00:00:00 2001 From: Shraiysh Vaishay Date: Wed, 2 Sep 2020 22:28:43 +0530 Subject: [PATCH] Showing an error message on invalid URLs. Solves #5 --- src/gpa/activateGPA.js | 5 ++++ src/popup/popup.html | 6 ++++- src/popup/popup.js | 38 +++++++++++++++++++++++++----- src/timetable/activateTimetable.js | 5 ++++ 4 files changed, 47 insertions(+), 7 deletions(-) diff --git a/src/gpa/activateGPA.js b/src/gpa/activateGPA.js index e5af4b4..a1f0a33 100644 --- a/src/gpa/activateGPA.js +++ b/src/gpa/activateGPA.js @@ -49,6 +49,11 @@ add_checkboxes = function(){ } show_total_gpa = function(){ + + if (document.location.href !== "https://aims.iith.ac.in/aims/courseReg/myCrsHistoryPage") { + return null; + } + var courses = []; $('#gpa_button').val('Calculating'); $('#gpa_bar').remove(); diff --git a/src/popup/popup.html b/src/popup/popup.html index a815d92..74291ce 100644 --- a/src/popup/popup.html +++ b/src/popup/popup.html @@ -58,7 +58,11 @@

aims helper

- +
GPA =
diff --git a/src/popup/popup.js b/src/popup/popup.js index 291ef05..e5a5f79 100644 --- a/src/popup/popup.js +++ b/src/popup/popup.js @@ -1,10 +1,21 @@ var whichButton = 0 ; // 0 : no button has been clicked + +// Show the error message div +function showError() { + document.getElementById("error-message").style.display = "block"; + document.getElementsByClassName("button-container")[0].style.display = "none"; + document.getElementById("loading-image").style.display = "none"; +} + + chrome.runtime.onMessage.addListener((request, sender) => { - if (request.action == "activateTimetable" && request.status == true) - { - onWindowLoad(); - } + if (request.action == "activateTimetable" && request.status == true) { + onWindowLoad(); + } + else { + showError(); + } }); chrome.runtime.onMessage.addListener(function(request, sender) { if (request.action == "getSource") { @@ -38,19 +49,29 @@ function onWindowLoad() { function showLoading() { document.getElementById("loading-image").style.display = "block"; document.getElementsByClassName("button-container")[0].style.display="none"; + document.getElementById("error-message").style.display = "none"; // adding to avoid any risks! } function removeLoading() { document.getElementById("loading-image").style.display = "none"; document.getElementsByClassName("button-container")[0].style.display = "block"; + document.getElementById("error-message").style.display = "none"; // adding to avoid any risks! } function injectTimetable() { showLoading(); chrome.tabs.executeScript(null, { file: "/src/timetable/activateTimetable.js" - }, function() { - if (chrome.runtime.lastError) console.log(chrome.runtime.lastError.message); + }, function () { + if (chrome.runtime.lastError) { + switch (chrome.runtime.lastError.message) { + case "Cannot access a chrome:// URL": + showError(); + break; + default: + console.log(chrome.runtime.lastError.message); + } + } }); } @@ -68,6 +89,11 @@ function injectGPA() { } chrome.runtime.onMessage.addListener(function(request, sender){ if(request.action == "parsedGPA"){ + + if (!request.data) { + return showError(); + } + removeLoading(); var gpa_value = request.data.gpa; document.getElementsByClassName("gpa-container")[0].style.display = "flex"; diff --git a/src/timetable/activateTimetable.js b/src/timetable/activateTimetable.js index f52ef3a..f259fda 100644 --- a/src/timetable/activateTimetable.js +++ b/src/timetable/activateTimetable.js @@ -1,4 +1,9 @@ function activate() { + + if (!(document.location.href === "https://aims.iith.ac.in/aims/courseReg/studentRegForm/44")) { + return false + } + var timeTabIcons = document.getElementsByClassName("time_tab_icon"); for (var i = 0 ; i < timeTabIcons.length ; i++) {