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

Commit 18b059a

Browse files
committed
Refactor popup.js
1 parent 9e06a5e commit 18b059a

1 file changed

Lines changed: 54 additions & 96 deletions

File tree

src/js/popup.js

Lines changed: 54 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -11,86 +11,54 @@ import { encodeSource, decodeSource } from 'libs'
1111

1212
const store = new Store()
1313
const storage = {
14-
data: {
15-
private: {},
16-
global: {}
14+
hosts: [],
15+
data: '',
16+
draft: '',
17+
setDraft: function (draft = '') {
18+
this.draft = draft
19+
this.saveToLocalStorage()
1720
},
18-
MODE: {
19-
private: 1,
20-
global: 2
21+
getDraft: function () {
22+
return this.draft
2123
},
22-
setMode: function (mode) {
23-
if (mode === this.MODE.private) {
24-
this.key = popup.key + '-' + popup.protocol + '//' + popup.host
25-
this.mode = this.MODE.private
26-
}
27-
28-
if (mode === this.MODE.global) {
29-
this.key = popup.key
30-
this.mode = this.MODE.global
31-
}
24+
removeDraft: function () {
25+
this.draft = ''
26+
this.saveToLocalStorage()
3227
},
33-
load: function () {
34-
this.setMode(this.MODE.private)
35-
this._setData(JSON.parse(window.localStorage.getItem(this.key) || '{}'))
36-
37-
this.setMode(this.MODE.global)
38-
this._setData(JSON.parse(window.localStorage.getItem(this.key) || '{}'))
28+
setData: function (data = '') {
29+
this.data = data
30+
this.saveToLocalStorage()
3931
},
40-
_getData: function (key) {
41-
const object = this.mode === this.MODE.private ? this.data.private : this.data.global
42-
if (key) {
43-
return object[key]
44-
} else {
45-
return object
46-
}
32+
getData: function () {
33+
return this.data
4734
},
48-
_setData: function (data, key) {
49-
if (this.mode === this.MODE.private) {
50-
if (key) {
51-
this.data.private[key] = data
52-
} else {
53-
this.data.private = data
54-
}
55-
}
56-
if (this.mode === this.MODE.global) {
57-
if (key) {
58-
this.data.global[key] = data
59-
} else {
60-
this.data.global = data
61-
}
62-
}
35+
getHosts: function () {
36+
return this.hosts
6337
},
64-
get: function (key) {
65-
return this._getData(key)
38+
setHosts: function (hosts = []) {
39+
this.hosts = hosts
40+
const hostsStr = JSON.stringify({ hosts: this.hosts })
41+
window.localStorage.setItem(popup.key, hostsStr)
6642
},
67-
set: function (arg1, arg2) {
68-
// arg1 is a key
69-
if (typeof arg1 === 'string') {
70-
this._setData(arg2, arg1)
71-
} else {
72-
// arg1 is data
73-
throw new Error('This should never happen!')
74-
// this._setData(arg1)
75-
}
76-
var str = JSON.stringify(this._getData() || {})
77-
window.localStorage.setItem(this.key, str)
43+
saveToLocalStorage: function () {
44+
const key = popup.key + '-' + popup.protocol + '//' + popup.host
45+
const str = JSON.stringify({
46+
data: this.data,
47+
draft: this.draft
48+
})
49+
window.localStorage.setItem(key, str)
7850
},
79-
remove: function (key) {
80-
if (key) {
81-
var data = this._getData()
82-
delete data[key]
83-
84-
if ($.isEmptyObject(data)) {
85-
this.remove()
86-
} else {
87-
var str = JSON.stringify(this._getData())
88-
window.localStorage.setItem(this.key, str)
89-
}
90-
} else {
91-
window.localStorage.removeItem(this.key)
92-
this._setData({})
93-
}
51+
load: function () {
52+
const key = popup.key + '-' + popup.protocol + '//' + popup.host
53+
const { data, draft } = JSON.parse(window.localStorage.getItem(key) || '{}')
54+
this.data = data
55+
this.draft = draft
56+
const { hosts } = JSON.parse(window.localStorage.getItem(popup.key) || '{}')
57+
this.hosts = hosts
58+
},
59+
remove: function () {
60+
const key = popup.key + '-' + popup.protocol + '//' + popup.host
61+
window.localStorage.removeItem(key)
9462
}
9563
}
9664

@@ -163,16 +131,13 @@ const popup = {
163131
popup.host = response.host
164132
popup.protocol = response.protocol
165133

166-
// Load storage (global, local) IMPORTANT: Must be called first of all storage operations
134+
// Load storage (global, local) IMPORTANT: Must be called first of all storage operations
167135
storage.load()
168136

169-
// Set storage to store data accessible from all hosts
170-
storage.setMode(storage.MODE.global)
171-
172-
const hosts = storage.get('hosts') || []
137+
const hosts = storage.getHosts()
173138
const url = popup.protocol + '//' + response.host
174139

175-
// Add current host to list
140+
// Add current host to list
176141
if (hosts.indexOf(url) === -1) {
177142
hosts.push(url)
178143
}
@@ -188,7 +153,7 @@ const popup = {
188153

189154
// Store host (current included in array) if is customjs defined
190155
if (response.customjs) {
191-
storage.set('hosts', hosts)
156+
storage.setHosts(hosts)
192157
}
193158

194159
/**
@@ -204,16 +169,13 @@ const popup = {
204169

205170
popup.data.source = decodeSource(popup.data.source)
206171

207-
// Set storage to store data accessible ONLY from current host
208-
storage.setMode(storage.MODE.private)
209-
210172
// Save local copy of live data
211173
if (response.customjs) {
212-
storage.set('data', popup.data)
174+
storage.setData(popup.data)
213175
}
214176

215177
// Apply data (draft if exist)
216-
popup.applyData(storage.get('draft'))
178+
popup.applyData(storage.getDraft())
217179
},
218180
applyData: function (data, notDraft) {
219181
if (data && !notDraft) {
@@ -261,8 +223,7 @@ const popup = {
261223
}
262224
},
263225
removeDraft: function () {
264-
storage.setMode(storage.MODE.private)
265-
storage.remove('draft')
226+
storage.removeDraft()
266227

267228
popup.applyData()
268229
popup.el.draftRemoveLink.addClass('is-hidden')
@@ -285,8 +246,7 @@ const popup = {
285246
chrome.runtime.sendMessage({ method: 'setData', customjs: data, reload: true })
286247

287248
// Save local copy of data
288-
storage.setMode(storage.MODE.private)
289-
storage.set('data', popup.data)
249+
storage.setData(popup.data)
290250

291251
// Clear draft
292252
popup.removeDraft()
@@ -305,14 +265,12 @@ const popup = {
305265
// TODO: confirm doesn't work with popup window
306266
// if (window.confirm('Do you really want all away?')) {
307267
// Remove stored data for current host
308-
storage.setMode(storage.MODE.private)
309268
storage.remove()
310269

311270
// Remove host from hosts inside global storage
312-
storage.setMode(storage.MODE.global)
313-
const oldHosts = storage.get('hosts')
271+
const oldHosts = storage.getHosts()
314272
const newHosts = oldHosts.filter((host) => host !== `${popup.protocol}//${popup.host}`)
315-
storage.set('hosts', newHosts)
273+
storage.setHosts(newHosts)
316274

317275
// Remove customjs from frontend
318276
chrome.runtime.sendMessage({ method: 'removeData' })
@@ -390,8 +348,7 @@ const draftAutoSave = function () {
390348
const source = draft.source
391349

392350
if ((source || !popup.data.source) && source !== popup.data.source) {
393-
storage.setMode(storage.MODE.private)
394-
storage.set('draft', draft)
351+
storage.setDraft(draft)
395352

396353
// Auto switch 'enable checkbox' on source edit
397354
if (!popup.el.enableCheck.hasClass('not-auto-change')) {
@@ -410,6 +367,7 @@ popup.el.hostSelect.on('change', function (e) {
410367
const hostData = JSON.parse(window.localStorage.getItem(popup.key + '-' + host), true)
411368

412369
if (host !== popup.protocol + '//' + popup.host) {
370+
// storage.load()
413371
// Stop making drafts
414372
clearInterval(draftAutoSaveInterval)
415373

@@ -438,7 +396,7 @@ popup.el.hostSelect.on('change', function (e) {
438396
// Show controls
439397
popup.el.saveBtn.removeClass('pure-button-disabled')
440398
popup.el.resetBtn.removeClass('pure-button-disabled')
441-
if (storage.get('draft')) {
399+
if (storage.getDraft()) {
442400
popup.el.draftRemoveLink.removeClass('is-hidden')
443401
}
444402

@@ -457,7 +415,7 @@ popup.el.enableCheck.on('click', function () {
457415
/**
458416
* Save script
459417
*/
460-
popup.el.saveBtn.on('click', popup.save)
418+
popup.el.saveBtn.on('click', popup.save.bind(popup))
461419

462420
/**
463421
* Reset script

0 commit comments

Comments
 (0)