Support for saving custom scripts

This commit is contained in:
Fabio Manganiello 2020-06-19 19:01:30 +02:00
parent 5d5ff69318
commit d7aadd52ec
3 changed files with 70 additions and 6 deletions

View File

@ -21,7 +21,8 @@
<form class="content" ref="content" @submit.prevent="save"> <form class="content" ref="content" @submit.prevent="save">
<div class="textarea"> <div class="textarea">
<textarea name="text" ref="text" v-model="config" v-text="loading ? 'Loading...' : config" @focus="onFocus" :disabled="loading" /> <PrismEditor name="text" v-model="config" :code="loading ? 'Loading...' : config" language="js" :emitEvents="true" />
<!-- <textarea name="text" ref="text" v-model="config" v-text="loading ? 'Loading...' : config" @focus="onFocus" :disabled="loading" /> -->
</div> </div>
<div class="buttons"> <div class="buttons">
@ -37,9 +38,14 @@
import axios from 'axios'; import axios from 'axios';
import mixins from '../utils'; import mixins from '../utils';
import 'prismjs';
import 'prismjs/themes/prism.css';
import PrismEditor from 'vue-prism-editor';
export default { export default {
name: 'Backup', name: 'Backup',
mixins: [mixins], mixins: [mixins],
components: { PrismEditor },
data() { data() {
return { return {
@ -64,11 +70,11 @@ export default {
this.savedConfig = this.config; this.savedConfig = this.config;
}, },
async save(event) { async save() {
this.loading = true; this.loading = true;
try { try {
const config = JSON.parse(event.target.text.value); const config = JSON.parse(this.config);
await this.saveConfig(config); await this.saveConfig(config);
await this.reload(); await this.reload();
this.$emit('reload'); this.$emit('reload');

View File

@ -297,7 +297,7 @@ export default {
script: this.script, script: this.script,
}; };
console.log(script); await this.saveScript(script);
}, },
async storeAction(event) { async storeAction(event) {

View File

@ -122,6 +122,28 @@ export default {
} }
}, },
async getScripts(parse = true) {
this.loading = true;
try {
const response = await browser.storage.local.get('scripts');
if (!response.scripts) {
return {};
}
return Object.entries(JSON.parse(response.scripts)).reduce((obj, [name, info]) => {
if (parse && typeof info.script === 'string') {
info.script = eval(info.script);
}
obj[name] = info;
return obj;
}, {});
} finally {
this.loading = false;
}
},
async saveActions(actions) { async saveActions(actions) {
this.loading = true; this.loading = true;
@ -145,14 +167,49 @@ export default {
this.notify('You can find this action under the Local Actions menu', 'Action saved'); this.notify('You can find this action under the Local Actions menu', 'Action saved');
}, },
async saveScripts(scripts) {
this.loading = true;
try {
scripts = Object.entries(scripts).reduce((obj, [name, info]) => {
if (typeof info.script === 'function') {
info.script = info.script.toString();
}
obj[name] = info;
return obj;
}, {});
await browser.storage.local.set({ scripts: JSON.stringify(scripts) });
} catch (e) {
this.notify(e.message, 'Error on script save');
} finally {
this.loading = false;
}
},
async saveScript(script) {
const scripts = await this.getScripts(false);
if (script.displayName in scripts) {
if (!confirm('A script with this name already exists. Do you want to overwrite it?')) {
return;
}
}
scripts[script.displayName] = script;
await this.saveScripts(scripts);
this.notify('You can find this script under the Local Actions menu', 'Script saved');
},
async loadConfig() { async loadConfig() {
this.loading = true; this.loading = true;
try { try {
const [hosts, actions] = await Promise.all([this.getHosts(), this.getActions()]); const [hosts, actions, scripts] = await Promise.all([this.getHosts(), this.getActions(), this.getScripts(false)]);
return { return {
hosts: hosts, hosts: hosts,
actions: actions, actions: actions,
scripts: scripts,
}; };
} finally { } finally {
this.loading = false; this.loading = false;
@ -163,9 +220,10 @@ export default {
this.loading = true; this.loading = true;
const hosts = config.hosts || {}; const hosts = config.hosts || {};
const actions = config.actions || {}; const actions = config.actions || {};
const scripts = config.scripts || {};
try { try {
await Promise.all([this.saveHosts(hosts), this.saveActions(actions)]); await Promise.all([this.saveHosts(hosts), this.saveActions(actions), this.saveScripts(scripts)]);
} finally { } finally {
this.loading = false; this.loading = false;
} }