From f9d593356bfaa6f54d4af94dc214e54ecaf54e93 Mon Sep 17 00:00:00 2001 From: antelle Date: Tue, 19 Apr 2016 20:39:17 +0300 Subject: [PATCH] auto-type bugfixes --- .jshintrc | 2 +- .../comp/auto-type/auto-type-obfuscator.js | 2 +- .../auto-type-emitter-impl-darwin.js | 12 ++--- .../auto-type-emitter-impl-win32.js | 54 ++++++++++--------- .../helper/auto-type-helper-darwin.js | 21 ++------ app/scripts/comp/launcher.js | 41 +++++++++++++- 6 files changed, 79 insertions(+), 53 deletions(-) diff --git a/.jshintrc b/.jshintrc index b09ce41d..c5bae1e3 100644 --- a/.jshintrc +++ b/.jshintrc @@ -51,7 +51,7 @@ "laxbreak" : false, // true: Tolerate possibly unsafe line breakings "laxcomma" : false, // true: Tolerate comma-first style coding "loopfunc" : false, // true: Tolerate functions being defined in loops - "multistr" : true, // true: Tolerate multi-line strings + "multistr" : false, // true: Tolerate multi-line strings "noyield" : false, // true: Tolerate generator functions with no yield statement in them. "notypeof" : false, // true: Tolerate invalid typeof operator values "proto" : false, // true: Tolerate using the `__proto__` property diff --git a/app/scripts/comp/auto-type/auto-type-obfuscator.js b/app/scripts/comp/auto-type/auto-type-obfuscator.js index 9095f57d..8521937c 100644 --- a/app/scripts/comp/auto-type/auto-type-obfuscator.js +++ b/app/scripts/comp/auto-type/auto-type-obfuscator.js @@ -5,7 +5,7 @@ var Logger = require('../../util/logger'); var logger = new Logger('auto-type-obfuscator'); logger.setLevel(localStorage.autoTypeDebug ? Logger.Level.All : Logger.Level.Warn); -var MaxFakeOps = 50; +var MaxFakeOps = 30; var MaxSteps = 1000; var MaxCopy = 2; var FakeCharAlphabet = 'ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz123456789O0oIl'; diff --git a/app/scripts/comp/auto-type/emitter-impl/auto-type-emitter-impl-darwin.js b/app/scripts/comp/auto-type/emitter-impl/auto-type-emitter-impl-darwin.js index eb735e4e..e542dd54 100644 --- a/app/scripts/comp/auto-type/emitter-impl/auto-type-emitter-impl-darwin.js +++ b/app/scripts/comp/auto-type/emitter-impl/auto-type-emitter-impl-darwin.js @@ -2,8 +2,6 @@ var Launcher = require('../../launcher'); -var spawn = Launcher.req('child_process').spawn; - var KeyMap = { tab: 48, enter: 36, space: 49, up: 126, down: 125, left: 123, right: 124, home: 115, end: 119, pgup: 116, pgdn: 121, @@ -89,11 +87,11 @@ AutoTypeEmitterImpl.prototype.mapMod = function(mod) { AutoTypeEmitterImpl.prototype.runScript = function(script, callback) { script = 'tell application "System Events" \n' + script + '\nend tell'; - var ps = spawn('osascript'); - ps.stdin.setEncoding('utf-8'); - ps.stdin.write(script); - ps.stdin.end(); - ps.on('close', function(code) { callback(code ? 'Exit code ' + code : undefined); }); + Launcher.spawn({ + cmd: 'osascript', + data: script, + complete: callback + }); }; module.exports = AutoTypeEmitterImpl; diff --git a/app/scripts/comp/auto-type/emitter-impl/auto-type-emitter-impl-win32.js b/app/scripts/comp/auto-type/emitter-impl/auto-type-emitter-impl-win32.js index e9b1fe81..47d5fdb4 100644 --- a/app/scripts/comp/auto-type/emitter-impl/auto-type-emitter-impl-win32.js +++ b/app/scripts/comp/auto-type/emitter-impl/auto-type-emitter-impl-win32.js @@ -2,8 +2,6 @@ var Launcher = require('../../launcher'); -var spawn = Launcher.req('child_process').spawn; - var KeyMap = { tab: '{tab}', enter: '{enter}', space: '{space}', up: '{up}', down: '{down}', left: '{left}', right: '{right}', home: '{home}', end: '{end}', pgup: '{pgup}', pgdn: '{pgdn}', @@ -49,7 +47,7 @@ AutoTypeEmitterImpl.prototype.text = function(text, callback) { return callback(); } text = this.addMod(this.escapeText(text.replace(TextReplaceRegex, function(match) { return '{' + match + '}'; }))); - this.pendingScript.push('[System.Windows.Forms.SendKeys]::SendWait("' + text + '");'); + this.pendingScript.push('[System.Windows.Forms.SendKeys]::SendWait("' + text + '")'); callback(); }; @@ -66,18 +64,18 @@ AutoTypeEmitterImpl.prototype.key = function(key, callback) { Object.keys(this.mod).forEach(function(mod) { this.pendingScript.push('[KwHelper]::Up(' + ModVk[mod] + ')'); }, this); } else { var text = this.addMod(key); - this.pendingScript.push('[System.Windows.Forms.SendKeys]::SendWait("' + text + '");'); + this.pendingScript.push('[System.Windows.Forms.SendKeys]::SendWait("' + text + '")'); } callback(); }; AutoTypeEmitterImpl.prototype.copyPaste = function(text, callback) { - this.pendingScript.push('[System.Threading.Thread]::Sleep(5000)'); + 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(5000)'); + this.pendingScript.push('[System.Threading.Thread]::Sleep(500)'); this.pendingScript.push('[System.Windows.Forms.SendKeys]::SendWait("+{ins}")'); - this.pendingScript.push('[System.Threading.Thread]::Sleep(5000)'); - this.waitComplete(callback); + this.pendingScript.push('[System.Threading.Thread]::Sleep(500)'); + callback(); }; AutoTypeEmitterImpl.prototype.waitComplete = function(callback) { @@ -103,25 +101,29 @@ AutoTypeEmitterImpl.prototype.addMod = function(text) { }; AutoTypeEmitterImpl.prototype.runScript = function(script, callback) { - script = 'Add-Type @"\ -using System;\ -using System.Runtime.InteropServices;\ -public static class KwHelper {\ - [DllImport("user32.dll")]\ - static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo);\ - public static void Down(byte code) { keybd_event(code, 0, 1, UIntPtr.Zero); }\ - public static void Up(byte code) { keybd_event(code, 0, 3, UIntPtr.Zero); }\ - public static void Press(byte code) { Down(code); Up(code); }\ -}\ -"@\ -[Console]::InputEncoding = [System.Text.Encoding]::UTF8\ -[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")\n' + script; + script = +'Add-Type @"\n' + +'using System;\n' + +'using System.Runtime.InteropServices;\n' + +'public static class KwHelper {\n' + + '[DllImport("user32.dll")]\n' + + 'static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo);\n' + + 'public static void Down(byte code) { keybd_event(code, 0, 1, UIntPtr.Zero); }\n' + + 'public static void Up(byte code) { keybd_event(code, 0, 3, UIntPtr.Zero); }\n' + + 'public static void Press(byte code) { Down(code); Up(code); }\n' + +'}\n' + +'"@\n\n' + +'[Console]::InputEncoding = [System.Text.Encoding]::UTF8\n' + +'[Console]::OutputEncoding = [System.Text.Encoding]::UTF8\n' + +'[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")\n' + +script; - var ps = spawn('powershell', ['-Command', '-']); - ps.stdin.setEncoding('utf-8'); - ps.stdin.write(script); - ps.stdin.end(); - ps.on('close', function(code) { callback(code ? 'Exit code ' + code : undefined); }); + Launcher.spawn({ + cmd: 'powershell', + args: ['-Command', '-'], + data: script, + complete: callback + }); }; module.exports = AutoTypeEmitterImpl; diff --git a/app/scripts/comp/auto-type/helper/auto-type-helper-darwin.js b/app/scripts/comp/auto-type/helper/auto-type-helper-darwin.js index 4521b0bd..58806f6f 100644 --- a/app/scripts/comp/auto-type/helper/auto-type-helper-darwin.js +++ b/app/scripts/comp/auto-type/helper/auto-type-helper-darwin.js @@ -2,8 +2,6 @@ var Launcher = require('../../launcher'); -var spawn = Launcher.req('child_process').spawn; - var ForeMostAppScript = 'tell application "System Events" to set frontApp to name of first process whose frontmost is true'; var ChromeScript = 'tell application "{}" to set appUrl to URL of active tab of front window\n' + 'tell application "{}" to set appTitle to title of active tab of front window\n' + @@ -53,21 +51,10 @@ AutoTypeHelper.prototype.getActiveWindowTitle = function(callback) { }; AutoTypeHelper.exec = function(script, callback) { - var ps = spawn('osascript', ['-e', script]); - var stderr = ''; - var stdout = ''; - ps.stdout.on('data', function(data) { - stdout += data.toString(); - }); - ps.stderr.on('data', function(data) { - stderr += data.toString(); - }); - ps.on('close', function(code) { - if (code) { - callback('Exit code ' + code + ': ' + stderr + (stdout ? '\nout: ' + stdout : '')); - } else { - callback(null, stdout); - } + Launcher.spawn({ + cmd: 'osascript', + args: ['-e', script], + complete: callback }); }; diff --git a/app/scripts/comp/launcher.js b/app/scripts/comp/launcher.js index 1864a04a..a3909298 100644 --- a/app/scripts/comp/launcher.js +++ b/app/scripts/comp/launcher.js @@ -1,10 +1,13 @@ 'use strict'; var Backbone = require('backbone'), - Locale = require('../util/locale'); + Locale = require('../util/locale'), + Logger = require('../util/logger'); var Launcher; +var logger = new Logger('launcher'); + if (window.process && window.process.versions && window.process.versions.electron) { /* jshint node:true */ Launcher = { @@ -128,6 +131,42 @@ if (window.process && window.process.versions && window.process.versions.electro } return true; }, + spawn: function(config) { + var ts = logger.ts(); + var complete = config.complete; + var ps = this.req('child_process').spawn(config.cmd, config.args); + [ps.stdin, ps.stdout, ps.stderr].forEach(function(s) { s.setEncoding('utf-8'); }); + var stderr = ''; + var stdout = ''; + ps.stderr.on('data', function(d) { stderr += d.toString('utf-8'); }); + ps.stdout.on('data', function(d) { stdout += d.toString('utf-8'); }); + ps.on('close', function(code) { + stdout = stdout.trim(); + stderr = stderr.trim(); + var msg = 'spawn ' + config.cmd + ': ' + code + ', ' + logger.ts(ts); + if (code) { + logger.error(msg + '\n' + stdout + '\n' + stderr); + } else { + logger.info(msg + (stdout ? '\n' + stdout : '')); + } + if (complete) { + complete(code ? 'Exit code ' + code : null, stdout, code); + complete = null; + } + }); + ps.on('error', function(err) { + logger.error('spawn error: ' + config.cmd + ', ' + logger.ts(ts), err); + if (complete) { + complete(err); + complete = null; + } + }); + if (config.data) { + ps.stdin.write(config.data); + ps.stdin.end(); + } + return ps; + }, platform: function() { return process.platform; }