emitter-impl => emitter

This commit is contained in:
antelle 2016-04-19 22:19:08 +03:00
parent 53f266f7c3
commit 177cbea905
7 changed files with 105 additions and 130 deletions

View File

@ -0,0 +1,15 @@
'use strict';
var Launcher = require('../../comp/launcher');
var AutoTypeEmitterFactory = {
create: function(callback) {
if (!Launcher) {
return null;
}
var AutoTypeEmitter = require('./emitter/auto-type-emitter-' + Launcher.platform());
return new AutoTypeEmitter(callback);
}
};
module.exports = AutoTypeEmitterFactory;

View File

@ -1,15 +0,0 @@
'use strict';
var Launcher = require('../../comp/launcher');
var AutoTypeEmitterImplFactory = {
create: function() {
if (!Launcher) {
return null;
}
var AutoTypeEmitterImpl = require('./emitter-impl/auto-type-emitter-impl-' + Launcher.platform());
return new AutoTypeEmitterImpl();
}
};
module.exports = AutoTypeEmitterImplFactory;

View File

@ -1,60 +0,0 @@
'use strict';
var Logger = require('../../util/logger'),
AutoTypeEmitterImplFactory = require('./auto-type-emitter-impl-factory');
var logger = new Logger('auto-type-emitter');
logger.setLevel(localStorage.autoTypeDebug ? Logger.Level.All : Logger.Level.Warn);
var AutoTypeEmitter = function(callback) {
this.callback = callback;
this.mod = {};
this.impl = AutoTypeEmitterImplFactory.create();
this.delay = 0;
};
AutoTypeEmitter.prototype.setMod = function(mod, enabled) {
logger.debug('Mod', mod, enabled);
if (enabled) {
this.mod[mod] = true;
} else {
delete this.mod[mod];
}
this.impl.setMod(mod, enabled);
};
AutoTypeEmitter.prototype.text = function(text) {
logger.debug('Text', text);
if (!text) {
return this.callback();
}
setTimeout(this.impl.text.bind(this.impl, text, this.callback), this.delay);
};
AutoTypeEmitter.prototype.key = function(key) {
logger.debug('Key', key);
setTimeout(this.impl.key.bind(this.impl, key, this.callback), this.delay);
};
AutoTypeEmitter.prototype.wait = function(time) {
logger.debug('Wait', time);
setTimeout(this.callback, time);
};
AutoTypeEmitter.prototype.setDelay = function(delay) {
logger.debug('SetDelay', delay);
this.delay = delay || 0;
this.callback();
};
AutoTypeEmitter.prototype.copyPaste = function(text) {
logger.debug('CopyPaste', text);
this.impl.copyPaste(text, this.callback);
};
AutoTypeEmitter.prototype.waitComplete = function() {
logger.debug('WaitComplete');
this.impl.waitComplete(this.callback);
};
module.exports = AutoTypeEmitter;

View File

