platypush-webext/src/background.js

94 lines
2.5 KiB
JavaScript
Raw Normal View History

2020-06-29 02:21:00 +02:00
import utils from './utils';
2020-06-12 01:03:46 +02:00
global.browser = require('webextension-polyfill');
2020-06-12 22:43:43 +02:00
2020-06-29 02:21:00 +02:00
const menu = {
hosts: {},
actions: {},
scripts: {},
categories: {},
separator: '//',
categoriesByHost(host) {
return Object.entries({ ...(this.actions || {}), ...(this.scripts || {}) }).reduce((obj, [actionName, action]) => {
if (action.hosts.indexOf(host) < 0) {
return obj;
}
const appendAction = category => {
if (!(category in obj)) {
obj[category] = {};
}
obj[category][actionName] = action;
};
if (!(action.categories && action.categories.length)) {
appendAction('');
} else {
action.categories.forEach(category => appendAction(category));
}
return obj;
}, {});
},
async refresh() {
this.hosts = await utils.methods.getHosts();
this.actions = await utils.methods.getActions();
this.scripts = await utils.methods.getScripts();
2020-07-01 17:41:00 +02:00
await browser.contextMenus.removeAll();
2020-06-29 02:21:00 +02:00
for (const [host] of Object.entries(this.hosts)) {
const hostId = this.separator + host;
browser.contextMenus.create({
id: hostId,
title: host,
});
const categories = this.categoriesByHost(host);
for (const [categoryName, category] of Object.entries(categories)) {
const categoryId = hostId + this.separator + (categoryName.length ? categoryName : '[NONE]');
browser.contextMenus.create({
id: categoryId,
parentId: hostId,
title: categoryName.length ? categoryName : '[No Category]',
});
for (const [action] of Object.entries(category)) {
const actionId = categoryId + this.separator + action;
browser.contextMenus.create({
id: actionId,
parentId: categoryId,
title: action,
});
}
}
}
browser.contextMenus.onClicked.addListener(async (info, tab) => {
const [host, , action] = info.menuItemId.split(this.separator).slice(1);
const target = await utils.methods.getTargetElement();
if (action in this.actions) {
2020-07-01 17:41:00 +02:00
await utils.methods.run(this.actions[action], this.hosts[host]);
2020-06-29 02:21:00 +02:00
} else {
2020-07-01 17:41:00 +02:00
await utils.methods.runScript(this.scripts[action].script, this.hosts[host], tab, target);
2020-06-29 02:21:00 +02:00
}
});
},
async create() {
await this.refresh();
},
};
const onCreate = () => {
2020-07-01 17:41:00 +02:00
// noinspection JSIgnoredPromiseFromCall
2020-06-29 02:21:00 +02:00
menu.create();
};
onCreate();
2020-06-12 22:43:43 +02:00
// vim:sw=2:ts=2:et: