Skip to content

Commit

Permalink
Import & export
Browse files Browse the repository at this point in the history
  • Loading branch information
adokseo committed Apr 26, 2020
1 parent cfc0e84 commit a6aa1fc
Show file tree
Hide file tree
Showing 4 changed files with 230 additions and 42 deletions.
31 changes: 31 additions & 0 deletions background.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
chrome.runtime.onMessage.addListener(function(request, sender) {
if (request.name === 'download') {
chrome.permissions.request({
permissions: ['downloads']
}, function(granted) {
if (granted) {
try {
var blob = new Blob([request.value], {
type: 'application/json;charset=utf-8'
});

chrome.downloads.download({
url: URL.createObjectURL(blob),
filename: request.filename,
saveAs: true
});
} catch (err) {
chrome.runtime.sendMessage({
name: 'satus-error',
value: err
});
}
} else {
chrome.runtime.sendMessage({
name: 'satus-error',
value: 'Permission is not granted'
});
}
});
}
});
6 changes: 6 additions & 0 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
"browser_action": {
"default_popup": "index.html"
},
"background": {
"persistent": false,
"scripts": [
"background.js"
]
},
"optional_permissions": [
"downloads"
],
Expand Down
149 changes: 108 additions & 41 deletions popup.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,25 @@

function change(old_index, new_index) {
var main = document.querySelector('.satus-main'),
data = JSON.parse(Satus.storage.get('data') || '{}'),
data2 = data,
clone;

if (main.history.length > 1) {
data = data[main.history[main.history.length - 1].storage_key];
}

old_index2 = Number(document.querySelectorAll('.satus-main .satus-list li')[old_index].querySelector('*').dataset.key);
new_index2 = Number(document.querySelectorAll('.satus-main .satus-list li')[new_index].querySelector('*').dataset.key);

clone = Object.assign(data[old_index2]);

data[old_index2] = data[new_index2];
data[new_index2] = clone;

Satus.storage.set('data', JSON.stringify(data2));
}

function update(container) {
var item = this.history[this.history.length - 1],
id = item.appearanceKey,
Expand All @@ -8,26 +29,7 @@ function update(container) {
type: 'list',
compact: true,
sortable: true,
onchange: function(old_index, new_index) {
var main = document.querySelector('.satus-main'),
data = JSON.parse(Satus.storage.get('data') || '{}'),
data2 = data,
clone;

if (main.history.length > 1) {
data = data[main.history[main.history.length - 1].storage_key];
}

old_index2 = Number(document.querySelectorAll('.satus-main .satus-list li')[old_index].querySelector('*').dataset.key);
new_index2 = Number(document.querySelectorAll('.satus-main .satus-list li')[new_index].querySelector('*').dataset.key);

clone = Object.assign(data[old_index2]);

data[old_index2] = data[new_index2];
data[new_index2] = clone;

Satus.storage.set('data', JSON.stringify(data2));
}
onchange: change
}
};

Expand All @@ -44,26 +46,7 @@ function update(container) {
type: 'list',
compact: true,
sortable: true,
onchange: function(old_index, new_index) {
var main = document.querySelector('.satus-main'),
data = JSON.parse(Satus.storage.get('data') || '{}'),
data2 = data,
clone;

if (main.history.length > 1) {
data = data[main.history[main.history.length - 1].storage_key];
}

old_index2 = Number(document.querySelectorAll('.satus-main .satus-list li')[old_index].querySelector('*').dataset.key);
new_index2 = Number(document.querySelectorAll('.satus-main .satus-list li')[new_index].querySelector('*').dataset.key);

clone = Object.assign(data[old_index2]);

data[old_index2] = data[new_index2];
data[new_index2] = clone;

Satus.storage.set('data', JSON.stringify(data2));
},
onchange: change,

0: {
type: 'folder',
Expand Down Expand Up @@ -204,7 +187,91 @@ var Menu = {
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>',
onClickRender: {
type: 'dialog',
class: 'satus-dialog--vertical-menu'
class: 'satus-dialog--vertical-menu',

export: {
type: 'button',
label: 'Export',
onclick: function() {
chrome.runtime.sendMessage({
name: 'download',
filename: 'todo.json',
value: Satus.storage.get('data')
});
}
},
import: {
type: 'button',
label: 'Import',
onclick: function() {
try {
var input = document.createElement('input');

input.type = 'file';

input.addEventListener('change', function() {
var file_reader = new FileReader();

file_reader.onload = function() {
var data = JSON.parse(this.result);

for (var i in data) {
Satus.storage.set(i, data[i]);
}

Satus.render({
type: 'dialog',

message: {
type: 'text',
label: 'successfullyImportedSettings',
style: {
'width': '100%',
'opacity': '.8'
}
},
section: {
type: 'section',
class: 'controls',
style: {
'justify-content': 'flex-end',
'display': 'flex'
},

cancel: {
type: 'button',
label: 'cancel',
onclick: function() {
var scrim = document.querySelectorAll('.satus-dialog__scrim');

scrim[scrim.length - 1].click();
}
},
ok: {
type: 'button',
label: 'OK',
onclick: function() {
var scrim = document.querySelectorAll('.satus-dialog__scrim');

scrim[scrim.length - 1].click();
}
}
}
});
};

file_reader.readAsText(this.files[0]);
});

input.click();
} catch (err) {
chrome.runtime.sendMessage({
name: 'dialog-error',
value: err
});
}
}
}
}
}
}
Expand Down
86 changes: 85 additions & 1 deletion src/js/header.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,91 @@ var Menu = {
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>',
onClickRender: {
type: 'dialog',
class: 'satus-dialog--vertical-menu'
class: 'satus-dialog--vertical-menu',

export: {
type: 'button',
label: 'Export',
onclick: function() {
chrome.runtime.sendMessage({
name: 'download',
filename: 'todo.json',
value: Satus.storage.get('data')
});
}
},
import: {
type: 'button',
label: 'Import',
onclick: function() {
try {
var input = document.createElement('input');

input.type = 'file';

input.addEventListener('change', function() {
var file_reader = new FileReader();

file_reader.onload = function() {
var data = JSON.parse(this.result);

for (var i in data) {
Satus.storage.set(i, data[i]);
}

Satus.render({
type: 'dialog',

message: {
type: 'text',
label: 'successfullyImportedSettings',
style: {
'width': '100%',
'opacity': '.8'
}
},
section: {
type: 'section',
class: 'controls',
style: {
'justify-content': 'flex-end',
'display': 'flex'
},

cancel: {
type: 'button',
label: 'cancel',
onclick: function() {
var scrim = document.querySelectorAll('.satus-dialog__scrim');

scrim[scrim.length - 1].click();
}
},
ok: {
type: 'button',
label: 'OK',
onclick: function() {
var scrim = document.querySelectorAll('.satus-dialog__scrim');

scrim[scrim.length - 1].click();
}
}
}
});
};

file_reader.readAsText(this.files[0]);
});

input.click();
} catch (err) {
chrome.runtime.sendMessage({
name: 'dialog-error',
value: err
});
}
}
}
}
}
}
Expand Down

0 comments on commit a6aa1fc

Please sign in to comment.