@ -1,8 +1,12 @@
'use strict';
var AutoTypeObfuscator = require('./auto-type-obfuscator'),
AutoTypeEmitter = require('./auto-type-emitter'),
Format = require('../../util/format');
AutoTypeEmitterFactory = require('./auto-type-emitter-factory'),
Format = require('../../util/format'),
Logger = require('../../util/logger');
var emitterLogger = new Logger('auto-type-emitter');
emitterLogger.setLevel(localStorage.autoTypeDebug ? Logger.Level.All : Logger.Level.Warn);
var AutoTypeRunner = function(ops) {
this.ops = ops;
@ -305,7 +309,7 @@ AutoTypeRunner.prototype.obfuscateOp = function(op) {
};
AutoTypeRunner.prototype.run = function(callback) {
this.emitter = new AutoTypeEmitter(this.emitNext.bind(this));
this.emitter = AutoTypeEmitterFactory.create(this.emitNext.bind(this));
this.emitterState = {
callback: callback,
stack: [],
@ -337,6 +341,7 @@ AutoTypeRunner.prototype.emitNext = function(err) {
} else {
this.resetEmitterMod({});
this.emitterState.finished = true;
emitterLogger.debug('waitComplete');
this.emitter.waitComplete();
}
return;
@ -365,9 +370,13 @@ AutoTypeRunner.prototype.emitNext = function(err) {
}
switch (op.type) {
case 'text':
this.emitter.text(op.value);
emitterLogger.debug('text', op.value);
if (op.value) {
this.emitter.text(op.value);
}
break;
case 'key':
emitterLogger.debug('key', op.value);
this.emitter.key(op.value);
break;
case 'cmd':
@ -375,6 +384,7 @@ AutoTypeRunner.prototype.emitNext = function(err) {
if (!method) {
throw 'Bad cmd: ' + op.value;
}
emitterLogger.debug(op.value, op.arg);
method.call(this.emitter, op.arg);
break;
default:
@ -385,6 +395,7 @@ AutoTypeRunner.prototype.emitNext = function(err) {
AutoTypeRunner.prototype.setEmitterMod = function(addedMod) {
Object.keys(addedMod).forEach(function(mod) {
if (addedMod[mod] && !this.emitterState.activeMod[mod]) {
emitterLogger.debug('mod', mod, true);
this.emitter.setMod(mod, true);
this.emitterState.activeMod[mod] = true;
}
@ -394,6 +405,7 @@ AutoTypeRunner.prototype.setEmitterMod = function(addedMod) {
AutoTypeRunner.prototype.resetEmitterMod = function(targetState) {
Object.keys(this.emitterState.activeMod).forEach(function(mod) {
if (this.emitterState.activeMod[mod] && !targetState[mod]) {
emitterLogger.debug('mod', mod, false);
this.emitter.setMod(mod, false);
delete this.emitterState.activeMod[mod];
}

View File

@ -21,12 +21,13 @@ var ModMap = {
'^^': 'control'
};
var AutoTypeEmitterImpl = function() {
var AutoTypeEmitter = function(callback) {
this.callback = callback;
this.mod = {};
this.pendingScript = [];
};
AutoTypeEmitterImpl.prototype.setMod = function(mod, enabled) {
AutoTypeEmitter.prototype.setMod = function(mod, enabled) {
if (enabled) {
this.mod[mod] = true;
} else {
@ -34,61 +35,71 @@ AutoTypeEmitterImpl.prototype.setMod = function(mod, enabled) {
}
};
AutoTypeEmitterImpl.prototype.text = function(text, callback) {
AutoTypeEmitter.prototype.text = function(text) {
text = text.replace(/"/g, '\\"');
this.pendingScript.push('keystroke "' + text + '"'+ this.modString());
callback();
this.callback();
};
AutoTypeEmitterImpl.prototype.key = function(key, callback) {
AutoTypeEmitter.prototype.key = function(key) {
if (typeof key !== 'number') {
if (!KeyMap[key]) {
return callback('Bad key: ' + key);
return this.callback('Bad key: ' + key);
}
key = KeyMap[key];
}
this.pendingScript.push('key code ' + key + this.modString());
callback();
this.callback();
};
AutoTypeEmitterImpl.prototype.copyPaste = function(text, callback) {
AutoTypeEmitter.prototype.copyPaste = function(text) {
this.pendingScript.push('delay 0.5');
this.pendingScript.push('set the clipboard to "' + text.replace(/"/g, '\\"') + '"');
this.pendingScript.push('delay 0.5');
this.pendingScript.push('keystroke "v" using command down');
this.pendingScript.push('delay 0.5');
callback();
this.callback();
};
AutoTypeEmitterImpl.prototype.waitComplete = function(callback) {
AutoTypeEmitter.prototype.wait = function(time) {
this.pendingScript.push('delay ' + (time / 1000));
this.callback();
};
AutoTypeEmitter.prototype.waitComplete = function() {
if (this.pendingScript.length) {
var script = this.pendingScript.join('\n');
this.pendingScript.length = 0;
this.runScript(script, callback);
this.runScript(script);
} else {
callback();
this.callback();
}
};
AutoTypeEmitterImpl.prototype.modString = function() {
AutoTypeEmitter.prototype.setDelay = function(delay) {
this.delay = delay || 0;
this.callback('Not implemented');
};
AutoTypeEmitter.prototype.modString = function() {
var keys = Object.keys(this.mod);
if (!keys.length) {
return '';
}
return ' using {' + keys.map(AutoTypeEmitterImpl.prototype.mapMod).join(', ') + '}';
return ' using {' + keys.map(AutoTypeEmitter.prototype.mapMod).join(', ') + '}';
};
AutoTypeEmitterImpl.prototype.mapMod = function(mod) {
AutoTypeEmitter.prototype.mapMod = function(mod) {
return ModMap[mod] + ' down';
};
AutoTypeEmitterImpl.prototype.runScript = function(script, callback) {
AutoTypeEmitter.prototype.runScript = function(script) {
script = 'tell application "System Events" \n' + script + '\nend tell';
Launcher.spawn({
cmd: 'osascript',
data: script,
complete: callback
complete: this.callback
});
};
module.exports = AutoTypeEmitterImpl;
module.exports = AutoTypeEmitter;

View File

@ -22,12 +22,13 @@ var ModMap = {
'^^': 'ctrl'
};
var AutoTypeEmitterImpl = function() {
var AutoTypeEmitter = function(callback) {
this.callback = callback;
this.mod = {};
this.pendingScript = [];
};
AutoTypeEmitterImpl.prototype.setMod = function(mod, enabled) {
AutoTypeEmitter.prototype.setMod = function(mod, enabled) {
if (enabled) {
this.mod[ModMap[mod]] = true;
} else {
@ -35,38 +36,38 @@ AutoTypeEmitterImpl.prototype.setMod = function(mod, enabled) {
}
};
AutoTypeEmitterImpl.prototype.text = function(text, callback) {
AutoTypeEmitter.prototype.text = function(text) {
this.pendingScript.push('text ' + this.modString() + text);
callback();
this.callback();
};
AutoTypeEmitterImpl.prototype.key = function(key, callback) {
AutoTypeEmitter.prototype.key = function(key) {
if (typeof key !== 'number') {
if (!KeyMap[key]) {
return callback('Bad key: ' + key);
return this.callback('Bad key: ' + key);
}
key = KeyMap[key].toString(16);
}
this.pendingScript.push('key ' + this.modString() + key);
callback();
this.callback();
};
AutoTypeEmitterImpl.prototype.copyPaste = function(text, callback) {
AutoTypeEmitter.prototype.copyPaste = function(text) {
// TODO
callback();
this.callback();
};
AutoTypeEmitterImpl.prototype.waitComplete = function(callback) {
AutoTypeEmitter.prototype.waitComplete = function() {
if (this.pendingScript.length) {
var script = this.pendingScript.join(' ');
this.pendingScript.length = 0;
this.runScript(script, callback);
this.runScript(script);
} else {
callback();
this.callback();
}
};
AutoTypeEmitterImpl.prototype.modString = function() {
AutoTypeEmitter.prototype.modString = function() {
var mod = '';
Object.keys(this.mod).forEach(function (key) {
mod += key + '+';
@ -74,12 +75,12 @@ AutoTypeEmitterImpl.prototype.modString = function() {
return mod;
};
AutoTypeEmitterImpl.prototype.runScript = function(script, callback) {
AutoTypeEmitter.prototype.runScript = function(script) {
Launcher.spawn({
cmd: 'xdotool',
data: script,
complete: callback
complete: this.callback
});
};
module.exports = AutoTypeEmitterImpl;
module.exports = AutoTypeEmitter;

View File

@ -29,12 +29,13 @@ var ModVk = {
var TextReplaceRegex = /[\(\)\{}\[\]\+\^%~]/g;
var AutoTypeEmitterImpl = function() {
var AutoTypeEmitter = function(callback) {
this.callback = callback;
this.mod = {};
this.pendingScript = [];
};
AutoTypeEmitterImpl.prototype.setMod = function(mod, enabled) {
AutoTypeEmitter.prototype.setMod = function(mod, enabled) {
if (enabled) {
this.mod[ModMap[mod]] = true;
} else {
@ -42,17 +43,17 @@ AutoTypeEmitterImpl.prototype.setMod = function(mod, enabled) {
}
};
AutoTypeEmitterImpl.prototype.text = function(text, callback) {
AutoTypeEmitter.prototype.text = function(text) {
text = this.addMod(this.escapeText(text.replace(TextReplaceRegex, function(match) { return '{' + match + '}'; })));
this.pendingScript.push('[System.Windows.Forms.SendKeys]::SendWait("' + text + '")');
callback();
this.callback();
};
AutoTypeEmitterImpl.prototype.key = function(key, callback) {
AutoTypeEmitter.prototype.key = function(key) {
if (typeof key !== 'number') {
key = KeyMap[key];
if (!key) {
return callback('Bad key: ' + key);
return this.callback('Bad key: ' + key);
}
}
if (typeof key === 'number') {
@ -63,33 +64,43 @@ AutoTypeEmitterImpl.prototype.key = function(key, callback) {
var text = this.addMod(key);
this.pendingScript.push('[System.Windows.Forms.SendKeys]::SendWait("' + text + '")');
}
callback();
this.callback();
};
AutoTypeEmitterImpl.prototype.copyPaste = function(text, callback) {
AutoTypeEmitter.prototype.copyPaste = function(text) {
this.pendingScript.push('[System.Threading.Thread]::Sleep(500)');
this.pendingScript.push('[System.Windows.Forms.Clipboard]::SetText("' + this.escapeText(text) + '")');
this.pendingScript.push('[System.Threading.Thread]::Sleep(500)');
this.pendingScript.push('[System.Windows.Forms.SendKeys]::SendWait("+{ins}")');
this.pendingScript.push('[System.Threading.Thread]::Sleep(500)');
callback();
this.callback();
};
AutoTypeEmitterImpl.prototype.waitComplete = function(callback) {
AutoTypeEmitter.prototype.wait = function(time) {
this.pendingScript.push('[System.Threading.Thread]::Sleep(' + time + ')');
this.callback();
};
AutoTypeEmitter.prototype.waitComplete = function() {
if (this.pendingScript.length) {
var script = this.pendingScript.join('\n');
this.pendingScript.length = 0;
this.runScript(script, callback);
this.runScript(script);
} else {
callback();
this.callback();
}
};
AutoTypeEmitterImpl.prototype.escapeText = function(text) {
AutoTypeEmitter.prototype.setDelay = function(delay) {
this.delay = delay || 0;
this.callback('Not implemented');
};
AutoTypeEmitter.prototype.escapeText = function(text) {
return text.replace(/"/g, '`"').replace(/`/g, '``').replace(/\n/g, '``n');
};
AutoTypeEmitterImpl.prototype.addMod = function(text) {
AutoTypeEmitter.prototype.addMod = function(text) {
var keys = Object.keys(this.mod);
if (!keys.length || !text) {
return text;
@ -97,7 +108,7 @@ AutoTypeEmitterImpl.prototype.addMod = function(text) {
return keys.join('') + (text.length > 1 ? '(' + text + ')' : text);
};
AutoTypeEmitterImpl.prototype.runScript = function(script, callback) {
AutoTypeEmitter.prototype.runScript = function(script) {
script =
'Add-Type @"\n' +
'using System;\n' +
@ -119,8 +130,8 @@ script;
cmd: 'powershell',
args: ['-Command', '-'],
data: script,
complete: callback
complete: this.callback
});
};
module.exports = AutoTypeEmitterImpl;
module.exports = AutoTypeEmitter;