Skip to content
This repository was archived by the owner on Apr 30, 2026. It is now read-only.

Commit be09379

Browse files
committed
Refactor
1 parent bdd0c09 commit be09379

3 files changed

Lines changed: 99 additions & 102 deletions

File tree

src/js/libs/index.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,27 @@ export const getActiveTab = async () => {
2424
}
2525
return tabs[0]
2626
}
27+
28+
const SOURCE_PREFIX = 'data:text/javascript'
29+
const BASE64_PREFIX = SOURCE_PREFIX + ';base64,'
30+
const UTF8_PREFIX = SOURCE_PREFIX + ';charset=utf-8,'
31+
32+
export const encodeSource = (script) => {
33+
// base64 may be smaller, but does not handle unicode characters
34+
// attempt base64 first, fall back to escaped text
35+
try {
36+
return (BASE64_PREFIX + window.btoa(script))
37+
} catch (e) {
38+
return (UTF8_PREFIX + encodeURIComponent(script))
39+
}
40+
}
41+
42+
export const decodeSource = (source) => {
43+
if (source.startsWith(BASE64_PREFIX)) {
44+
return window.atob(source.replace(BASE64_PREFIX, ''))
45+
}
46+
if (source.startsWith(UTF8_PREFIX)) {
47+
return decodeURIComponent(source.replace(UTF8_PREFIX, ''))
48+
}
49+
throw new Error(`Unrecognized source format: ${source}`)
50+
}

src/js/popup.js

Lines changed: 68 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { render } from 'react-dom'
77
import { Provider } from 'mobx-react'
88
import Editor from 'components/Editor'
99
import Store from 'stores'
10+
import { encodeSource, decodeSource } from 'libs'
1011

1112
const store = new Store()
1213
const 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
}
397368
initEditor()
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

src/js/stores/EditorStore.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@ import { action, observable } from 'mobx'
33
export default class TabStore {
44
constructor (store) {
55
this.store = store
6+
this.setDefaultValue()
67
}
78

8-
@observable value = '// Here You can type your custom JavaScript...'
9+
@observable value = ''
10+
11+
@action
12+
setDefaultValue = () => {
13+
this.value = '// Here You can type your custom JavaScript...'
14+
}
915

1016
@action
1117
setValue = (value) => {

0 commit comments

Comments
 (0)