@@ -19,7 +19,7 @@ function storage() {
19
19
}
20
20
21
21
//Load settings
22
- function loadSettings ( ) {
22
+ function loadSettings ( callback ) {
23
23
storage ( ) . get ( DEFAULT_OPTIONS ( ) , function ( items ) {
24
24
$ ( '#danger_domains' ) . val ( items . dangerDomains ) ;
25
25
$ ( '#safe_domains' ) . val ( items . safeDomains ) ;
@@ -54,6 +54,9 @@ function loadSettings() {
54
54
$ ( this ) . css ( 'color' , 'grey' ) ;
55
55
}
56
56
} ) ;
57
+
58
+ if ( callback )
59
+ callback ( ) ;
57
60
} ) ;
58
61
}
59
62
//Save settings
@@ -81,15 +84,16 @@ function saveSettings() {
81
84
outlineColor : $ ( '#outline_color' ) . val ( ) ,
82
85
badgeText : $ ( '#badge_text' ) . val ( ) ,
83
86
badgeColor : $ ( '#badge_color' ) . val ( ) ,
84
- cssCode : $ ( '#css_code' ) . val ( ) ,
85
- jsCode : $ ( '#js_code' ) . val ( )
87
+ cssCode : ignoreP ( $ ( '#css_code' ) ) ,
88
+ jsCode : ignoreP ( $ ( '#js_code' ) )
86
89
} , function ( ) {
87
90
//Update background page
88
91
chrome . runtime . sendMessage ( {
89
92
action : "updateSettings"
90
93
} ) ;
91
94
} ) ;
92
95
}
96
+
93
97
//Update disabled inputs and stuff
94
98
function updateInputStates ( ) {
95
99
$ ( '#prefix_text' ) . attr ( "disabled" , ! $ ( "#do_prefix" ) . is ( ":checked" ) ) ;
@@ -170,34 +174,39 @@ function bindTemplate(name, code) {
170
174
t . appendChild ( document . createElement ( "br" ) ) ;
171
175
}
172
176
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
+
173
187
jQuery ( document ) . ready ( function ( ) {
174
188
//Hook placeholders
175
189
$ ( 'textarea' ) . focus ( function ( ) {
176
190
if ( $ ( this ) . val ( ) === pVal ( $ ( this ) ) ) {
177
191
$ ( this ) . val ( '' ) ;
178
- $ ( this ) . css ( 'color' , 'black' ) ;
179
192
}
193
+ $ ( this ) . css ( 'color' , 'black' ) ;
180
194
} ) ;
181
195
$ ( 'textarea' ) . blur ( function ( ) {
182
- if ( $ ( this ) . val ( ) === '' ) {
196
+ if ( $ ( this ) . val ( ) === '' ) {
183
197
$ ( this ) . val ( pVal ( $ ( this ) ) ) ;
184
198
$ ( this ) . css ( 'color' , 'grey' ) ;
185
199
}
186
200
} ) ;
187
201
188
202
//Allows resetting of all settings
203
+ let clearModal = $ ( "#clear_modal" ) ;
189
204
$ ( "#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 ( ) ;
198
207
} ) ;
199
208
let hideModal = function ( ) {
200
- $ ( "#clear_modal" ) . hide ( ) ;
209
+ clearModal . hide ( ) ;
201
210
} ;
202
211
$ ( "#clear_modal .close" ) . click ( hideModal ) ;
203
212
$ ( "#clear_no_btn" ) . click ( hideModal ) ;
@@ -239,4 +248,61 @@ jQuery(document).ready(function () {
239
248
240
249
//Load initial settings
241
250
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
+
242
270
} ) ;
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