Skip to content

Commit

Permalink
Add getDefaults function
Browse files Browse the repository at this point in the history
  • Loading branch information
taupiqueur committed Aug 20, 2024
1 parent dade9df commit dbb778c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
8 changes: 2 additions & 6 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ import optionsWorker from './options/service_worker.js'

const { TAB_GROUP_ID_NONE } = chrome.tabGroups

// Retrieve the default config.
const gettingDefaults = fetch('config.json')
.then((response) => response.json())

/**
* Adds items to the browser’s context menu.
*
Expand Down Expand Up @@ -79,7 +75,7 @@ function onInstalled(details) {
* @returns {Promise<void>}
*/
async function onInstall() {
const defaults = await gettingDefaults
const defaults = await optionsWorker.getDefaults()
await chrome.storage.sync.set(defaults)
await chrome.tabs.create({
active: true,
Expand All @@ -94,7 +90,7 @@ async function onInstall() {
* @returns {Promise<void>}
*/
async function onUpdate(previousVersion) {
const defaults = await gettingDefaults
const defaults = await optionsWorker.getDefaults()
const localStorage = await chrome.storage.sync.get()
await chrome.storage.sync.set({
...defaults,
Expand Down
20 changes: 15 additions & 5 deletions src/options/service_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,19 @@
// Service workers: https://developer.chrome.com/docs/extensions/develop/concepts/service-workers
// Long-lived connections: https://developer.chrome.com/docs/extensions/develop/concepts/messaging#connect

// Retrieve the default config.
const gettingDefaults = fetch('config.json')
.then((response) => response.json())
/**
* Retrieves the default config.
*
* @returns {Promise<object>}
*/
async function getDefaults() {
return (
fetch('config.json')
.then((response) =>
response.json()
)
)
}

/**
* Handles a new connection when opening the “Options” page.
Expand Down Expand Up @@ -62,9 +72,9 @@ async function saveOptions(partialOptions) {
* @returns {Promise<void>}
*/
async function resetOptions() {
const defaults = await gettingDefaults
const defaults = await getDefaults()
await chrome.storage.sync.clear()
await chrome.storage.sync.set(defaults)
}

export default { onConnect }
export default { getDefaults, onConnect }

0 comments on commit dbb778c

Please sign in to comment.