Skip to content

Commit aa98a1d

Browse files
committed
Implement backup + restore
1 parent 096d1d4 commit aa98a1d

File tree

3 files changed

+111
-16
lines changed

3 files changed

+111
-16
lines changed

moment.min.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

options.html

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
<title>DynamicHistory Settings</title>
1818
<!-- JQuery 2.2.0 -->
1919
<script src="jquery-2.2.0.min.js"></script>
20+
<!-- Moment -->
21+
<script src="moment.min.js"></script>
2022
<!-- Shared -->
2123
<script src="shared.js"></script>
2224
<!-- Core Javascript -->
@@ -57,10 +59,17 @@
5759

5860
.modal-header {
5961
padding: 2px 16px;
60-
background-color: #f44336;
6162
color: white;
6263
}
6364

65+
#clear_modal .modal-header {
66+
background-color: #f44336;
67+
}
68+
69+
#restore_modal .modal-header {
70+
background-color: #4caf50;
71+
}
72+
6473
.modal-body {padding: 2px 16px;}
6574

6675
.modal-footer {
@@ -167,7 +176,12 @@ <h3>History processor templates:</h3>
167176
</div>
168177

169178
<h2>Useful actions</h2>
170-
<button id="clear_btn">Reset All Settings</button>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a id="oninstall_link" style="display:none" href="oninstall.html">View Installation Instructions</a>
179+
<button id="clear_btn">Reset All Settings</button>
180+
<button id="backup_btn">Backup Settings</button>
181+
<button id="restore_btn">Restore Settings</button>
182+
<input id="file_input" style="display:none" type="file" id="file-input" />
183+
<br /><br />
184+
<a id="oninstall_link" style="display:none" href="oninstall.html">View Installation Instructions</a>
171185
</div>
172186

173187
<div id="clear_modal" class="modal">
@@ -186,5 +200,19 @@ <h2>Reset all settings?</h2>
186200
</div>
187201
</div>
188202
</div>
203+
<div id="restore_modal" class="modal">
204+
<div class="modal-content">
205+
<div class="modal-header">
206+
<span class="close">&times;</span>
207+
<h2>Restore complete!</h2>
208+
</div>
209+
<div class="modal-body">
210+
<p>Your settings have been restored!</p>
211+
</div>
212+
<div class="modal-footer">
213+
<button id="restore_close_btn">Close</button>
214+
</div>
215+
</div>
216+
</div>
189217
</body>
190218
</html>

options.js

Lines changed: 80 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function storage() {
1919
}
2020

2121
//Load settings
22-
function loadSettings() {
22+
function loadSettings(callback) {
2323
storage().get(DEFAULT_OPTIONS(), function(items) {
2424
$('#danger_domains').val(items.dangerDomains);
2525
$('#safe_domains').val(items.safeDomains);
@@ -54,6 +54,9 @@ function loadSettings() {
5454
$(this).css('color', 'grey');
5555
}
5656
});
57+
58+
if(callback)
59+
callback();
5760
});
5861
}
5962
//Save settings
@@ -81,15 +84,16 @@ function saveSettings() {
8184
outlineColor: $('#outline_color').val(),
8285
badgeText: $('#badge_text').val(),
8386
badgeColor: $('#badge_color').val(),
84-
cssCode: $('#css_code').val(),
85-
jsCode: $('#js_code').val()
87+
cssCode: ignoreP($('#css_code')),
88+
jsCode: ignoreP($('#js_code'))
8689
}, function() {
8790
//Update background page
8891
chrome.runtime.sendMessage({
8992
action: "updateSettings"
9093
});
9194
});
9295
}
96+
9397
//Update disabled inputs and stuff
9498
function updateInputStates() {
9599
$('#prefix_text').attr("disabled", !$("#do_prefix").is(":checked"));
@@ -170,34 +174,39 @@ function bindTemplate(name, code) {
170174
t.appendChild(document.createElement("br"));
171175
}
172176

177+
function scrollToTop() {
178+
//Scroll to top hack
179+
$("#body").css("height", "500px");
180+
$("#body").css("overflow-y", "hidden");
181+
setTimeout(function() {
182+
$("#body").css("height", "");
183+
$("#body").css("overflow-y", "visible");
184+
}, 100);
185+
}
186+
173187
jQuery(document).ready(function () {
174188
//Hook placeholders
175189
$('textarea').focus(function(){
176190
if($(this).val() === pVal($(this))){
177191
$(this).val('');
178-
$(this).css('color', 'black');
179192
}
193+
$(this).css('color', 'black');
180194
});
181195
$('textarea').blur(function(){
182-
if($(this).val() ===''){
196+
if($(this).val() === ''){
183197
$(this).val(pVal($(this)));
184198
$(this).css('color', 'grey');
185199
}
186200
});
187201

188202
//Allows resetting of all settings
203+
let clearModal = $("#clear_modal");
189204
$("#clear_btn").click(function () {
190-
$("#clear_modal").show();
191-
//Scroll to top hack
192-
$("#body").css("height", "500px");
193-
$("#body").css("overflow-y", "hidden");
194-
setTimeout(function() {
195-
$("#body").css("height", "");
196-
$("#body").css("overflow-y", "visible");
197-
}, 100);
205+
clearModal.show();
206+
scrollToTop();
198207
});
199208
let hideModal = function() {
200-
$("#clear_modal").hide();
209+
clearModal.hide();
201210
};
202211
$("#clear_modal .close").click(hideModal);
203212
$("#clear_no_btn").click(hideModal);
@@ -239,4 +248,61 @@ jQuery(document).ready(function () {
239248

240249
//Load initial settings
241250
loadSettings();
251+
252+
$('#backup_btn').click(function() {
253+
storage().get(DEFAULT_OPTIONS(), function(items) {
254+
dlFile("DynamicHistory_" + moment().format() + ".json", JSON.stringify(items));
255+
});
256+
});
257+
258+
let fileInput = $('#file_input');
259+
260+
$('#restore_btn').click(function() {
261+
//Clear old restore
262+
fileInput.prop('value', null);
263+
fileInput.click();
264+
});
265+
266+
fileInput.on('change', function() {
267+
readBackup(fileInput);
268+
});
269+
242270
});
271+
272+
function readBackup(element) {
273+
let file = element[0].files[0]
274+
if (file) {
275+
let fr = new FileReader();
276+
fr.onload = function(e) {
277+
storage().set(JSON.parse(e.target.result), function() {
278+
loadSettings(function() {
279+
saveAndUpdate();
280+
let restoreModal = $("#restore_modal");
281+
restoreModal.show();
282+
scrollToTop();
283+
284+
//Hack to trigger textarea placeholder updaters
285+
$('textarea').focus();
286+
$('textarea').blur();
287+
288+
let hideModal = function() {
289+
restoreModal.hide();
290+
};
291+
$("#restore_modal .close").click(hideModal);
292+
$("#restore_close_btn").click(hideModal);
293+
});
294+
});
295+
};
296+
fr.readAsText(file);
297+
}
298+
}
299+
300+
function dlFile(filename, data) {
301+
let blob = new Blob([data], {type: 'text/json'});
302+
let elem = window.document.createElement('a');
303+
elem.href = window.URL.createObjectURL(blob);
304+
elem.download = filename;
305+
document.body.appendChild(elem);
306+
elem.click();
307+
elem.remove();
308+
}

0 commit comments

Comments
 (0)