From 531d2a82ed9d2e9af363710f0bf0cf2ab8440528 Mon Sep 17 00:00:00 2001
From: kaffem <29717789+kaffem@users.noreply.github.com>
Date: Sat, 15 Aug 2020 21:36:17 +0200
Subject: [PATCH] #32 added a SettingsProvider which supports all
chrome.storage.local functions
---
manifest.json | 1 +
settings/SettingsProvider.js | 151 +++++++++++++++++++++++++++++++++++
settings/settings.html | 1 +
3 files changed, 153 insertions(+)
create mode 100644 settings/SettingsProvider.js
diff --git a/manifest.json b/manifest.json
index a1dd607..a206238 100644
--- a/manifest.json
+++ b/manifest.json
@@ -17,6 +17,7 @@
"*://aniwatch.me/*"
],
"js": [
+ "settings/SettingsProvider.js",
"utils/colors.js",
"utils/helpers.js",
"utils/aniwatchCore.js"
diff --git a/settings/SettingsProvider.js b/settings/SettingsProvider.js
new file mode 100644
index 0000000..77a5118
--- /dev/null
+++ b/settings/SettingsProvider.js
@@ -0,0 +1,151 @@
+// Currently only supports local storage
+
+let data = {
+ 'usedBytes': {}
+};
+
+// Get all the keys + values from the storage
+function getStorageData(keys) {
+ chrome.storage.local.get(keys, function(values) {
+ Object.assign(data, values);
+ });
+}
+
+// Save all the keys + values to the storage
+function setStorageData(items) {
+ chrome.storage.local.set(items);
+}
+
+// Get Data from data{}
+// or just use data[key] or data.key to grab it
+function getStorageEntry(entry) {
+ if (typeof entry === 'string') {
+ if (data.hasOwnProperty(entry)) {
+ console.log(entry);
+ console.log(data[entry]);
+ return data[entry];
+ }
+ else {
+ console.log("Initialize Data first.");
+ }
+ }
+}
+
+// Get bytes used by local storage
+// Accepts nothing, an object or a string
+function getStorageBytesInUse(keys) {
+ // Get all used bytes
+ if (typeof keys === 'undefined' || (typeof keys === 'objects' && Object.keys(keys).length === 0)){
+ console.log("No keys given, grabbing total bytes used.");
+ chrome.storage.local.getBytesInUse(null, function(value) {
+ if (chrome.runtime.lastError) {
+ console.log("Error retrieving entry: " + chrome.runtime.lastError);
+ return;
+ }
+ else {
+ console.log(`Used storage bytes: ${value}`);
+ data.usedBytes.total = value;
+ }
+ });
+ }
+ // Get bytes for the provided object
+ else if (typeof keys === 'object') {
+ if (Object.keys(keys).length === 1) {
+ console.log(keys);
+ console.log(`Key given, grabbing total bytes used by ${keys}.`);
+ chrome.storage.local.getBytesInUse(keys, function(value) {
+ if (chrome.runtime.lastError) {
+ console.log("Error retrieving entry: " + chrome.runtime.lastError);
+ return;
+ }
+ console.log(`Used storage bytes by ${keys}: ${value}`);
+ data.usedBytes[keys] = value;
+ });
+ }
+ else if (Object.keys(keys).length > 1) {
+ console.log(keys);
+ console.log(`Multiple keys given, grabbing total bytes used by ${keys}.`);
+ for (var key in keys) {
+ let temp = keys[key];
+ console.log(temp);
+ chrome.storage.local.getBytesInUse(temp, function(value) {
+ if (chrome.runtime.lastError) {
+ console.log("Error retrieving entry: " + chrome.runtime.lastError);
+ return;
+ }
+ else {
+ console.log(`Used storage bytes by ${temp}: ${value}`);
+ data.usedBytes[temp] = value;
+ }
+ });
+ }
+ }
+ }
+ // Get bytes for the provided string
+ else if (typeof keys === 'string') {
+ console.log(keys);
+ console.log(`Key given, grabbing total bytes used by ${keys}.`);
+ chrome.storage.local.getBytesInUse(keys, function(values) {
+ if (chrome.runtime.lastError) {
+ console.log("Error retrieving entry: " + chrome.runtime.lastError);
+ return;
+ }
+ else {
+ console.log(`Used storage bytes by ${keys}: ${values}`);
+ data.usedBytes[keys] = values;
+ }
+ });
+
+ }
+}
+
+// Remove the provided Entry from local storage
+function removeStorageEntry(keys) {
+ // stop if undefined or empty object
+ if (typeof keys === 'undefined' || (typeof keys === 'objects' && Object.keys(keys).length === 0)) {
+ console.log("thats not how this works.");
+ return;
+ }
+ // handle keys if object
+ else if (typeof keys === 'object') {
+ if (Object.keys(keys).length >= 1) {
+ console.log(keys);
+ for (var key in keys) {
+ let temp = keys[key];
+ chrome.storage.local.remove(temp, function() {
+ if (chrome.runtime.lastError) {
+ console.log("Error retrieving entry: " + chrome.runtime.lastError);
+ return;
+ }
+ else {
+ console.log("deleting");
+ delete data[temp];
+ }
+ });
+ }
+ return;
+ }
+ }
+ // handle keys as string
+ else if (typeof keys === 'string') {
+ console.log("reached");
+ chrome.storage.local.remove(keys, function() {
+ if (chrome.runtime.lastError) {
+ console.log("Error retrieving entry: " + chrome.runtime.lastError);
+ return;
+ }
+ else {
+ delete data[keys];
+ }
+ });
+ }
+}
+
+// Clear the storage completely
+// also clears data
+function clearStorageEntries() {
+ chrome.storage.local.clear();
+ data = {};
+}
+
+// missing onChanged Event
diff --git a/settings/settings.html b/settings/settings.html
index 3d3b2ec..c0bb0b8 100644
--- a/settings/settings.html
+++ b/settings/settings.html
@@ -18,5 +18,6 @@
+