ultrawidify/src/popup/js/ExecAction.js

76 lines
2.3 KiB
JavaScript
Raw Normal View History

import Comms from '../../ext/lib/comms/Comms';
class ExecAction {
2019-02-13 23:58:19 +01:00
constructor(settings, site) {
this.settings = settings;
2019-02-13 23:58:19 +01:00
this.site = site;
}
setSettings(settings) {
this.settings = settings;
}
2019-02-13 23:58:19 +01:00
setSite(site) {
this.site = site;
}
async exec(action, scope, frame) {
for (var cmd of action.cmd) {
if (scope === 'page') {
const message = {
forwardToContentScript: true,
targetFrame: frame,
frame: frame,
cmd: cmd.action,
2019-04-25 22:02:10 +02:00
arg: cmd.arg,
customArg: cmd.customArg
}
Comms.sendMessage(message);
2019-06-02 23:52:27 +02:00
} else {
2019-02-13 23:58:19 +01:00
// set-ar-persistence sends stuff to content scripts as well (!)
// it's important to do that BEFORE the save step
if (cmd === 'set-ar-persistence') {
// even when setting global defaults, we only send message to the current tab in
// order to avoid problems related to
const message = {
forwardToContentScript: true,
targetFrame: frame,
frame: frame,
cmd: cmd.action,
arg: cmd.arg,
}
// this hopefully delays settings.save() until current crops are saved on the site
// and thus avoid any fucky-wuckies
await Comms.sendMessage(message);
}
2019-02-13 23:58:19 +01:00
let site = this.site;
2019-06-02 23:52:27 +02:00
if (scope === 'global') {
site = '@global';
} else if (!this.site) {
2019-02-13 23:58:19 +01:00
site = window.location.host;
}
2019-06-02 23:52:27 +02:00
if (scope === 'site' && !this.settings.active.sites[site]) {
2019-02-13 23:58:19 +01:00
this.settings.active.sites[site] = this.settings.getDefaultOption();
}
if (cmd.action === "set-stretch") {
2019-02-13 23:58:19 +01:00
this.settings.active.sites[site].stretch = cmd.arg;
} else if (cmd.action === "set-alignment") {
2019-02-13 23:58:19 +01:00
this.settings.active.sites[site].videoAlignment = cmd.arg;
} else if (cmd.action === "set-extension-mode") {
2019-02-13 23:58:19 +01:00
this.settings.active.sites[site].mode = cmd.arg;
} else if (cmd.action === "set-autoar-mode") {
2019-02-13 23:58:19 +01:00
this.settings.active.sites[site].autoar = cmd.arg;
2019-06-02 23:54:32 +02:00
} else if (cmd.action === 'set-keyboard') {
2019-06-02 23:52:27 +02:00
this.settings.active.sites[site].keyboardShortcutsEnabled = cmd.arg;
}
this.settings.save();
}
}
}
}
export default ExecAction;