2020-06-12 22:43:43 +02:00
|
|
|
import axios from 'axios';
|
|
|
|
|
|
|
|
export default {
|
2020-06-13 17:28:50 +02:00
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
loading: false,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2020-06-12 22:43:43 +02:00
|
|
|
methods: {
|
2020-06-16 01:43:04 +02:00
|
|
|
notify(message, title = 'platypush') {
|
2020-06-12 22:43:43 +02:00
|
|
|
browser.notifications.create({
|
|
|
|
type: 'basic',
|
|
|
|
title: title,
|
|
|
|
message: message,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
async run(action, host) {
|
|
|
|
const url = (host.ssl ? 'https' : 'http') + '://' + host.address + ':' + host.port + '/execute';
|
|
|
|
const config = {};
|
2020-06-18 21:27:51 +02:00
|
|
|
let args = action.args || {};
|
2020-06-12 22:43:43 +02:00
|
|
|
if (Array.isArray(action.args)) {
|
|
|
|
args = action.args
|
|
|
|
.filter(arg => arg.value && arg.value.length)
|
|
|
|
.reduce((obj, arg) => {
|
|
|
|
obj[arg.name] = arg.value;
|
|
|
|
return obj;
|
|
|
|
}, {});
|
|
|
|
}
|
|
|
|
|
2020-06-14 02:34:35 +02:00
|
|
|
Object.keys(args).forEach(name => {
|
|
|
|
if (args[name] === '$URL$') {
|
|
|
|
// URL wildcard
|
|
|
|
args[name] = window.location.href;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-06-12 22:43:43 +02:00
|
|
|
if (host.token && host.token.length) {
|
|
|
|
config.headers = {
|
|
|
|
'X-Token': host.token,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const msg = await axios.post(
|
|
|
|
url,
|
|
|
|
{
|
|
|
|
type: 'request',
|
2020-06-14 02:34:35 +02:00
|
|
|
action: action.name,
|
2020-06-12 22:43:43 +02:00
|
|
|
args: args,
|
|
|
|
},
|
|
|
|
config
|
|
|
|
);
|
|
|
|
|
|
|
|
const errors = msg.data.response.errors;
|
|
|
|
if (errors && errors.length) {
|
|
|
|
throw new Error(errors[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return msg.data.response.output;
|
|
|
|
} catch (e) {
|
2020-06-15 01:03:09 +02:00
|
|
|
this.notify(e.toString(), 'Request error');
|
2020-06-12 22:43:43 +02:00
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
},
|
2020-06-13 17:28:50 +02:00
|
|
|
|
2020-06-19 00:26:39 +02:00
|
|
|
async runScript(script, host) {
|
|
|
|
this.loading = true;
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (typeof script === 'string') {
|
|
|
|
/* eslint no-eval: "off" */
|
|
|
|
script = eval(this.script);
|
|
|
|
}
|
|
|
|
|
|
|
|
return await script(this, host, browser, window);
|
|
|
|
} catch (e) {
|
|
|
|
this.notify(e.message, 'Script error');
|
|
|
|
throw e;
|
|
|
|
} finally {
|
|
|
|
this.loading = false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-06-15 22:33:06 +02:00
|
|
|
async getHosts() {
|
2020-06-13 17:28:50 +02:00
|
|
|
this.loading = true;
|
|
|
|
|
|
|
|
try {
|
|
|
|
const response = await browser.storage.local.get('hosts');
|
2020-06-15 22:33:06 +02:00
|
|
|
if (!response.hosts) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-06-15 01:03:09 +02:00
|
|
|
return JSON.parse(response.hosts);
|
2020-06-13 17:28:50 +02:00
|
|
|
} finally {
|
|
|
|
this.loading = false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-06-15 01:03:09 +02:00
|
|
|
async saveHosts(hosts) {
|
|
|
|
this.loading = true;
|
|
|
|
try {
|
|
|
|
await browser.storage.local.set({ hosts: JSON.stringify(hosts) });
|
|
|
|
} finally {
|
|
|
|
this.loading = false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-06-15 22:33:06 +02:00
|
|
|
async getActions() {
|
2020-06-15 01:03:09 +02:00
|
|
|
this.loading = true;
|
|
|
|
|
|
|
|
try {
|
|
|
|
const response = await browser.storage.local.get('actions');
|
2020-06-15 22:33:06 +02:00
|
|
|
if (!response.actions) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-06-15 01:03:09 +02:00
|
|
|
return JSON.parse(response.actions);
|
|
|
|
} finally {
|
|
|
|
this.loading = false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-06-19 19:01:30 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-06-15 22:33:06 +02:00
|
|
|
async saveActions(actions) {
|
|
|
|
this.loading = true;
|
|
|
|
|
|
|
|
try {
|
|
|
|
await browser.storage.local.set({ actions: JSON.stringify(actions) });
|
|
|
|
} finally {
|
|
|
|
this.loading = false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-06-15 01:03:09 +02:00
|
|
|
async saveAction(action) {
|
2020-06-15 22:33:06 +02:00
|
|
|
const actions = await this.getActions();
|
2020-06-15 01:03:09 +02:00
|
|
|
if (action.displayName in actions) {
|
|
|
|
if (!confirm('An action with this name already exists. Do you want to overwrite it?')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
actions[action.displayName] = action;
|
2020-06-15 22:33:06 +02:00
|
|
|
await this.saveActions(actions);
|
|
|
|
this.notify('You can find this action under the Local Actions menu', 'Action saved');
|
2020-06-15 01:03:09 +02:00
|
|
|
},
|
|
|
|
|
2020-06-19 19:01:30 +02:00
|
|
|
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');
|
|
|
|
},
|
|
|
|
|
2020-06-15 01:03:09 +02:00
|
|
|
async loadConfig() {
|
2020-06-16 01:43:04 +02:00
|
|
|
this.loading = true;
|
|
|
|
|
|
|
|
try {
|
2020-06-19 19:01:30 +02:00
|
|
|
const [hosts, actions, scripts] = await Promise.all([this.getHosts(), this.getActions(), this.getScripts(false)]);
|
2020-06-16 01:43:04 +02:00
|
|
|
return {
|
|
|
|
hosts: hosts,
|
|
|
|
actions: actions,
|
2020-06-19 19:01:30 +02:00
|
|
|
scripts: scripts,
|
2020-06-16 01:43:04 +02:00
|
|
|
};
|
|
|
|
} finally {
|
|
|
|
this.loading = false;
|
2020-06-16 18:46:38 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
async saveConfig(config) {
|
|
|
|
this.loading = true;
|
|
|
|
const hosts = config.hosts || {};
|
|
|
|
const actions = config.actions || {};
|
2020-06-19 19:01:30 +02:00
|
|
|
const scripts = config.scripts || {};
|
2020-06-16 18:46:38 +02:00
|
|
|
|
|
|
|
try {
|
2020-06-19 19:01:30 +02:00
|
|
|
await Promise.all([this.saveHosts(hosts), this.saveActions(actions), this.saveScripts(scripts)]);
|
2020-06-16 18:46:38 +02:00
|
|
|
} finally {
|
|
|
|
this.loading = false;
|
2020-06-16 01:43:04 +02:00
|
|
|
}
|
2020-06-13 17:28:50 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
formToHost(form) {
|
|
|
|
return {
|
|
|
|
name: form.name.value,
|
|
|
|
address: form.address.value,
|
|
|
|
port: parseInt(form.port.value),
|
|
|
|
websocketPort: parseInt(form.websocketPort.value),
|
|
|
|
ssl: form.ssl.checked,
|
|
|
|
token: form.token.value,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
onAddrChange(form) {
|
|
|
|
if (form.name.value.length && !form.address.value.startsWith(form.name.value)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
form.name.value = form.address.value;
|
|
|
|
},
|
|
|
|
|
|
|
|
onPortChange(form) {
|
|
|
|
const port = form.port.value;
|
|
|
|
if (!this.isPortValid(port)) return;
|
|
|
|
form.websocketPort.value = '' + (parseInt(port) + 1);
|
|
|
|
},
|
|
|
|
|
|
|
|
isPortValid(port) {
|
|
|
|
port = parseInt(port);
|
|
|
|
return !isNaN(port) && port > 0 && port < 65536;
|
|
|
|
},
|
|
|
|
|
|
|
|
isHostFormValid(form) {
|
|
|
|
return form.name.value.length && form.address.value.length && this.isPortValid(form.port.value) && this.isPortValid(form.websocketPort.value);
|
|
|
|
},
|
2020-06-12 22:43:43 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
// vim:sw=2:ts=2:et:
|