Skip to content

Commit

Permalink
Loads config.json on first start (#172, #422)
Browse files Browse the repository at this point in the history
  • Loading branch information
victrme committed Sep 24, 2024
1 parent 79f8554 commit e285bb0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/scripts/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1079,8 +1079,8 @@ function resetSettings(action: 'yes' | 'no' | 'first') {
storage.sync.clear()
storage.local.clear()

setTimeout(() => {
storage.sync.set({ ...getSyncDefaults() })
setTimeout(async () => {
storage.sync.set({ ...(await getSyncDefaults()) })
storage.local.set({ ...LOCAL_DEFAULT })
fadeOut()
}, 50)
Expand All @@ -1098,7 +1098,7 @@ export function updateSettingsJSON(data?: Sync.Storage) {
function updateTextArea(data: Sync.Storage) {
const pre = document.getElementById('settings-data')

if (pre) {
if (pre && data.about) {
data.about.browser = PLATFORM
pre.textContent = orderedStringify(data)
}
Expand Down
16 changes: 13 additions & 3 deletions src/scripts/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ function verifyDataAsLocal(data: Keyval) {

export async function getSyncDefaults(): Promise<Sync.Storage> {
try {
const json = await (await fetch('defaults.json')).json()
const json = await (await fetch('config.json')).json()
return verifyDataAsSync(json)
} catch (error) {
console.log('No defaults.json settings found')
//@ts-expect-error
console.log(error.stack)
}

return SYNC_DEFAULT
Expand Down Expand Up @@ -137,10 +138,15 @@ function online(): Storage {
}

const init = async () => {
const config = await getSyncDefaults()
const about = parse<Sync.Storage>(localStorage.bonjourr)?.about

if (!localStorage.bonjourr) {
online().sync.set(config)
}

if (!about) {
online().sync.set({ about: SYNC_DEFAULT.about })
online().sync.set({ about: config.about })
}

return {
Expand Down Expand Up @@ -212,6 +218,10 @@ function webext(): Storage {
})
}

if (Object.keys(store.sync)?.length === 0) {
store.sync = await getSyncDefaults()
}

const sync = verifyDataAsSync(store.sync)
const local = verifyDataAsLocal(store.local)

Expand Down

0 comments on commit e285bb0

Please sign in to comment.