auto-type emitter

This commit is contained in:
antelle 2016-04-09 17:27:42 +03:00
parent 806f375e05
commit e35d5eb9ae
4 changed files with 138 additions and 10 deletions

View File

@ -0,0 +1,52 @@
'use strict';
var Logger = require('../../util/logger');
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 = {};
};
AutoTypeEmitter.prototype.setMod = function(mod, enabled) {
logger.debug('Mod', mod, enabled);
if (enabled) {
this.mod[mod] = true;
} else {
delete this.mod[mod];
}
};
AutoTypeEmitter.prototype.text = function(text) {
logger.debug('Text', text);
this.callback();
};
AutoTypeEmitter.prototype.key = function(key) {
logger.debug('Key', key);
this.callback();
};
AutoTypeEmitter.prototype.wait = function(time) {
logger.debug('Wait', time);
this.callback();
};
AutoTypeEmitter.prototype.setDelay = function(delay) {
logger.debug('Set delay', delay);
this.callback();
};
AutoTypeEmitter.prototype.copyText = function(text) {
logger.debug('Copy text', text);
this.callback();
};
AutoTypeEmitter.prototype.waitComplete = function() {
logger.debug('Wait complete');
this.callback();
};
module.exports = AutoTypeEmitter;

View File

@ -159,8 +159,8 @@ AutoTypeObfuscator.prototype.inputChar = function(ch) {
AutoTypeObfuscator.prototype.copyPaste = function(ch) {
logger.debug('copyPaste', ch);
this.ops.push({type: 'copyText', value: ch});
this.ops.push({type: 'waitComplete', value: ch});
this.ops.push({type: 'cmd', value: 'copyText', arg: ch});
this.ops.push({type: 'cmd', value: 'waitComplete'});
this.ops.push({type: 'key', value: 'v', mod: {'^': true}});
this.inputChars.splice(this.inputCursor, this.inputSel, { ch: ch });
this.inputCursor++;

View File

@ -1,6 +1,7 @@
'use strict';
var AutoTypeObfuscator = require('./auto-type-obfuscator'),
AutoTypeEmitter = require('./auto-type-emitter'),
Format = require('../../util/format');
var AutoTypeRunner = function(ops) {
@ -48,12 +49,6 @@ AutoTypeRunner.Substitutions = {
'dt_utc_second': function(runner) { return runner.udt('s'); }
};
AutoTypeRunner.Commands = {
wait: function() {},
setDelay: function() {},
copyText: function() {}
};
AutoTypeRunner.prototype.resolve = function(entry, callback) {
this.entry = entry;
try {
@ -305,7 +300,89 @@ AutoTypeRunner.prototype.obfuscateOp = function(op) {
op.type = 'group';
};
AutoTypeRunner.prototype.run = function() {
AutoTypeRunner.prototype.run = function(callback) {
this.emitter = new AutoTypeEmitter(this.emitNext.bind(this));
this.emitterState = {
callback: callback,
stack: [],
ops: this.ops,
opIx: 0,
mod: {},
activeMod: {}
};
this.emitNext(this.ops);
};
AutoTypeRunner.prototype.emitNext = function() {
this.resetEmitterMod(this.emitterState.mod);
if (this.emitterState.opIx >= this.emitterState.ops.length) {
var state = this.emitterState.stack.pop();
if (state) {
_.extend(this.emitterState, { ops: state.ops, opIx: state.opIx, mod: state.mod });
this.emitNext();
} else {
this.resetEmitterMod({});
this.emitterState.callback();
}
return;
}
var op = this.emitterState.ops[this.emitterState.opIx];
if (op.type === 'group') {
if (op.mod) {
this.setEmitterMod(op.mod);
}
this.emitterState.stack.push({
ops: this.emitterState.ops,
opIx: this.emitterState.opIx + 1,
mod: _.clone(this.emitterState.mod)
});
_.extend(this.emitterState, {
ops: op.value,
opIx: 0,
mod: _.clone(this.emitterState.activeMod)
});
this.emitNext();
return;
}
this.emitterState.opIx++;
if (op.mod) {
this.setEmitterMod(op.mod);
}
switch (op.type) {
case 'text':
this.emitter.text(op.value);
break;
case 'key':
this.emitter.key(op.value);
break;
case 'cmd':
var method = this.emitter[op.value];
if (!method) {
throw 'Bad cmd: ' + op.value;
}
method.call(this.emitter, op.arg);
break;
default:
throw 'Bad op: ' + op.type;
}
};
AutoTypeRunner.prototype.setEmitterMod = function(addedMod) {
Object.keys(addedMod).forEach(function(mod) {
if (addedMod[mod] && !this.emitterState.activeMod[mod]) {
this.emitter.setMod(mod, true);
this.emitterState.activeMod[mod] = true;
}
}, this);
};
AutoTypeRunner.prototype.resetEmitterMod = function(targetState) {
Object.keys(this.emitterState.activeMod).forEach(function(mod) {
if (this.emitterState.activeMod[mod] && !targetState[mod]) {
this.emitter.setMod(mod, false);
delete this.emitterState.activeMod[mod];
}
}, this);
};
module.exports = AutoTypeRunner;

View File

@ -39,7 +39,6 @@ var AutoType = {
return callback();
});
});
runner.run();
} catch (ex) {
logger.error('Parse error', ex);
return callback(ex);