Init stuff to use storage.

This commit is contained in:
deepjyoti30
2020-05-16 01:40:35 +05:30
parent 1083a14548
commit 40fedd9c78
9 changed files with 859 additions and 24 deletions

46
js/jsoneditor.min.js vendored Normal file

File diff suppressed because one or more lines are too long

View File

@@ -36,7 +36,7 @@ validWeatherUnit = [
"fah", "cel"
]
function initBody() {
async function initBody() {
/**
* Function called when the body is loaded.
*
@@ -44,7 +44,12 @@ function initBody() {
* other things.
*/
// Read the json file
json = readJSON("config.json")
storedSettings = await chrome.storage.sync.get(result => {return result})
console.log(storedSettings)
if (storedSettings == undefined || storedSettings.length == 0)
readJSON("config.json")
else
parseAndCreate(storedSettings)
}
function initSearchBar(jsonData) {
@@ -169,9 +174,14 @@ function readJSON(fileName) {
.then(response => {return response.json()})
.then(jsonData => {
parseAndCreate(jsonData)
saveSettings(jsonData)
})
}
function saveSettings(settings) {
chrome.storage.sync.set(settings)
}
function parseAndCreate(jsonData) {
/**
* Parse the passed jsonData and create div's accordingly.

View File

@@ -8,11 +8,38 @@
*/
modalId = "settings"
closeBtn = "close"
closeId = "close"
jsonContainer = "jsoneditor"
function showSettings() {
async function showSettings() {
modalEl = document.getElementById(modalId)
closeBtn = document.getElementsByClassName(closeBtn)[0]
closeBtn = document.getElementsByClassName(closeId)[0]
modalEl.style.display = "block"
closeBtn.onclick = () => {modalEl.style.display = "none"}
updatedJson = await loadJson()
closeBtn.onclick = () => {
modalEl.style.display = "none"
// Get the updated JSON
//updatedJson = editor.get()
console.log(updatedJson)
chrome.storage.sync.set(updatedJson)
document.getElementById(jsonContainer).innerHTML = ""
location.reload()
}
}
async function loadJson() {
container = document.getElementById(jsonContainer)
const options = {
mode: 'tree',
modes: ['code', 'tree', 'view']
}
const editor = new JSONEditor(container, options)
const response = await fetch("config.json")
const initialJson = await response.json()
// Populate the editor
editor.set(initialJson)
return editor.get()
}