aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStephen Blott2017-10-29 06:09:28 +0000
committerGitHub2017-10-29 06:09:28 +0000
commit098d24d87f0a92ace37e8ebb1c4fb032b7c08d4c (patch)
tree7795ce39ebcf411d519e4401a17d10f3513cc438
parent534a0f8c471ea96081b30fc2a57d183f6b4268c9 (diff)
parent8d5a7d585e559c55f4ec766fdf151f7f2c0fff70 (diff)
downloadvimium-098d24d87f0a92ace37e8ebb1c4fb032b7c08d4c.tar.bz2
Merge pull request #2750 from smblott-github/backup-settings
Add backup/restore for Vimium options.
-rw-r--r--lib/settings.coffee4
-rw-r--r--pages/options.coffee44
-rw-r--r--pages/options.html27
3 files changed, 74 insertions, 1 deletions
diff --git a/lib/settings.coffee b/lib/settings.coffee
index 11cf7557..fd1ef268 100644
--- a/lib/settings.coffee
+++ b/lib/settings.coffee
@@ -202,7 +202,7 @@ Settings.init()
# Perform migration from old settings versions, if this is the background page.
if Utils.isBackgroundPage()
- Settings.onLoaded ->
+ Settings.applyMigrations = ->
unless Settings.get "settingsVersion"
# This is a new install. For some settings, we retain a legacy default behaviour for existing users but
# use a non-default behaviour for new users.
@@ -218,6 +218,8 @@ if Utils.isBackgroundPage()
# be removed after 1.58 has been out for sufficiently long.
Settings.nuke "copyNonDefaultsToChromeStorage-20150717"
+ Settings.onLoaded Settings.applyMigrations.bind Settings
+
root = exports ? (window.root ?= {})
root.Settings = Settings
extend window, root unless exports?
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>