Skip to content

Commit

Permalink
feat: reset all preferences to default values
Browse files Browse the repository at this point in the history
This is a small UX feature that lets users to fallback
to 'safe' defaults after they went crazy with custom settings
and are not sure why things stopped working :-)
  • Loading branch information
lidel committed Oct 8, 2017
1 parent 8a43015 commit 7759546
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
18 changes: 17 additions & 1 deletion add-on/src/options/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
font: caption;
font-size: 1rem;
}
strong {
font-weight: bold;
}
em {
font-style: italic;
}
Expand Down Expand Up @@ -38,7 +41,6 @@
}
fieldset > div > * {
align-self: center;

}
label {
flex: 1;
Expand All @@ -59,6 +61,10 @@
padding: .5rem;
font-family: monospace;
}
#resetAllOptions {
flex: 1;
text-align: center;
}
input[type=text],
input[type=url],
input[type=number],
Expand Down Expand Up @@ -191,6 +197,16 @@
</label>
<input type="checkbox" id="dnslink" />
</div>
<div>
<label for="resetAllOptions">
<dl>
<dt>Reset Everything<dt>
<dd>Replaces user preferences with default ones<br>
<strong>(this can't be undone!)</strong></dd>
</dl>
</label>
<span id="resetAllOptions"><button>Reset Everything!</button></span>
</div>
</fieldset>
</form>

Expand Down
19 changes: 18 additions & 1 deletion add-on/src/options/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,25 @@ function readAllOptions () {
Object.keys(optionDefaults).map(key => readOption(key))
}

function resetAllOptions (event) {
// go over every key and set its value to a default one
Object.keys(optionDefaults).map(async key => {
const change = {}
change[key] = optionDefaults[key]
await browser.storage.local.set(change)
readOption(key)
})
window.scrollTo(0, 0)
event.target.disabled = true
setTimeout(() => { event.target.disabled = false }, 3000)
event.preventDefault()
}

// initial load
document.addEventListener('DOMContentLoaded', readAllOptions)
document.addEventListener('DOMContentLoaded', () => {
readAllOptions()
document.querySelector('#resetAllOptions > button').addEventListener('click', resetAllOptions)
})

// update on external changes such as browserAction menu
browser.storage.onChanged.addListener(readAllOptions)

0 comments on commit 7759546

Please sign in to comment.