@@ -7,6 +7,7 @@ import { render } from 'react-dom'
77import { Provider } from 'mobx-react'
88import Editor from 'components/Editor'
99import Store from 'stores'
10+ import { encodeSource , decodeSource } from 'libs'
1011
1112const store = new Store ( )
1213const storage = {
@@ -72,7 +73,6 @@ const storage = {
7273 throw new Error ( 'This should never happen!' )
7374 // this._setData(arg1)
7475 }
75-
7676 var str = JSON . stringify ( this . _getData ( ) || { } )
7777 window . localStorage . setItem ( this . key , str )
7878 } ,
@@ -144,99 +144,76 @@ const popup = {
144144 source : ''
145145 } ,
146146 data : null ,
147- apiclb : {
148- onSelectedTab : function ( tab ) {
149- popup . tabId = tab . id
150- chrome . runtime . sendMessage (
151- { method : 'getData' } ,
152- popup . apiclb . onGetData
153- )
154- } ,
155- onGetData : function ( response ) {
156- if ( ! response || typeof response . host !== 'string' ) {
157- popup . error ( )
158- return
159- }
147+ init : function ( ) {
148+ chrome . runtime . sendMessage (
149+ { method : 'getData' } ,
150+ popup . onGetData
151+ )
152+ } ,
153+ onGetData : function ( response ) {
154+ if ( ! response || typeof response . host !== 'string' ) {
155+ popup . error ( )
156+ return
157+ }
160158
161159 /**
162160 * Create 'hosts select'
163161 */
164162
165- popup . host = response . host
166- popup . protocol = response . protocol
163+ popup . host = response . host
164+ popup . protocol = response . protocol
167165
168166 // Load storage (global, local) IMPORTANT: Must be called first of all storage operations
169- storage . load ( )
167+ storage . load ( )
170168
171169 // Set storage to store data accessible from all hosts
172- storage . setMode ( storage . MODE . global )
170+ storage . setMode ( storage . MODE . global )
173171
174- const hosts = storage . get ( 'hosts' ) || [ ]
175- const url = popup . protocol + '//' + response . host
172+ const hosts = storage . get ( 'hosts' ) || [ ]
173+ const url = popup . protocol + '//' + response . host
176174
177175 // Add current host to list
178- if ( hosts . indexOf ( url ) === - 1 ) {
179- hosts . push ( url )
180- }
176+ if ( hosts . indexOf ( url ) === - 1 ) {
177+ hosts . push ( url )
178+ }
181179
182180 // Fill 'hosts select'
183- hosts . forEach ( function ( host ) {
184- var option = $ ( '<option>' + host + '</option>' )
185- if ( host === url ) {
186- option . attr ( 'selected' , 'selected' )
187- }
188- popup . el . hostSelect . append ( option )
189- } )
190-
191- // Store host (current included in array) if is customjs defined
192- if ( response . customjs ) {
193- storage . set ( 'hosts' , hosts )
181+ hosts . forEach ( function ( host ) {
182+ var option = $ ( '<option>' + host + '</option>' )
183+ if ( host === url ) {
184+ option . attr ( 'selected' , 'selected' )
194185 }
186+ popup . el . hostSelect . append ( option )
187+ } )
195188
196- /**
197- * Set-up data (script, enable, include, extra)
198- */
199-
200- // Set-up data pattern if empty
201- if ( ! popup . data ) {
202- popup . data = $ . extend ( true , { } , popup . emptyDataPattern )
203- }
189+ // Store host (current included in array) if is customjs defined
190+ if ( response . customjs ) {
191+ storage . set ( 'hosts' , hosts )
192+ }
204193
205- // Merge host's data to defaults
206- popup . data = $ . extend ( popup . data , response . customjs )
194+ /**
195+ * Set-up data (script, enable, include, extra)
196+ */
197+ // Set-up data pattern if empty
198+ if ( ! popup . data ) {
199+ popup . data = $ . extend ( true , { } , popup . emptyDataPattern )
200+ }
207201
208- // ... source is now encoded as base64
209- if ( popup . data . source . indexOf ( 'data:text/javascript;base64,' ) === 0 ) {
210- popup . data . source = popup . data . source . replace ( 'data:text/javascript;base64,' , '' )
211- popup . data . source = window . atob ( popup . data . source )
212- } else if ( popup . data . source . indexOf ( 'data:text/javascript;charset=utf-8,' ) === 0 ) {
213- popup . data . source = popup . data . source . replace ( 'data:text/javascript;charset=utf-8,' , '' )
214- popup . data . source = decodeURIComponent ( popup . data . source )
215- }
202+ // Merge host's data to defaults
203+ popup . data = $ . extend ( popup . data , response . customjs )
216204
217- // Set storage to store data accessible ONLY from current host
218- storage . setMode ( storage . MODE . private )
205+ popup . data . source = decodeSource ( popup . data . source )
219206
220- // Save local copy of live data
221- if ( response . customjs ) {
222- storage . set ( 'data' , popup . data )
223- }
207+ // Set storage to store data accessible ONLY from current host
208+ storage . setMode ( storage . MODE . private )
224209
225- // Apply data (draft if exist)
226- popup . applyData ( storage . get ( 'draft' ) )
227- }
228- } ,
229- generateScriptDataUrl : function ( script ) {
230- var b64 = 'data:text/javascript'
231- // base64 may be smaller, but does not handle unicode characters
232- // attempt base64 first, fall back to escaped text
233- try {
234- b64 += ( ';base64,' + window . btoa ( script ) )
235- } catch ( e ) {
236- b64 += ( ';charset=utf-8,' + encodeURIComponent ( script ) )
210+ // Save local copy of live data
211+ if ( response . customjs ) {
212+ storage . set ( 'data' , popup . data )
237213 }
238214
239- return b64
215+ // Apply data (draft if exist)
216+ popup . applyData ( storage . get ( 'draft' ) )
240217 } ,
241218 applyData : function ( data , notDraft ) {
242219 if ( data && ! notDraft ) {
@@ -302,7 +279,7 @@ const popup = {
302279
303280 // Transform source for correct apply
304281 data . config . extra = data . config . extra . replace ( '\n' , ';' )
305- data . source = popup . generateScriptDataUrl ( data . source )
282+ data . source = encodeSource ( data . source )
306283
307284 // Send new data to apply
308285 chrome . runtime . sendMessage ( { method : 'setData' , customjs : data , reload : true } )
@@ -316,8 +293,6 @@ const popup = {
316293
317294 // Close popup
318295 window . close ( )
319-
320- return false
321296 } ,
322297 reset : function ( e ) {
323298 e . preventDefault ( )
@@ -328,31 +303,27 @@ const popup = {
328303 }
329304
330305 // TODO: confirm doesn't work with popup window
331- if ( window . confirm ( 'Do you really want all away?' ) ) {
332- // Remove stored data for current host
333- storage . setMode ( storage . MODE . private )
334- storage . remove ( )
335-
336- // Remove host from hosts inside global storage
337- storage . setMode ( storage . MODE . global )
338- const oldHosts = storage . get ( 'hosts' )
339- const newHosts = [ ]
340- oldHosts . forEach ( function ( host ) {
341- if ( host !== popup . protocol + '//' + popup . host ) {
342- newHosts . push ( host )
343- }
344- } )
345- storage . set ( 'hosts' , newHosts )
306+ // if (window.confirm('Do you really want all away?')) {
307+ // Remove stored data for current host
308+ storage . setMode ( storage . MODE . private )
309+ storage . remove ( )
346310
347- // Remove customjs from frontend
348- chrome . runtime . sendMessage ( { method : 'removeData' } )
311+ // Remove host from hosts inside global storage
312+ storage . setMode ( storage . MODE . global )
313+ const oldHosts = storage . get ( 'hosts' )
314+ const newHosts = oldHosts . filter ( ( host ) => host !== `${ popup . protocol } //${ popup . host } ` )
315+ storage . set ( 'hosts' , newHosts )
349316
350- // Set-up empty data
351- popup . data = $ . extend ( true , { } , popup . emptyDataPattern )
352- popup . applyData ( )
317+ // Remove customjs from frontend
318+ chrome . runtime . sendMessage ( { method : 'removeData' } )
353319
354- popup . removeDraft ( )
355- }
320+ // Set-up empty data
321+ popup . data = $ . extend ( true , { } , popup . emptyDataPattern )
322+ popup . applyData ( )
323+
324+ popup . removeDraft ( )
325+ store . EditorStore . setDefaultValue ( )
326+ // }
356327
357328 return false
358329 } ,
@@ -396,11 +367,7 @@ const initEditor = () => {
396367}
397368initEditor ( )
398369
399- /**
400- * Connect front end (load info about current site)
401- */
402-
403- chrome . tabs . getSelected ( null , popup . apiclb . onSelectedTab )
370+ popup . init ( )
404371
405372/**
406373* 'Include extra scripts' control
0 commit comments