import {syncWithServer} from '../auto_store.js'; //export {getCategoryFromDatabase, addCategory}; /* Fetch elements from HTML page */ document.getElementById("addButton").onclick = function(){addCategory();}; document.getElementById("closeButton").onclick = function(){window.close();}; /* Generic error handler */ function onError(error){ console.log(error); } function getCategoryFromDatabase(category){ var base64category = btoa(category); console.log("Candidate: ", category, " B64: ", base64category); /* Get and store in local storage DB.*/ var oldCategoryIdName = browser.storage.local.get("category").then((categoryArray) => {console.log(categoryArray["category"]); setDatabase(categoryArray, category, base64category);}); } function generateId(){ var randInt = Math.floor(Math.random() * (9999 - 1000 + 1)) + 1000; return randInt; } function setDatabase(oldCategory, newCategory){ let existingCategories = Object.values(oldCategory); let combinedCategories = []; // Recursively loop through array of arrays. var recursiveArray = function(arr){ if(typeof(arr) == Object || typeof(arr) == Array || typeof(arr) == "object"){ let y; let len = existingCategories.length; for (y = 0; y < len; y++){ console.log("|- Looping through existing categories array: ", arr[y]); if(typeof(arr[y]) == Object || typeof(arr[y]) == Array || typeof(arr[y]) == "object"){ let it; let len_ = arr[y].length; for(it = 0; it < len_; it++){ combinedCategories.push(arr[y][it]); console.log("combined: ", arr[y][it]); } } recursiveArray(arr[y]); } } // IF not an array, then add string to combined array, given that it is not undefined. else if(typeof(arr) == "string"){ console.log("|-- Added string to combinedCategories array: ", arr); if(combinedCategories.includes(arr) == false){ combinedCategories.push(arr.toUpperCase()); combinedCategories.sort(); } } else if(arr == null){ console.log("Categories array is null"); } else{ console.log("Not in recursive array loop. Type is: ", typeof(arr), " value: ", arr); } } /* Find array of arrays in existing categories */ recursiveArray(existingCategories); recursiveArray(newCategory); /* Add newly created category as well if(combinedCategories.includes(newCategory) == false){ recursiveArray(newCategory) combinedCategories.push(newCategory.toUpperCase()); console.log("UNIQUE: ", newCategory, typeof(newCategory) ," added. ", combinedCategories); combinedCategories.sort(); }*/ /* Make a dictionary to store in web extension local storage. if(existingCategories.includes(newCategory.toUpperCase()) == false){ if(Array.isArray(newCategory == false){ existingCategories.push(newCategory); let storeCategories = {category: existingCategories}; console.log("|- STORECATEGORIES: ", storeCategories); browser.storage.local.set(storeCategories).then(function(){console.log(storeCategories, "-> Saved to local storage.");}); } } else{ console.log("Not adding duplicate category ", typeof(newCategory), newCategory); }*/ let storeCategories = {category: combinedCategories}; browser.storage.local.set(storeCategories).then(function(){console.log(storeCategories, "-> Saved to local storage.");}); } function setItem(){ console.log("Category added to .json database."); } function addCategory(){ var textArea = document.getElementById('myTextarea'); var category = textArea.value; if(category != ""){ console.log(category, " added."); getCategoryFromDatabase(category); textArea.value = ""; } else{ console.log("Text area provides null."); } }