@@ -9,6 +9,97 @@ import Editor from 'components/Editor'
99import Store from 'stores'
1010
1111const store = new Store ( )
12+ const storage = {
13+ data : {
14+ private : { } ,
15+ global : { }
16+ } ,
17+ MODE : {
18+ private : 1 ,
19+ global : 2
20+ } ,
21+ setMode : function ( mode ) {
22+ if ( mode === this . MODE . private ) {
23+ this . key = popup . key + '-' + popup . protocol + '//' + popup . host
24+ this . mode = this . MODE . private
25+ }
26+
27+ if ( mode === this . MODE . global ) {
28+ this . key = popup . key
29+ this . mode = this . MODE . global
30+ }
31+ } ,
32+ load : function ( ) {
33+ this . setMode ( this . MODE . private )
34+ this . _setData ( JSON . parse ( window . localStorage . getItem ( this . key ) || '{}' ) )
35+
36+ this . setMode ( this . MODE . global )
37+ this . _setData ( JSON . parse ( window . localStorage . getItem ( this . key ) || '{}' ) )
38+ } ,
39+ _getData : function ( key ) {
40+ if ( this . mode === this . MODE . private ) {
41+ if ( key ) {
42+ return this . data . private [ key ]
43+ } else {
44+ return this . data . private
45+ }
46+ }
47+ if ( this . mode === this . MODE . global ) {
48+ if ( key ) {
49+ return this . data . global [ key ]
50+ } else {
51+ return this . data . global
52+ }
53+ }
54+ } ,
55+ _setData : function ( data , key ) {
56+ if ( this . mode === this . MODE . private ) {
57+ if ( key ) {
58+ this . data . private [ key ] = data
59+ } else {
60+ this . data . private = data
61+ }
62+ }
63+ if ( this . mode === this . MODE . global ) {
64+ if ( key ) {
65+ this . data . global [ key ] = data
66+ } else {
67+ this . data . global = data
68+ }
69+ }
70+ } ,
71+ get : function ( key ) {
72+ return this . _getData ( key )
73+ } ,
74+ set : function ( arg1 , arg2 ) {
75+ // arg1 is a key
76+ if ( typeof arg1 === 'string' ) {
77+ this . _setData ( arg2 , arg1 )
78+ } else {
79+ // arg1 is data
80+ this . _setData ( arg1 )
81+ }
82+
83+ var str = JSON . stringify ( this . _getData ( ) || { } )
84+ window . localStorage . setItem ( this . key , str )
85+ } ,
86+ remove : function ( key ) {
87+ if ( key ) {
88+ var data = this . _getData ( )
89+ delete data [ key ]
90+
91+ if ( $ . isEmptyObject ( data ) ) {
92+ this . remove ( )
93+ } else {
94+ var str = JSON . stringify ( this . _getData ( ) )
95+ window . localStorage . setItem ( this . key , str )
96+ }
97+ } else {
98+ window . localStorage . removeItem ( this . key )
99+ this . _setData ( { } )
100+ }
101+ }
102+ }
12103
13104const popup = {
14105 key : 'popup' ,
@@ -31,27 +122,9 @@ const popup = {
31122 donateForm : $ ( '#donate-form' )
32123 } ,
33124 title : {
34- host : {
35- select : 'List of websites modified by Your custom js' ,
36- goTo : 'Jump to the selected host'
37- } ,
38- share : 'Share Your script with other people' ,
39- save : 'Save and apply this script' ,
40125 include : {
41- textarea : 'Uncomment address of script below or type your own (one per line)' ,
42- mask : 'Click to close textarea popup'
43- } ,
44- draft : 'This is a draft, click to remove it'
45- } ,
46- applyTitles : function ( ) {
47- this . el . hostSelect . attr ( 'title' , this . title . host . select )
48- this . el . hostGoToLink . attr ( 'title' , this . title . host . goTo )
49-
50- this . el . includeTextarea . attr ( 'title' , this . title . include . textarea )
51- this . el . includeMask . attr ( 'title' , this . title . include . mask )
52-
53- this . el . saveBtn . attr ( 'title' , this . title . save )
54- this . el . draftRemoveLink . attr ( 'title' , this . title . draft )
126+ textarea : 'Uncomment address of script below or type your own (one per line)'
127+ }
55128 } ,
56129 include : {
57130 predefined : [
@@ -78,99 +151,6 @@ const popup = {
78151 source : ''
79152 } ,
80153 data : null ,
81- storage : {
82- data : {
83- private : { } ,
84- global : { }
85- } ,
86- MODE : {
87- private : 1 ,
88- global : 2
89- } ,
90- setMode : function ( mode ) {
91- if ( mode === this . MODE . private ) {
92- this . key = popup . key + '-' + popup . protocol + '//' + popup . host
93- this . mode = this . MODE . private
94- }
95-
96- if ( mode === this . MODE . global ) {
97- this . key = popup . key
98- this . mode = this . MODE . global
99- }
100- } ,
101- load : function ( ) {
102- this . setMode ( this . MODE . private )
103- this . _setData ( JSON . parse ( window . localStorage . getItem ( this . key ) || '{}' ) )
104-
105- this . setMode ( this . MODE . global )
106- this . _setData ( JSON . parse ( window . localStorage . getItem ( this . key ) || '{}' ) )
107- } ,
108- _getData : function ( key ) {
109- const storage = popup . storage
110- if ( storage . mode === storage . MODE . private ) {
111- if ( key ) {
112- return storage . data . private [ key ]
113- } else {
114- return storage . data . private
115- }
116- }
117- if ( storage . mode === storage . MODE . global ) {
118- if ( key ) {
119- return storage . data . global [ key ]
120- } else {
121- return storage . data . global
122- }
123- }
124- } ,
125- _setData : function ( data , key ) {
126- var storage = popup . storage
127- if ( storage . mode === storage . MODE . private ) {
128- if ( key ) {
129- storage . data . private [ key ] = data
130- } else {
131- storage . data . private = data
132- }
133- }
134- if ( storage . mode === storage . MODE . global ) {
135- if ( key ) {
136- storage . data . global [ key ] = data
137- } else {
138- storage . data . global = data
139- }
140- }
141- } ,
142- get : function ( key ) {
143- return this . _getData ( key )
144- } ,
145- set : function ( arg1 , arg2 ) {
146- // arg1 is a key
147- if ( typeof arg1 === 'string' ) {
148- this . _setData ( arg2 , arg1 )
149- } else {
150- // arg1 is data
151- this . _setData ( arg1 )
152- }
153-
154- var str = JSON . stringify ( this . _getData ( ) || { } )
155- window . localStorage . setItem ( this . key , str )
156- } ,
157- remove : function ( key ) {
158- if ( key ) {
159- var data = this . _getData ( )
160- delete data [ key ]
161-
162- if ( $ . isEmptyObject ( data ) ) {
163- this . remove ( )
164- } else {
165- var str = JSON . stringify ( this . _getData ( ) )
166- window . localStorage . setItem ( this . key , str )
167- }
168- } else {
169- window . localStorage . removeItem ( this . key )
170- this . _setData ( { } )
171- }
172- }
173- } ,
174154 apiclb : {
175155 onSelectedTab : function ( tab ) {
176156 popup . tabId = tab . id
@@ -193,12 +173,12 @@ const popup = {
193173 popup . protocol = response . protocol
194174
195175 // Load storage (global, local) IMPORTANT: Must be called first of all storage operations
196- popup . storage . load ( )
176+ storage . load ( )
197177
198178 // Set storage to store data accessible from all hosts
199- popup . storage . setMode ( popup . storage . MODE . global )
179+ storage . setMode ( storage . MODE . global )
200180
201- const hosts = popup . storage . get ( 'hosts' ) || [ ]
181+ const hosts = storage . get ( 'hosts' ) || [ ]
202182 const url = popup . protocol + '//' + response . host
203183
204184 // Add current host to list
@@ -217,7 +197,7 @@ const popup = {
217197
218198 // Store host (current included in array) if is customjs defined
219199 if ( response . customjs ) {
220- popup . storage . set ( 'hosts' , hosts )
200+ storage . set ( 'hosts' , hosts )
221201 }
222202
223203 /**
@@ -242,15 +222,15 @@ const popup = {
242222 }
243223
244224 // Set storage to store data accessible ONLY from current host
245- popup . storage . setMode ( popup . storage . MODE . private )
225+ storage . setMode ( storage . MODE . private )
246226
247227 // Save local copy of live data
248228 if ( response . customjs ) {
249- popup . storage . set ( 'data' , popup . data )
229+ storage . set ( 'data' , popup . data )
250230 }
251231
252232 // Apply data (draft if exist)
253- popup . applyData ( popup . storage . get ( 'draft' ) )
233+ popup . applyData ( storage . get ( 'draft' ) )
254234 }
255235 } ,
256236 generateScriptDataUrl : function ( script ) {
@@ -311,8 +291,8 @@ const popup = {
311291 }
312292 } ,
313293 removeDraft : function ( ) {
314- popup . storage . setMode ( popup . storage . MODE . private )
315- popup . storage . remove ( 'draft' )
294+ storage . setMode ( storage . MODE . private )
295+ storage . remove ( 'draft' )
316296
317297 popup . applyData ( )
318298 popup . el . draftRemoveLink . addClass ( 'is-hidden' )
@@ -335,8 +315,8 @@ const popup = {
335315 chrome . runtime . sendMessage ( { method : 'setData' , customjs : data , reload : true } )
336316
337317 // Save local copy of data
338- popup . storage . setMode ( popup . storage . MODE . private )
339- popup . storage . set ( 'data' , popup . data )
318+ storage . setMode ( storage . MODE . private )
319+ storage . set ( 'data' , popup . data )
340320
341321 // Clear draft
342322 popup . removeDraft ( )
@@ -357,19 +337,19 @@ const popup = {
357337 // TODO: confirm doesn't work with popup window
358338 if ( window . confirm ( 'Do you really want all away?' ) ) {
359339 // Remove stored data for current host
360- popup . storage . setMode ( popup . storage . MODE . private )
361- popup . storage . remove ( )
340+ storage . setMode ( storage . MODE . private )
341+ storage . remove ( )
362342
363343 // Remove host from hosts inside global storage
364- popup . storage . setMode ( popup . storage . MODE . global )
365- const oldHosts = popup . storage . get ( 'hosts' )
344+ storage . setMode ( storage . MODE . global )
345+ const oldHosts = storage . get ( 'hosts' )
366346 const newHosts = [ ]
367347 oldHosts . forEach ( function ( host ) {
368348 if ( host !== popup . protocol + '//' + popup . host ) {
369349 newHosts . push ( host )
370350 }
371351 } )
372- popup . storage . set ( 'hosts' , newHosts )
352+ storage . set ( 'hosts' , newHosts )
373353
374354 // Remove customjs from frontend
375355 chrome . runtime . sendMessage ( { method : 'removeData' } )
@@ -391,12 +371,6 @@ const popup = {
391371
392372window . popup = popup
393373
394- /**
395- * Add titles to elements
396- */
397-
398- popup . applyTitles ( )
399-
400374/**
401375* Click to goTo host link
402376*/
@@ -456,8 +430,8 @@ const draftAutoSave = function () {
456430 const source = draft . source
457431
458432 if ( ( source || ! popup . data . source ) && source !== popup . data . source ) {
459- popup . storage . setMode ( popup . storage . MODE . private )
460- popup . storage . set ( 'draft' , draft )
433+ storage . setMode ( storage . MODE . private )
434+ storage . set ( 'draft' , draft )
461435
462436 // Auto switch 'enable checkbox' on source edit
463437 if ( ! popup . el . enableCheck . hasClass ( 'not-auto-change' ) ) {
@@ -504,7 +478,7 @@ popup.el.hostSelect.on('change', function (e) {
504478 // Show controls
505479 popup . el . saveBtn . removeClass ( 'pure-button-disabled' )
506480 popup . el . resetBtn . removeClass ( 'pure-button-disabled' )
507- if ( popup . storage . get ( 'draft' ) ) {
481+ if ( storage . get ( 'draft' ) ) {
508482 popup . el . draftRemoveLink . removeClass ( 'is-hidden' )
509483 }
510484
0 commit comments