Skip to content

Commit a6aa1fc

Browse files
committed
Import & export
1 parent cfc0e84 commit a6aa1fc

4 files changed

Lines changed: 230 additions & 42 deletions

File tree

background.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
chrome.runtime.onMessage.addListener(function(request, sender) {
2+
if (request.name === 'download') {
3+
chrome.permissions.request({
4+
permissions: ['downloads']
5+
}, function(granted) {
6+
if (granted) {
7+
try {
8+
var blob = new Blob([request.value], {
9+
type: 'application/json;charset=utf-8'
10+
});
11+
12+
chrome.downloads.download({
13+
url: URL.createObjectURL(blob),
14+
filename: request.filename,
15+
saveAs: true
16+
});
17+
} catch (err) {
18+
chrome.runtime.sendMessage({
19+
name: 'satus-error',
20+
value: err
21+
});
22+
}
23+
} else {
24+
chrome.runtime.sendMessage({
25+
name: 'satus-error',
26+
value: 'Permission is not granted'
27+
});
28+
}
29+
});
30+
}
31+
});

manifest.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111
"browser_action": {
1212
"default_popup": "index.html"
1313
},
14+
"background": {
15+
"persistent": false,
16+
"scripts": [
17+
"background.js"
18+
]
19+
},
1420
"optional_permissions": [
1521
"downloads"
1622
],

popup.js

Lines changed: 108 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,25 @@
11

2+
function change(old_index, new_index) {
3+
var main = document.querySelector('.satus-main'),
4+
data = JSON.parse(Satus.storage.get('data') || '{}'),
5+
data2 = data,
6+
clone;
7+
8+
if (main.history.length > 1) {
9+
data = data[main.history[main.history.length - 1].storage_key];
10+
}
11+
12+
old_index2 = Number(document.querySelectorAll('.satus-main .satus-list li')[old_index].querySelector('*').dataset.key);
13+
new_index2 = Number(document.querySelectorAll('.satus-main .satus-list li')[new_index].querySelector('*').dataset.key);
14+
15+
clone = Object.assign(data[old_index2]);
16+
17+
data[old_index2] = data[new_index2];
18+
data[new_index2] = clone;
19+
20+
Satus.storage.set('data', JSON.stringify(data2));
21+
}
22+
223
function update(container) {
324
var item = this.history[this.history.length - 1],
425
id = item.appearanceKey,
@@ -8,26 +29,7 @@ function update(container) {
829
type: 'list',
930
compact: true,
1031
sortable: true,
11-
onchange: function(old_index, new_index) {
12-
var main = document.querySelector('.satus-main'),
13-
data = JSON.parse(Satus.storage.get('data') || '{}'),
14-
data2 = data,
15-
clone;
16-
17-
if (main.history.length > 1) {
18-
data = data[main.history[main.history.length - 1].storage_key];
19-
}
20-
21-
old_index2 = Number(document.querySelectorAll('.satus-main .satus-list li')[old_index].querySelector('*').dataset.key);
22-
new_index2 = Number(document.querySelectorAll('.satus-main .satus-list li')[new_index].querySelector('*').dataset.key);
23-
24-
clone = Object.assign(data[old_index2]);
25-
26-
data[old_index2] = data[new_index2];
27-
data[new_index2] = clone;
28-
29-
Satus.storage.set('data', JSON.stringify(data2));
30-
}
32+
onchange: change
3133
}
3234
};
3335

