platypush-webext/src/utils.js

211 lines
4.9 KiB
JavaScript
Raw Normal View History

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) {
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 {};
}
return JSON.parse(response.hosts);
2020-06-13 17:28:50 +02:00
} finally {
this.loading = false;
}
},
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() {
this.loading = true;
try {
const response = await browser.storage.local.get('actions');
2020-06-15 22:33:06 +02:00
if (!response.actions) {
return {};
}
return JSON.parse(response.actions);
} 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;
}
},
async saveAction(action) {
2020-06-15 22:33:06 +02:00
const actions = await this.getActions();
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');
},
async loadConfig() {
2020-06-16 01:43:04 +02:00
this.loading = true;
try {
const [hosts, actions] = await Promise.all([this.getHosts(), this.getActions()]);
return {
hosts: hosts,
actions: actions,
};
} 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 || {};
try {
await Promise.all([this.saveHosts(hosts), this.saveActions(actions)]);
} 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: