2020-06-29 02:21:00 +02:00
|
|
|
import utils from './utils';
|
2020-07-12 16:50:20 +02:00
|
|
|
import Message from './listeners/message';
|
|
|
|
import Menu from './listeners/menu';
|
2020-06-29 02:21:00 +02:00
|
|
|
|
2020-06-12 01:03:46 +02:00
|
|
|
global.browser = require('webextension-polyfill');
|
2020-06-12 22:43:43 +02:00
|
|
|
|
2020-07-04 00:53:14 +02:00
|
|
|
const app = {
|
2020-06-29 02:21:00 +02:00
|
|
|
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;
|
|
|
|
}, {});
|
|
|
|
},
|
|
|
|
|
2020-07-12 16:50:20 +02:00
|
|
|
prepareMenu() {
|
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,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-12 16:50:20 +02:00
|
|
|
},
|
2020-06-29 02:21:00 +02:00
|
|
|
|
2020-07-12 16:50:20 +02:00
|
|
|
async refresh() {
|
|
|
|
this.hosts = await utils.methods.getHosts();
|
|
|
|
this.actions = await utils.methods.getActions();
|
|
|
|
this.scripts = await utils.methods.getScripts();
|
|
|
|
await browser.contextMenus.removeAll();
|
2020-07-04 00:53:14 +02:00
|
|
|
|
2020-07-12 16:50:20 +02:00
|
|
|
this.prepareMenu();
|
|
|
|
browser.contextMenus.onClicked.addListener(Menu.Listener);
|
|
|
|
browser.runtime.onConnect.addListener(Message.Listener);
|
2020-06-29 02:21:00 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
async create() {
|
|
|
|
await this.refresh();
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const onCreate = () => {
|
2020-07-04 00:53:14 +02:00
|
|
|
app.create().then(() => {
|
|
|
|
console.debug('Extension context created');
|
|
|
|
});
|
2020-06-29 02:21:00 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
onCreate();
|
|
|
|
|
2020-06-12 22:43:43 +02:00
|
|
|
// vim:sw=2:ts=2:et:
|