@@ -44,26 +46,7 @@ function update(container) {
4446
type: 'list',
4547
compact: true,
4648
sortable: true,
47-
onchange: function(old_index, new_index) {
48-
var main = document.querySelector('.satus-main'),
49-
data = JSON.parse(Satus.storage.get('data') || '{}'),
50-
data2 = data,
51-
clone;
52-
53-
if (main.history.length > 1) {
54-
data = data[main.history[main.history.length - 1].storage_key];
55-
}
56-
57-
old_index2 = Number(document.querySelectorAll('.satus-main .satus-list li')[old_index].querySelector('*').dataset.key);
58-
new_index2 = Number(document.querySelectorAll('.satus-main .satus-list li')[new_index].querySelector('*').dataset.key);
59-
60-
clone = Object.assign(data[old_index2]);
61-
62-
data[old_index2] = data[new_index2];
63-
data[new_index2] = clone;
64-
65-
Satus.storage.set('data', JSON.stringify(data2));
66-
},
49+
onchange: change,
6750

6851
0: {
6952
type: 'folder',
@@ -204,7 +187,91 @@ var Menu = {
204187
icon: '<svg fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" viewBox="0 0 24 24"><circle cx="12" cy="5.25" r="0.45"/><circle cx="12" cy="12" r="0.45"/><circle cx="12" cy="18.75" r="0.45"/></svg>',
205188
onClickRender: {
206189
type: 'dialog',
207-
class: 'satus-dialog--vertical-menu'
190+
class: 'satus-dialog--vertical-menu',
191+
192+
export: {
193+
type: 'button',
194+
label: 'Export',
195+
onclick: function() {
196+
chrome.runtime.sendMessage({
197+
name: 'download',
198+
filename: 'todo.json',
199+
value: Satus.storage.get('data')
200+
});
201+
}
202+
},
203+
import: {
204+
type: 'button',
205+
label: 'Import',
206+
onclick: function() {
207+
try {
208+
var input = document.createElement('input');
209+
210+
input.type = 'file';
211+
212+
input.addEventListener('change', function() {
213+
var file_reader = new FileReader();
214+
215+
file_reader.onload = function() {
216+
var data = JSON.parse(this.result);
217+
218+
for (var i in data) {
219+
Satus.storage.set(i, data[i]);
220+
}
221+
222+
Satus.render({
223+
type: 'dialog',
224+
225+
message: {
226+
type: 'text',
227+
label: 'successfullyImportedSettings',
228+
style: {
229+
'width': '100%',
230+
'opacity': '.8'
231+
}
232+
},
233+
section: {
234+
type: 'section',
235+
class: 'controls',
236+
style: {
237+
'justify-content': 'flex-end',
238+
'display': 'flex'
239+
},
240+
241+
cancel: {
242+
type: 'button',
243+
label: 'cancel',
244+
onclick: function() {
245+
var scrim = document.querySelectorAll('.satus-dialog__scrim');
246+
247+
scrim[scrim.length - 1].click();
248+
}
249+
},
250+
ok: {
251+
type: 'button',
252+
label: 'OK',
253+
onclick: function() {
254+
var scrim = document.querySelectorAll('.satus-dialog__scrim');
255+
256+
scrim[scrim.length - 1].click();
257+
}
258+
}
259+
}
260+
});
261+
};
262+
263+
file_reader.readAsText(this.files[0]);
264+
});
265+
266+
input.click();
267+
} catch (err) {
268+
chrome.runtime.sendMessage({
269+
name: 'dialog-error',
270+
value: err
271+
});
272+
}
273+
}
274+
}
208275
}
209276
}
210277
}

src/js/header.js

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,91 @@ var Menu = {
3333
icon: '<svg fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" viewBox="0 0 24 24"><circle cx="12" cy="5.25" r="0.45"/><circle cx="12" cy="12" r="0.45"/><circle cx="12" cy="18.75" r="0.45"/></svg>',
3434
onClickRender: {
3535
type: 'dialog',
36-
class: 'satus-dialog--vertical-menu'
36+
class: 'satus-dialog--vertical-menu',
37+
38+
export: {
39+
type: 'button',
40+
label: 'Export',
41+
onclick: function() {
42+
chrome.runtime.sendMessage({
43+
name: 'download',
44+
filename: 'todo.json',
45+
value: Satus.storage.get('data')
46+
});
47+
}
48+
},
49+
import: {
50+
type: 'button',
51+
label: 'Import',
52+
onclick: function() {
53+
try {
54+
var input = document.createElement('input');
55+
56+
input.type = 'file';
57+
58+
input.addEventListener('change', function() {
59+
var file_reader = new FileReader();
60+
61+
file_reader.onload = function() {
62+
var data = JSON.parse(this.result);
63+
64+
for (var i in data) {
65+
Satus.storage.set(i, data[i]);
66+
}
67+
68+
Satus.render({
69+
type: 'dialog',
70+
71+
message: {
72+
type: 'text',
73+
label: 'successfullyImportedSettings',
74+
style: {
75+
'width': '100%',
76+
'opacity': '.8'
77+
}
78+
},
79+
section: {
80+
type: 'section',
81+
class: 'controls',
82+
style: {
83+
'justify-content': 'flex-end',
84+
'display': 'flex'
85+
},
86+
87+
cancel: {
88+
type: 'button',
89+
label: 'cancel',
90+
onclick: function() {
91+
var scrim = document.querySelectorAll('.satus-dialog__scrim');
92+
93+
scrim[scrim.length - 1].click();
94+
}
95+
},
96+
ok: {
97+
type: 'button',
98+
label: 'OK',
99+
onclick: function() {
100+
var scrim = document.querySelectorAll('.satus-dialog__scrim');
101+
102+
scrim[scrim.length - 1].click();
103+
}
104+
}
105+
}
106+
});
107+
};
108+
109+
file_reader.readAsText(this.files[0]);
110+
});
111+
112+
input.click();
113+
} catch (err) {
114+
chrome.runtime.sendMessage({
115+
name: 'dialog-error',
116+
value: err
117+
});
118+
}
119+
}
120+
}
37121
}
38122
}
39123
}

0 commit comments

Comments
 (0)