diff options
| author | Stephen Blott | 2017-10-29 06:09:28 +0000 |
|---|---|---|
| committer | GitHub | 2017-10-29 06:09:28 +0000 |
| commit | 098d24d87f0a92ace37e8ebb1c4fb032b7c08d4c (patch) | |
| tree | 7795ce39ebcf411d519e4401a17d10f3513cc438 /pages | |
| parent | 534a0f8c471ea96081b30fc2a57d183f6b4268c9 (diff) | |
| parent | 8d5a7d585e559c55f4ec766fdf151f7f2c0fff70 (diff) | |
| download | vimium-098d24d87f0a92ace37e8ebb1c4fb032b7c08d4c.tar.bz2 | |
Merge pull request #2750 from smblott-github/backup-settings
Add backup/restore for Vimium options.
Diffstat (limited to 'pages')
| -rw-r--r-- | pages/options.coffee | 44 | ||||
| -rw-r--r-- | pages/options.html | 27 |
2 files changed, 71 insertions, 0 deletions
diff --git a/pages/options.coffee b/pages/options.coffee index 035dd403..45d8942e 100644 --- a/pages/options.coffee +++ b/pages/options.coffee @@ -1,6 +1,7 @@ $ = (id) -> document.getElementById id bgExclusions = chrome.extension.getBackgroundPage().Exclusions +restoreSettingsVersion = null # We have to use Settings from the background page here (not Settings, directly) to avoid a race condition for # the page popup. Specifically, we must ensure that the settings have been updated on the background page @@ -42,6 +43,12 @@ class Option # Static method. @saveOptions: -> Option.all.map (option) -> option.save() + # If we're restoring a backup, then restore the backed up settingsVersion. + if restoreSettingsVersion? + bgSettings.set "settingsVersion", restoreSettingsVersion + restoreSettingsVersion = null + # 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 +330,43 @@ document.addEventListener "DOMContentLoaded", -> xhr.send() +# +# Backup and restore. "?" is for the tests." +DomUtils?.documentReady -> + $("backupButton").addEventListener "click", -> + document.activeElement?.blur() + backup = settingsVersion: bgSettings.get "settingsVersion" + for option in Option.all + backup[option.field] = option.readValueFromElement() + # Create the blob in the background page so it isn't garbage collected when the page closes in FF. + bgWin = chrome.extension.getBackgroundPage() + blob = new bgWin.Blob [ JSON.stringify backup, null, 2 ] + url = bgWin.URL.createObjectURL blob + a = $ "backupLink" + a.href = url + a.style.display = "" + a.click() unless Utils.isFirefox() + + $("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 + + restoreSettingsVersion = backup["settingsVersion"] if "settingsVersion" of backup + 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} diff --git a/pages/options.html b/pages/options.html index 46307b6f..79cb0999 100644 --- a/pages/options.html +++ b/pages/options.html @@ -315,6 +315,33 @@ b: http://b.com/?q=%s description </tr> --> </tbody> + <tbody id='backupAndRestor'> + <tr> + <td colspan="2"><header>Backup and Restore</header></td> + </tr> + <tr> + <td class="caption">Backup</td> + <td> + <div class="help"> + <div class="example"> + </div> + </div> + <input id="backupButton" type="button" value="Create Backup" /> + <a id="backupLink" style="display: none" download="vimium-options.json">Download Backup</span> + </td> + </tr> + <tr> + <td class="caption">Restore</td> + <td> + <div class="help"> + <div class="example"> + Choose file, then click <i>Save Changes</i>, below, to confirm restore. + </div> + </div> + <input id="chooseFile" type="file" accept=".json" style="width: 200px;"/> + </td> + </tr> + </tbody> </table> </div> |
