Adding support to use the storage API for browsers.

This commit is contained in:
deepjyoti30
2020-05-16 11:38:50 +05:30
parent 40fedd9c78
commit e24a6638cc
2 changed files with 39 additions and 28 deletions

View File

@@ -36,7 +36,7 @@ validWeatherUnit = [
"fah", "cel" "fah", "cel"
] ]
async function initBody() { function initBody() {
/** /**
* Function called when the body is loaded. * Function called when the body is loaded.
* *
@@ -44,12 +44,9 @@ async function initBody() {
* other things. * other things.
*/ */
// Read the json file // Read the json file
storedSettings = await chrome.storage.sync.get(result => {return result}) BROWSER.storage.sync.get(result => {
console.log(storedSettings) Object.keys(result).length == 0 ? readJSON("config.json") : parseAndCreate(result)
if (storedSettings == undefined || storedSettings.length == 0) })
readJSON("config.json")
else
parseAndCreate(storedSettings)
} }
function initSearchBar(jsonData) { function initSearchBar(jsonData) {
@@ -155,7 +152,6 @@ function updateWeather(weatherConfig) {
.then(jsonData => { .then(jsonData => {
temp = Math.floor(jsonData["main"]["temp"]) temp = Math.floor(jsonData["main"]["temp"])
weatherType = jsonData["weather"][0]["main"] weatherType = jsonData["weather"][0]["main"]
console.log(temp, weatherType)
temp = !unit.includes("cel") ? temp = !unit.includes("cel") ?
getFahrenheit(temp) + "°F" : temp + "°C" getFahrenheit(temp) + "°F" : temp + "°C"
@@ -179,7 +175,7 @@ function readJSON(fileName) {
} }
function saveSettings(settings) { function saveSettings(settings) {
chrome.storage.sync.set(settings) BROWSER.storage.sync.set(settings)
} }
function parseAndCreate(jsonData) { function parseAndCreate(jsonData) {

View File

@@ -11,35 +11,50 @@ modalId = "settings"
closeId = "close" closeId = "close"
jsonContainer = "jsoneditor" jsonContainer = "jsoneditor"
async function showSettings() { // Detect browser
BROWSER = detectBrowser()
function showSettings() {
modalEl = document.getElementById(modalId) modalEl = document.getElementById(modalId)
closeBtn = document.getElementsByClassName(closeId)[0] closeBtn = document.getElementsByClassName(closeId)[0]
modalEl.style.display = "block" modalEl.style.display = "block"
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() { // Define the jsonEditor
container = document.getElementById(jsonContainer) container = document.getElementById(jsonContainer)
const options = { const options = {
mode: 'tree', mode: 'tree',
modes: ['code', 'tree', 'view'] modes: ['code', 'tree', 'view']
} }
const editor = new JSONEditor(container, options) const editor = new JSONEditor(container, options)
loadJson(editor)
const response = await fetch("config.json") closeBtn.onclick = () => {
const initialJson = await response.json() modalEl.style.display = "none"
// Get the updated JSON
// Populate the editor updatedJson = editor.get()
editor.set(initialJson) BROWSER.storage.sync.set(updatedJson)
document.getElementById(jsonContainer).innerHTML = ""
location.reload()
}
}
return editor.get() async function loadJson(editor) {
BROWSER.storage.sync.get(async result => {
if (Object.keys(result).length == 0) {
const response = await fetch("config.json")
result = await response.json()
}
// Populate the editor
editor.set(result)
})
}
function detectBrowser() {
// Firefox
if (typeof InstallTrigger !== 'undefined')
BROWSER = browser
else if (!!window.chrome && (!!window.chrome.webstore || !!window.chrome.runtime))
BROWSER = chrome
return BROWSER
} }