Init stuff to use storage.
This commit is contained in:
46
js/jsoneditor.min.js
vendored
Normal file
46
js/jsoneditor.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
14
js/main.js
14
js/main.js
@@ -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.
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
Reference in New Issue
Block a user