From 95a74a55aa277e77e1c8090777e18e6f506ee6ce Mon Sep 17 00:00:00 2001 From: Nicolas Stepien Date: Thu, 21 Feb 2013 19:11:08 +0100 Subject: [PATCH] Add import/export settings buttons in the Settings. (v2 edition) #702 --- 4chan_x.user.js | 73 ++++++++++++++++++++++++++++++++++++++++++++++++- changelog | 2 ++ script.coffee | 56 ++++++++++++++++++++++++++++++++++++- 3 files changed, 129 insertions(+), 2 deletions(-) diff --git a/4chan_x.user.js b/4chan_x.user.js index 30b04f018..7ca3b976c 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -2737,7 +2737,14 @@
\
\ \ -
\ +
\ +
\ + \ + \ + \ +
\ +

\ +
\ \
\
Sauce is disabled.
\ @@ -2819,6 +2826,9 @@
\
' }); + $.on($('#main_tab + div .export', dialog), 'click', Options["export"]); + $.on($('#main_tab + div .import', dialog), 'click', Options["import"]); + $.on($('#main_tab + div input', dialog), 'change', Options.onImport); _ref = Config.main; for (key in _ref) { obj = _ref[key]; @@ -2994,6 +3004,67 @@ Favicon["switch"](); Unread.update(true); return this.nextElementSibling.innerHTML = " "; + }, + "export": function() { + var a, data, now, output; + now = Date.now(); + data = { + version: Main.version, + date: now, + Conf: Conf, + WatchedThreads: $.get('watched', {}) + }; + a = $.el('a', { + className: 'warning', + textContent: 'Save me!', + download: "4chan X v" + Main.version + "-" + now + ".json", + href: "data:application/json;base64," + (btoa(unescape(encodeURIComponent(JSON.stringify(data))))), + target: '_blank' + }); + if ($.engine !== 'gecko') { + a.click(); + return; + } + output = this.parentNode.nextElementSibling; + output.innerHTML = null; + return $.add(output, a); + }, + "import": function() { + return this.nextElementSibling.click(); + }, + onImport: function() { + var file, output, reader; + if (!(file = this.files[0])) { + return; + } + output = this.parentNode.nextElementSibling; + if (!confirm('Your current settings will be entirely overwritten, are you sure?')) { + output.textContent = 'Import aborted.'; + return; + } + reader = new FileReader(); + reader.onload = function(e) { + var data; + try { + data = JSON.parse(decodeURIComponent(escape(e.target.result))); + Options.loadSettings(data); + if (confirm('Import successful. Refresh now?')) { + return window.location.reload(); + } + } catch (err) { + return output.textContent = 'Import failed due to an error.'; + } + }; + return reader.readAsText(file); + }, + loadSettings: function(data) { + var key, val, _ref; + _ref = data.Conf; + for (key in _ref) { + val = _ref[key]; + $.set(key, val); + } + return $.set('watched', data.WatchedThreads); } }; diff --git a/changelog b/changelog index 77bfc088d..1dc2b03a9 100644 --- a/changelog +++ b/changelog @@ -1,4 +1,6 @@ master +- Mayhem + Add Import/Export settings. 2.38.1 - Mayhem diff --git a/script.coffee b/script.coffee index 55a3debe7..2fc2f4381 100644 --- a/script.coffee +++ b/script.coffee @@ -2185,7 +2185,14 @@ Options =
-
+
+
+ + + +
+

+
Sauce is disabled.
@@ -2267,6 +2274,10 @@ Options =
' + $.on $('#main_tab + div .export', dialog), 'click', Options.export + $.on $('#main_tab + div .import', dialog), 'click', Options.import + $.on $('#main_tab + div input', dialog), 'change', Options.onImport + #main for key, obj of Config.main ul = $.el 'ul', @@ -2423,6 +2434,49 @@ Options = Unread.update true @nextElementSibling.innerHTML = " " + export: -> + now = Date.now() + data = + version: Main.version + date: now + Conf: Conf + WatchedThreads: $.get('watched', {}) + a = $.el 'a', + className: 'warning' + textContent: 'Save me!' + download: "4chan X v#{Main.version}-#{now}.json" + href: "data:application/json;base64,#{btoa unescape encodeURIComponent JSON.stringify data}" + target: '_blank' + if $.engine isnt 'gecko' + a.click() + return + # XXX Firefox won't let us download automatically. + output = @parentNode.nextElementSibling + output.innerHTML = null + $.add output, a + import: -> + @nextElementSibling.click() + onImport: -> + return unless file = @files[0] + output = @parentNode.nextElementSibling + unless confirm 'Your current settings will be entirely overwritten, are you sure?' + output.textContent = 'Import aborted.' + return + reader = new FileReader() + reader.onload = (e) -> + try + data = JSON.parse decodeURIComponent escape e.target.result + Options.loadSettings data + if confirm 'Import successful. Refresh now?' + window.location.reload() + catch err + output.textContent = 'Import failed due to an error.' + reader.readAsText file + loadSettings: (data) -> + for key, val of data.Conf + $.set key, val + $.set 'watched', data.WatchedThreads + Updater = init: -> html = '
'