From 491f7b79382900389036cea0af63886f0bef1c18 Mon Sep 17 00:00:00 2001 From: Aaron Opell Date: Tue, 16 Jan 2018 23:46:46 -0800 Subject: [PATCH] Added ability to disable notifications, added grades link Closes #2 (finally, I think, it seems to work) Closes #17 --- css/all.css | 11 ++++++++-- js/all.js | 18 +++++++++------- js/background.js | 56 +++++++++++++++++++++++++++++------------------- js/preload.js | 39 +++++++++++++++++++++++++++------ manifest.json | 2 +- 5 files changed, 86 insertions(+), 40 deletions(-) diff --git a/css/all.css b/css/all.css index c2d2c9ff..2dd80e90 100644 --- a/css/all.css +++ b/css/all.css @@ -181,7 +181,7 @@ video.easter-egg { } .setting-entry { - padding-bottom: 4px; + margin-bottom: 8px; } .setting-modified { @@ -190,5 +190,12 @@ video.easter-egg { .modal-button { margin-left: 0 !important; - margin-top: 4px; + margin-top: 8px; +} + +.modal-body input[type=text], .modal-body input[type=number] { + color: #333; + border: 1px solid #c7c7c7; + font-family: "Lucida Grande", Verdana, Helvetica, Arial, sans-serif; + padding: 4px; } \ No newline at end of file diff --git a/js/all.js b/js/all.js index e6647657..707a7cfd 100644 --- a/js/all.js +++ b/js/all.js @@ -1,15 +1,13 @@ // Page Modifications let svg = ''; -let modalHTML = ''; +let modalHTML = ``; +document.getElementById("primary-home").insertAdjacentElement("afterEnd", createElement("li", undefined, { id: "primary-home", innerHTML: 'Grades' })); document.getElementById("home").innerHTML = svg; document.body.appendChild(document.createElement("div")).innerHTML = modalHTML; let modal = document.getElementById("settings-modal"); -let modalFooterText = document.querySelector(".modal-footer-text"); -modalFooterText.textContent += ` | Schoology Plus v${chrome.runtime.getManifest().version}`; - let video = document.body.appendChild(createElement("video", ["easter-egg"], { onended: function () { this.style.visibility = "hidden"; @@ -57,12 +55,16 @@ window.onclick = function (event) { } } +function openOptionsMenu() { + modalBody.innerHTML = ""; + modalBody.appendChild(getModalContents()); + document.getElementById("settings-modal").style.display = "block"; +} + function modalClose() { - if(anySettingsModified()){ - if(!confirm("You have unsaved settings.\nAre you sure you want to exit?")) return; + if (anySettingsModified()) { + if (!confirm("You have unsaved settings.\nAre you sure you want to exit?")) return; updateSettings(); - modalBody.innerHTML = ""; - modalBody.appendChild(getModalContents()); } modal.style.display = "none"; } \ No newline at end of file diff --git a/js/background.js b/js/background.js index 68d56c65..c22df339 100644 --- a/js/background.js +++ b/js/background.js @@ -28,19 +28,19 @@ chrome.browserAction.onClicked.addListener(function () { }); function onAlarm(alarm) { - if (alarm && alarm.name === "notification") { - try { - console.log(`[${new Date()}] Checking for new notifications`); - fetch("https://lms.lausd.net/home/notifications?filter=all", { - credentials: "same-origin" - }).then(function (response) { - if (response.ok) { - return response.json(); - } - throw new Error("Error loading notifications: " + response); - }).then(function (response) { - chrome.storage.sync.get("lastTime", function (value) { - console.log(value); + chrome.storage.sync.get(null, function (value) { + if (alarm && alarm.name === "notification" && value.notifications != "disabled") { + try { + console.log(`[${new Date()}] Checking for new notifications`); + fetch("https://lms.lausd.net/home/notifications?filter=all", { + credentials: "same-origin" + }).then(function (response) { + if (response.ok) { + return response.json(); + } + throw new Error("Error loading notifications: " + response); + }).then(function (response) { + console.log("Last new grade: " + new Date(value.lastTime).toString()); let time = value.lastTime; let timeModified = false; if (!time) { @@ -94,11 +94,19 @@ function onAlarm(alarm) { isClickable: true }; console.dir(n); - chrome.browserAction.getBadgeText({}, x => { - let num = Number.parseInt(x); - chrome.browserAction.setBadgeText({ text: (num ? num + totalAssignments : totalAssignments).toString() }); - }); - chrome.notifications.create("gradeNotification", n, null); + if (!value.notifications || value.notifications == "enabled" || value.notifications == "badge") { + chrome.browserAction.getBadgeText({}, x => { + let num = Number.parseInt(x); + chrome.browserAction.setBadgeText({ text: (num ? num + totalAssignments : totalAssignments).toString() }); + }); + } else { + console.log("Number badge is disabled"); + } + if (!value.notifications || value.notifications == "enabled" || value.notifications == "popup") { + chrome.notifications.create("gradeNotification", n, null); + } else { + console.log("Popup notifications are disabled"); + } } if (timeModified) { @@ -106,10 +114,14 @@ function onAlarm(alarm) { } else { console.log("No new notifications"); } + }); - }); - } catch (error) { - console.error(error); + } catch (error) { + console.error(error); + } + } + else if (value.notifications === "disabled") { + console.log("Notifications are disabled"); } - } + }); } \ No newline at end of file diff --git a/js/preload.js b/js/preload.js index 57410551..b893e6a1 100644 --- a/js/preload.js +++ b/js/preload.js @@ -8,10 +8,6 @@ function getModalContents() { return modalContents; } -function openOptionsMenu() { - document.getElementById("settings-modal").style.display = "block"; -} - let rainbowInterval = undefined; let rainbowColor = 0; function colorLoop() { @@ -102,6 +98,35 @@ function updateSettings() { }, element => element.value === "true" ), + createSetting( + "notifications", + "Desktop Notifications", + "Displays desktop notifications and a number badge on the extension button when new grades are entered", + "select", + { + options: [ + { + text: "Number Badge and Desktop Notifications", + value: "enabled" + }, + { + text: "Number Badge Only", + value: "badge" + }, + { + text: "Desktop Notifications Only", + value: "popup" + }, + { + text: "Disabled", + value: "disabled" + } + ] + }, + (value, element) => element.value = value || "enabled", + undefined, + element => element.value + ), createElement("span", ["submit-span-wrapper", "modal-button"], { onclick: saveSettings }, [createElement("input", ["form-submit"], { type: "button", value: "Save Settings", id: "save-settings" })]) ]); }); @@ -174,7 +199,7 @@ function settingModified(event) { let element = event.target || event; let parent = element.parentElement; if (parent && !parent.querySelector(".setting-modified")) { - parent.appendChild(createElement("span", ["setting-modified"], { textContent: " *" })); + parent.appendChild(createElement("span", ["setting-modified"], { textContent: " *", title: "This setting has been modified from its saved value" })); } let setting = settings[element.dataset.settingName]; setting.modified = true; @@ -182,8 +207,8 @@ function settingModified(event) { } function anySettingsModified() { - for(let setting in settings) { - if(settings[setting].modified) { + for (let setting in settings) { + if (settings[setting].modified) { return true; } } diff --git a/manifest.json b/manifest.json index 1d229bc9..d4b50dd8 100644 --- a/manifest.json +++ b/manifest.json @@ -2,7 +2,7 @@ "manifest_version": 2, "name": "Schoology Plus", "description": "Provides some enhancements to your LAUSD Schoology experience", - "version": "3.0.2", + "version": "3.1", "icons": { "128": "imgs/icon@128.png", "64": "imgs/icon@64.png",