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,
|
|
|
|
hosts: [],
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2020-06-12 22:43:43 +02:00
|
|
|
methods: {
|
|
|
|
notify(message, title) {
|
|
|
|
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 = {};
|
|
|
|
let args = action.args;
|
|
|
|
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) {
|
|
|
|
this.notify('Request error', e.toString());
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
},
|
2020-06-13 17:28:50 +02:00
|
|
|
|
|
|
|
async loadHosts() {
|
|
|
|
this.loading = true;
|
|
|
|
|
|
|
|
try {
|
|
|
|
const response = await browser.storage.local.get('hosts');
|
|
|
|
this.hosts = JSON.parse(response.hosts);
|
|
|
|
} finally {
|
|
|
|
this.loading = false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
async saveHosts() {
|
|
|
|
await browser.storage.local.set({ hosts: JSON.stringify(this.hosts) });
|
|
|
|
},
|
|
|
|
|
|
|
|
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:
|