diff options
| author | Stephen Blott | 2017-10-28 15:35:39 +0100 |
|---|---|---|
| committer | Stephen Blott | 2017-10-28 15:35:39 +0100 |
| commit | 720474858f2f52ad62934b43c346b49165c461ac (patch) | |
| tree | 1a6c782bcdd1ffd5d4b546ca36505315730c2f4e /pages/options.coffee | |
| parent | 534a0f8c471ea96081b30fc2a57d183f6b4268c9 (diff) | |
| download | vimium-720474858f2f52ad62934b43c346b49165c461ac.tar.bz2 | |
Add backup/restore for Vimium options.
See the *very* bottom of the options page (below advanced settings).
Clicking "Backup" creates a JSON file.
Selecting a backup populates the options inputs, the user then clicks *Save Changes* to confirm.
Diffstat (limited to 'pages/options.coffee')
| -rw-r--r-- | pages/options.coffee | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/pages/options.coffee b/pages/options.coffee index 035dd403..0038c3f9 100644 --- a/pages/options.coffee +++ b/pages/options.coffee @@ -42,6 +42,8 @@ class Option # Static method. @saveOptions: -> Option.all.map (option) -> option.save() + # We need to apply migrations in case we are restoring an old backup. + bgSettings.applyMigrations() # Abstract method; only implemented in sub-classes. # Populate the option's DOM element (@element) with the setting's current value. @@ -323,6 +325,43 @@ document.addEventListener "DOMContentLoaded", -> xhr.send() +# +# Backup and restore. "?" is for the tests." +DomUtils?.documentReady -> + $("backupButton").addEventListener "click", -> + document.activeElement?.blur() + backup = {} + for option in Option.all + backup[option.field] = option.readValueFromElement() + blob = new Blob [ JSON.stringify backup ] + url = window.URL.createObjectURL blob + a = document.createElement "a" + document.body.appendChild a + a.style = "display: none" + a.href = url + a.download = "vimium-options-#{new Date().toISOString().split("T")[0]}.json" + a.click() + document.body.removeChild a + + $("chooseFile").addEventListener "change", (event) -> + document.activeElement?.blur() + files = event.target.files + if files.length == 1 + file = files[0] + reader = new FileReader + reader.readAsText file + reader.onload = (event) -> + try + backup = JSON.parse reader.result + catch + alert "Failed to parse Vimium backup." + return + + for option in Option.all + if option.field of backup + option.populateElement backup[option.field] + option.onUpdated() + # Exported for tests. root = exports ? window extend root, {Options, isVimiumOptionsPage: true} |
