keeweb/app/scripts/auto-type/emitter/auto-type-emitter-linux.js

162 lines
3.9 KiB
JavaScript
Raw Normal View History

2019-09-15 14:16:32 +02:00
import { Launcher } from 'comp/launcher';
import { Locale } from 'util/locale';
2016-04-19 20:48:49 +02:00
// https://cgit.freedesktop.org/xorg/proto/x11proto/plain/keysymdef.h
2016-08-07 19:56:40 +02:00
const KeyMap = {
2019-08-16 23:05:39 +02:00
tab: 'Tab',
enter: 'KP_Enter',
space: 'KP_Space',
up: 'Up',
down: 'Down',
left: 'Left',
right: 'Right',
home: 'Home',
end: 'End',
pgup: 'Page_Up',
pgdn: 'Page_Down',
ins: 'Insert',
del: 'Delete',
bs: 'BackSpace',
esc: 'Escape',
win: 'Meta_L',
rwin: 'Meta_R',
f1: 'F1',
f2: 'F2',
f3: 'F3',
f4: 'F4',
f5: 'F5',
f6: 'F6',
f7: 'F7',
f8: 'F8',
f9: 'F9',
f10: 'F10',
f11: 'F11',
f12: 'F12',
f13: 'F13',
f14: 'F14',
f15: 'F15',
f16: 'F16',
add: 'KP_Add',
subtract: 'KP_Subtract',
multiply: 'KP_Multiply',
divide: 'KP_Divide',
n0: 'KP_0',
n1: 'KP_1',
n2: 'KP_2',
n3: 'KP_3',
n4: 'KP_4',
n5: 'KP_5',
n6: 'KP_6',
n7: 'KP_7',
n8: 'KP_8',
n9: 'KP_9'
2016-04-19 20:48:49 +02:00
};
2016-08-07 19:56:40 +02:00
const ModMap = {
2016-04-19 20:48:49 +02:00
'^': 'ctrl',
'+': 'shift',
'%': 'alt',
'^^': 'ctrl'
};
2020-06-01 16:53:51 +02:00
const AutoTypeEmitter = function (callback, windowId) {
2016-04-19 21:19:08 +02:00
this.callback = callback;
if (typeof windowId !== 'undefined' && windowId) {
this.windowParameter = '--window ' + windowId + ' ';
} else {
this.windowParameter = '';
}
2016-04-19 20:48:49 +02:00
this.mod = {};
this.pendingScript = [];
};
2020-06-01 16:53:51 +02:00
AutoTypeEmitter.prototype.setMod = function (mod, enabled) {
2016-04-19 20:48:49 +02:00
if (enabled) {
this.mod[ModMap[mod]] = true;
} else {
delete this.mod[ModMap[mod]];
}
};
2020-06-01 16:53:51 +02:00
AutoTypeEmitter.prototype.text = function (text) {
2016-09-13 07:28:41 +02:00
this.pendingScript.push('keyup ctrl alt shift t');
2020-06-01 16:53:51 +02:00
Object.keys(this.mod).forEach((mod) => {
this.pendingScript.push('keydown ' + this.windowParameter + ModMap[mod]);
2016-04-25 23:19:40 +02:00
});
2020-06-01 16:53:51 +02:00
text.split('').forEach((char) => {
2020-05-21 16:51:25 +02:00
this.pendingScript.push(
'key ' + this.windowParameter + 'U' + char.charCodeAt(0).toString(16)
2020-05-21 16:51:25 +02:00
);
2016-04-25 23:19:40 +02:00
});
2020-06-01 16:53:51 +02:00
Object.keys(this.mod).forEach((mod) => {
this.pendingScript.push('keyup ' + this.windowParameter + ModMap[mod]);
2016-07-30 21:12:49 +02:00
});
this.waitComplete();
2016-04-19 20:48:49 +02:00
};
2020-06-01 16:53:51 +02:00
AutoTypeEmitter.prototype.key = function (key) {
const isSpecialKey = typeof key !== 'number';
if (isSpecialKey) {
2016-04-19 20:48:49 +02:00
if (!KeyMap[key]) {
2016-04-19 21:19:08 +02:00
return this.callback('Bad key: ' + key);
2016-04-19 20:48:49 +02:00
}
key = KeyMap[key].toString();
2016-04-19 20:48:49 +02:00
}
2020-05-21 16:51:25 +02:00
this.pendingScript.push(
'key --clearmodifiers ' + this.windowParameter + this.modString() + key
2020-05-21 16:51:25 +02:00
);
if (isSpecialKey) {
this.waitComplete();
} else {
this.callback();
}
2016-04-19 20:48:49 +02:00
};
2020-06-01 16:53:51 +02:00
AutoTypeEmitter.prototype.copyPaste = function (text) {
2016-04-25 23:19:40 +02:00
this.pendingScript.push('sleep 0.5');
2016-04-19 21:21:32 +02:00
Launcher.setClipboardText(text);
this.pendingScript.push('key --clearmodifiers ' + this.windowParameter + 'shift+Insert');
2016-04-25 23:19:40 +02:00
this.pendingScript.push('sleep 0.5');
2016-04-19 21:21:32 +02:00
this.waitComplete();
2016-04-19 20:48:49 +02:00
};
2020-06-01 16:53:51 +02:00
AutoTypeEmitter.prototype.wait = function (time) {
2019-08-16 23:05:39 +02:00
this.pendingScript.push('sleep ' + time / 1000);
2016-07-14 20:56:48 +02:00
this.callback();
};
2020-06-01 16:53:51 +02:00
AutoTypeEmitter.prototype.waitComplete = function (callback) {
2016-04-19 20:48:49 +02:00
if (this.pendingScript.length) {
2017-01-31 07:50:28 +01:00
const script = this.pendingScript.join(' ');
2016-04-19 20:48:49 +02:00
this.pendingScript.length = 0;
2016-04-25 23:19:40 +02:00
this.runScript(script, callback);
2016-04-19 20:48:49 +02:00
} else {
2016-04-19 21:19:08 +02:00
this.callback();
2016-04-19 20:48:49 +02:00
}
};
2020-06-01 16:53:51 +02:00
AutoTypeEmitter.prototype.modString = function () {
2017-01-31 07:50:28 +01:00
let mod = '';
2020-06-01 16:53:51 +02:00
Object.keys(this.mod).forEach((key) => {
2016-04-19 20:48:49 +02:00
mod += key + '+';
});
return mod;
};
2020-06-01 16:53:51 +02:00
AutoTypeEmitter.prototype.runScript = function (script, callback) {
2016-04-19 20:48:49 +02:00
Launcher.spawn({
cmd: 'xdotool',
2016-04-25 23:19:40 +02:00
args: ['-'],
2016-04-19 20:48:49 +02:00
data: script,
2016-08-07 19:56:40 +02:00
complete: (err, stdout, code) => {
if (err && err.code === 'ENOENT') {
err = Locale.autoTypeErrorNotInstalled.replace('{}', 'xdotool');
}
2017-01-31 07:50:28 +01:00
const cb = callback || this.callback;
2016-08-07 19:56:40 +02:00
cb(err, stdout, code);
}
2016-04-19 20:48:49 +02:00
});
};
2019-09-15 14:16:32 +02:00
export { AutoTypeEmitter };