fix auto-type

This commit is contained in:
antelle 2016-04-10 09:32:18 +03:00
parent 8236146ed2
commit 195884062e
4 changed files with 55 additions and 28 deletions

View File

@ -40,19 +40,19 @@ AutoTypeEmitter.prototype.wait = function(time) {
};
AutoTypeEmitter.prototype.setDelay = function(delay) {
logger.debug('Set delay', delay);
logger.debug('SetDelay', delay);
this.delay = delay || 0;
this.callback();
};
AutoTypeEmitter.prototype.copyText = function(text) {
logger.debug('Copy text', text);
AutoTypeEmitter.prototype.copyPaste = function(text) {
logger.debug('CopyPaste', text);
Launcher.setClipboardText(text);
this.callback();
this.impl.paste(this.callback);
};
AutoTypeEmitter.prototype.waitComplete = function() {
logger.debug('Wait complete');
logger.debug('WaitComplete');
this.impl.waitComplete(this.callback);
};

View File

@ -7,6 +7,7 @@ logger.setLevel(localStorage.autoTypeDebug ? Logger.Level.All : Logger.Level.War
var MaxFakeOps = 50;
var MaxSteps = 1000;
var MaxCopy = 2;
var FakeCharAlphabet = 'ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz123456789O0oIl';
var AutoTypeObfuscator = function(chars) {
@ -16,6 +17,7 @@ var AutoTypeObfuscator = function(chars) {
this.inputSel = 0;
this.ops = [];
this.stepCount = 0;
this.copyCount = 0;
};
AutoTypeObfuscator.prototype.obfuscate = function() {
@ -56,14 +58,14 @@ AutoTypeObfuscator.prototype.stepFake = function() {
this.moveToPos(pos);
var insert = this.inputChars.length === 0 || Math.random() > 0.3;
if (insert) {
if (Math.random() > 0.1) {
this.inputChar(ch);
} else {
this.copyPaste(ch);
}
this.inputChar(ch);
} else {
var moveLeft = pos > 0 && Math.random() > 0.5;
var moveLeft = Math.random() > 0.5;
var maxMove = moveLeft ? pos : this.inputChars.length - pos;
if (maxMove === 0) {
moveLeft = !moveLeft;
maxMove = moveLeft ? pos : this.inputChars.length - pos;
}
var moveCount = Math.max(Math.floor(Math.pow(Math.random(), 3) * maxMove), 1);
if (moveCount <= 1 && Math.random() > 0.5) {
this.deleteText(moveLeft);
@ -116,10 +118,11 @@ AutoTypeObfuscator.prototype.stepReal = function() {
} else {
var insPos = action.from + Math.floor(Math.random() * (action.to - action.from));
this.moveToPos(insPos);
if (Math.random() > 0.5) {
this.inputChar(action.ch);
} else {
if (this.copyCount < MaxCopy && Math.random() > 0.5) {
this.copyCount++;
this.copyPaste(action.ch);
} else {
this.inputChar(action.ch);
}
this.inputChars[insPos].ix = action.ix;
}
@ -159,9 +162,7 @@ AutoTypeObfuscator.prototype.inputChar = function(ch) {
AutoTypeObfuscator.prototype.copyPaste = function(ch) {
logger.debug('copyPaste', 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.ops.push({type: 'cmd', value: 'copyPaste', arg: ch});
this.inputChars.splice(this.inputCursor, this.inputSel, { ch: ch });
this.inputCursor++;
this.inputSel = 0;
@ -188,12 +189,15 @@ AutoTypeObfuscator.prototype.selectText = function(backward, count) {
AutoTypeObfuscator.prototype.deleteText = function(backward) {
logger.debug('deleteText', backward ? 'left' : 'right');
this.ops.push({ type: 'key', value: backward ? 'bs' : 'del' });
var deleteCount = this.inputSel || 1;
this.inputChars.splice(backward ? this.inputCursor - deleteCount : this.inputCursor, deleteCount);
if (backward) {
this.inputCursor--;
if (this.inputSel) {
this.inputChars.splice(this.inputCursor, this.inputSel);
this.inputSel = 0;
} else {
this.inputChars.splice(backward ? this.inputCursor - 1 : this.inputCursor, 1);
if (backward) {
this.inputCursor--;
}
}
this.inputSel = 0;
};
module.exports = AutoTypeObfuscator;

View File

@ -312,16 +312,22 @@ AutoTypeRunner.prototype.run = function(callback) {
ops: this.ops,
opIx: 0,
mod: {},
activeMod: {}
activeMod: {},
finished: null
};
this.emitNext();
};
AutoTypeRunner.prototype.emitNext = function(err) {
if (err) {
this.emitterState.finished = true;
this.emitterState.callback(err);
return;
}
if (this.emitterState.finished) {
this.emitterState.callback();
return;
}
this.resetEmitterMod(this.emitterState.mod);
if (this.emitterState.opIx >= this.emitterState.ops.length) {
var state = this.emitterState.stack.pop();
@ -330,7 +336,8 @@ AutoTypeRunner.prototype.emitNext = function(err) {
this.emitNext();
} else {
this.resetEmitterMod({});
this.emitterState.callback();
this.emitterState.finished = true;
this.emitter.waitComplete();
}
return;
}

View File

@ -25,6 +25,7 @@ var ModMap = {
var AutoTypeEmitterImpl = function() {
this.mod = {};
this.pendingScript = [];
};
AutoTypeEmitterImpl.prototype.setMod = function(mod, enabled) {
@ -40,7 +41,8 @@ AutoTypeEmitterImpl.prototype.text = function(text, callback) {
return callback();
}
text = text.replace(/"/g, '\\"');
this.runScript('keystroke "' + text + '"'+ this.modString(), callback);
this.pendingScript.push('keystroke "' + text + '"'+ this.modString());
callback();
};
AutoTypeEmitterImpl.prototype.key = function(key, callback) {
@ -50,11 +52,24 @@ AutoTypeEmitterImpl.prototype.key = function(key, callback) {
}
key = KeyMap[key];
}
this.runScript('key code ' + key + this.modString(), callback);
this.pendingScript.push('key code ' + key + this.modString());
callback();
};
AutoTypeEmitterImpl.prototype.paste = function(callback) {
this.pendingScript.push('keystroke "v" using command down');
this.pendingScript.push('delay 0.2');
this.waitComplete(callback);
};
AutoTypeEmitterImpl.prototype.waitComplete = function(callback) {
callback();
if (this.pendingScript.length) {
var script = this.pendingScript.join('\n');
this.pendingScript.length = 0;
this.runScript(script, callback);
} else {
callback();
}
};
AutoTypeEmitterImpl.prototype.modString = function() {
@ -70,7 +85,8 @@ AutoTypeEmitterImpl.prototype.mapMod = function(mod) {
};
AutoTypeEmitterImpl.prototype.runScript = function(script, callback) {
var ps = spawn('osascript', ['-e', 'tell application "System Events" to ' + script]);
script = 'tell application "System Events" \n' + script + '\nend tell';
var ps = spawn('osascript', ['-e', script]);
ps.on('close', function(code) { callback(code ? 'Exit code ' + code : undefined); });
};