From 720474858f2f52ad62934b43c346b49165c461ac Mon Sep 17 00:00:00 2001
From: Stephen Blott
Date: Sat, 28 Oct 2017 15:35:39 +0100
Subject: 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.
---
lib/settings.coffee | 4 +++-
pages/options.coffee | 39 +++++++++++++++++++++++++++++++++++++++
pages/options.html | 26 ++++++++++++++++++++++++++
3 files changed, 68 insertions(+), 1 deletion(-)
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..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}
diff --git a/pages/options.html b/pages/options.html
index 46307b6f..943fc95c 100644
--- a/pages/options.html
+++ b/pages/options.html
@@ -315,6 +315,32 @@ b: http://b.com/?q=%s description
-->
+
+
+
Backup and Restore
+
+
+
Backup
+
+
+
+
+
+
+
+
+
+
Restore
+
+
+
+ Choose file, then click Save Changes, below, to confirm restore.
+
+
+
+
+
+
--
cgit v1.2.3
From b1f6da21a882f84965de73eea175475f5d8f7b37 Mon Sep 17 00:00:00 2001
From: mrmr1993
Date: Sat, 28 Oct 2017 16:42:51 +0100
Subject: FF: Show backup download link in the options page
Firefox doesn't seem to respect the |download| attribute for links, and
it garbage collects our blob URL when we navigate away from the options
page (to the object URL). We work around this by simply showing the
download link in Firefox.
Disappointingly, the user needs to right-click the link and choose to
download from the context menu.
Behaviour in Chrome is unchanged, except that the download link is now
visible.
---
pages/options.coffee | 15 +++++++--------
pages/options.html | 1 +
2 files changed, 8 insertions(+), 8 deletions(-)
diff --git a/pages/options.coffee b/pages/options.coffee
index 0038c3f9..416b3ea7 100644
--- a/pages/options.coffee
+++ b/pages/options.coffee
@@ -333,15 +333,14 @@ DomUtils?.documentReady ->
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"
+ # 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 ]
+ url = bgWin.URL.createObjectURL blob
+ a = $ "backupLink"
a.href = url
- a.download = "vimium-options-#{new Date().toISOString().split("T")[0]}.json"
- a.click()
- document.body.removeChild a
+ a.style.display = ""
+ a.click() unless Utils.isFirefox()
$("chooseFile").addEventListener "change", (event) ->
document.activeElement?.blur()
diff --git a/pages/options.html b/pages/options.html
index 943fc95c..79cb0999 100644
--- a/pages/options.html
+++ b/pages/options.html
@@ -327,6 +327,7 @@ b: http://b.com/?q=%s description
+ Download Backup