Update lib/std,js, lib/chrome.js, lib/autoit.js

This commit is contained in:
Namhyeon Go 2022-02-10 12:09:52 +09:00
parent e7b8aee6ab
commit d609e71915
6 changed files with 159 additions and 268 deletions

View File

@ -2,65 +2,56 @@
// AutoIt (AutoIt3, AutoItX) API // AutoIt (AutoIt3, AutoItX) API
//////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////
function AutoIt_CreateObject = function() {
var obj = null;
try {
obj = CreateObject("AutoItX3.Control");
} catch (e) {
console.error("AutoIt_CreateObject() ->", e.message);
}
return obj;
}
function AutoIt_execScript = function(scriptName) {
var result = null;
var cmd = [
"%PROGRAMFILES(X86)%\\AutoIt3\\AutoIt3.exe",
scriptName + ".au3"
];
if (typeof args !== "undefined") {
cmd = cmd.concat(args);
}
try {
result = SHELL.exec(cmd);
} catch (e) {
console.error("AutoIt_execScript() ->", e.message);
}
return result;
}
function AutoItObject() { function AutoItObject() {
this.interface = null; this.interface = null;
this.create = function() { this.create = function() {
this.interface = AutoIt_CreateObject(); try {
this.interface = CreateObject("AutoItX3.Control");
} catch (e) {
console.error("AutoIt_CreateObject() ->", e.message);
}
}; };
this.execScript = function(scriptName) { this.execScript = function(scriptName) {
return AutoIt_execScript(scriptName); var result = null;
var cmd = [
"%PROGRAMFILES(X86)%\\AutoIt3\\AutoIt3.exe",
scriptName + ".au3"
];
if (typeof args !== "undefined") {
cmd = cmd.concat(args);
}
try {
result = SHELL.exec(cmd);
} catch (e) {
console.error("AutoIt_execScript() ->", e.message);
}
return result;
}; };
this.callFunction = function(functionName, args) { this.callFunction = function(functionName, args) {
console.log("Calling AutoItX function...", functionName);
if (this.interface != null) { if (this.interface != null) {
try { try {
this.interface[functionName].apply(args); //this.interface[functionName].apply(args);
} catch (e) { eval("this.interface." + functionName + "(\"" + args.map(addslashes).join("\", \"") + "\")");
console.error("AutoItObject.callFunction() ->", e.message); } catch (e) {
} console.error("AutoItObject.callFunction() ->", e.message);
}
} else { } else {
console.warn("AutoItX is disabled"); console.warn("AutoItX is disabled");
} }
}; };
this.create(); this.create();
} }
exports.create = function() { exports.create = function() {
return new AutoItObject(); return new AutoItObject();
}; };

View File

@ -506,7 +506,7 @@ var ChromeObject = function(interfaces) {
this.terminate = function() { this.terminate = function() {
try { try {
AutoIt.callFunction("WinKill", [this.getTitle()]); this.oAutoIt.callFunction("WinKill", [this.getTitle()]);
} catch (e) { } catch (e) {
console.error("ChromeObject.terminate() ->", e.message); console.error("ChromeObject.terminate() ->", e.message);
} }
@ -528,10 +528,10 @@ var ChromeObject = function(interfaces) {
// find window by title // find window by title
var pageList = this.getPageList(); var pageList = this.getPageList();
if (pageList.length > 0) { if (pageList.length > 0) {
AutoIt.callFunction("WinActivate", [title]); this.oAutoIt.callFunction("WinActivate", [title]);
} }
} catch (e) { } catch (e) {
console.error("ChromeObject._focus() ->", e.message); console.error("ChromeObject.focus() ->", e.message);
} }
} }
@ -544,22 +544,26 @@ var ChromeObject = function(interfaces) {
this._focus = function() { this._focus = function() {
var title = ""; var title = "";
// get current title try {
var _title = this.getTitle(); // get current title
var _title = this.getTitle();
// if not focused // if not focused
if (_title.indexOf(this.pageId.substring(0, 6)) < 0) { if (_title.indexOf(this.pageId.substring(0, 6)) < 0) {
// save previous title // save previous title
this.title = _title; this.title = _title;
// will be change title // will be change title
title = this.title + " " + this.pageId.substring(0, 6); title = this.title + " " + this.pageId.substring(0, 6);
// change webpage title for focusing window // change webpage title for focusing window
this.setTitle(title); this.setTitle(title);
} else { } else {
title = _title; /// when it is already catch title = _title; /// when it is already catch
} }
} catch (e) {
console.error("ChromeObject._focus() ->", e.message);
}
return title; return title;
}; };
@ -603,7 +607,7 @@ var ChromeObject = function(interfaces) {
var y = this.getRandomInt(0, bY); var y = this.getRandomInt(0, bY);
var w = this.getRandomInt(bX * 3, sX - bX); var w = this.getRandomInt(bX * 3, sX - bX);
var h = this.getRandomInt(bY, sY - bY); var h = this.getRandomInt(bY, sY - bY);
AutoIt.callFunction("WinMove", [title, "", x, y, w, h]); this.oAutoIt.callFunction("WinMove", [title, "", x, y, w, h]);
// blur // blur
this.blur(); this.blur();
@ -619,7 +623,7 @@ var ChromeObject = function(interfaces) {
var h = this.getRandomInt(h1, h2); var h = this.getRandomInt(h1, h2);
var x = this.getRandomInt(0, (sX - w < 0 ? parseInt(sX * 0.2) : (sX - w))); var x = this.getRandomInt(0, (sX - w < 0 ? parseInt(sX * 0.2) : (sX - w)));
var y = this.getRandomInt(0, (sY - h < 0 ? parseInt(sY * 0.2) : (sY - h))); var y = this.getRandomInt(0, (sY - h < 0 ? parseInt(sY * 0.2) : (sY - h)));
AutoIt.callFunction("WinMove", [title, "", x, y, w, h]); this.oAutoIt.callFunction("WinMove", [title, "", x, y, w, h]);
// blur // blur
this.blur(); this.blur();
@ -629,8 +633,8 @@ var ChromeObject = function(interfaces) {
if (this.debuggingPort > 0) { if (this.debuggingPort > 0) {
try { try {
var pos = this.getScreenPosition(); var pos = this.getScreenPosition();
AutoIt.callFunction("MouseMove" [pos.x + 100, pos.y + 100]); this.oAutoIt.callFunction("MouseMove" [pos.x + 100, pos.y + 100]);
AutoIt.callFunction("MouseWheel", ["down", times]); this.oAutoIt.callFunction("MouseWheel", ["down", times]);
} catch (e) { } catch (e) {
console.error("ChromeObject.downMouseWheel() ->", e.message); console.error("ChromeObject.downMouseWheel() ->", e.message);
} }
@ -641,8 +645,8 @@ var ChromeObject = function(interfaces) {
if (this.debuggingPort > 0) { if (this.debuggingPort > 0) {
try { try {
var pos = this.getScreenPosition(); var pos = this.getScreenPosition();
AutoIt.callFunction("MouseMove", [pos.x + 100, pos.y + 100]); this.oAutoIt.callFunction("MouseMove", [pos.x + 100, pos.y + 100]);
AutoIt.callFunction("MouseWheel", ["up", times]); this.oAutoIt.callFunction("MouseWheel", ["up", times]);
} catch (e) { } catch (e) {
console.error("ChromeObject.upMouseWheel() ->", e.message); console.error("ChromeObject.upMouseWheel() ->", e.message);
} }
@ -1069,11 +1073,11 @@ var ChromeObject = function(interfaces) {
}; };
this.sendKeys = function(s) { this.sendKeys = function(s) {
AutoIt.callFunction("Send", [s]); this.oAutoIt.callFunction("Send", [s]);
}; };
this.sendSpaceKey = function() { this.sendSpaceKey = function() {
AutoIt.callFunction("Send", ["{SPACE}"]); this.oAutoIt.callFunction("Send", ["{SPACE}"]);
}; };
this.setValue = function(selector, value, repeat, searchIndex) { this.setValue = function(selector, value, repeat, searchIndex) {
@ -1162,8 +1166,8 @@ var ChromeObject = function(interfaces) {
}; };
this.mouseClick = function(x, y) { this.mouseClick = function(x, y) {
AutoIt.callFunction("MouseMove", [x, y]); this.oAutoIt.callFunction("MouseMove", [x, y]);
AutoIt.callFunction("MouseClick", ["left"]); this.oAutoIt.callFunction("MouseClick", ["left"]);
}; };
this.create(); this.create();

View File

@ -39,5 +39,5 @@ exports.execCommand = function(cmd) {
}; };
exports.runAs = function(cmd) { exports.runAs = function(cmd) {
return exports.execCommand("Start-Process cmd \"/q /c " + SHELL.addslashes(SHELL.makeCmdLine(cmd)) + "\" -Verb RunAs"); return exports.execCommand("Start-Process cmd \"/q /c " + addslashes(SHELL.makeCmdLine(cmd)) + "\" -Verb RunAs");
}; };

43
lib/powershell.js.bak Normal file
View File

@ -0,0 +1,43 @@
////////////////////////////////////////////////////////////////////////
// Powershell API
///////////////////////////////////////////////////////////////////////
var SHELL = require("lib/shell");
exports.VERSIONINFO = "Powershell (powershell.js) version 0.1";
exports.global = global;
exports.require = global.require;
exports.execScript = function(scriptName, args) {
var cmd = [
"powershell.exe",
"-NoProfile",
"-ExecutionPolicy",
"ByPass",
"-nologo",
"-file",
scriptName + ".ps1"
];
if (typeof(cmd) !== "undefined") {
cmd = cmd.concat(args);
}
return SHELL.exec(cmd);
};
exports.execCommand = function(cmd) {
return SHELL.exec([
"powershell.exe",
"-NoProfile",
"-ExecutionPolicy",
"ByPass",
"-nologo",
"-Command",
"& {" + cmd + "}"
]);
};
exports.runAs = function(cmd) {
return exports.execCommand("Start-Process cmd \"/q /c " + SHELL.addslashes(SHELL.makeCmdLine(cmd)) + "\" -Verb RunAs");
};

View File

@ -36,7 +36,7 @@ var ShellObject = function() {
if (typeof(dirname) === "string") { if (typeof(dirname) === "string") {
this.workingDirectory = dirname; this.workingDirectory = dirname;
this.interface.CurrentDirectory = this.workingDirectory; this.interface.CurrentDirectory = this.workingDirectory;
console.log("ShellObject.workingDirectory ->", this.workingDirectory); console.info("ShellObject.workingDirectory ->", this.workingDirectory);
} }
return this; return this;
}; };
@ -53,7 +53,7 @@ var ShellObject = function() {
return cmd.map(function(s) { return cmd.map(function(s) {
if (s == '') { if (s == '') {
return "''"; return "''";
} else if (!/[ "]/g.test(s)) { } else if (!/[ "=]/g.test(s)) {
return s; return s;
} else { } else {
return "\"" + addslashes(s) + "\""; return "\"" + addslashes(s) + "\"";
@ -67,7 +67,7 @@ var ShellObject = function() {
this.createProcess = function(cmd) { this.createProcess = function(cmd) {
try { try {
var c = this.build(cmd); var c = this.build(cmd);
console.log("ShellObject.createProcess() ->", c); console.info("ShellObject.createProcess() ->", c);
return this.interface.Exec(c); return this.interface.Exec(c);
} catch (e) { } catch (e) {
console.error("ShellObject.createProcess() ->", e.message); console.error("ShellObject.createProcess() ->", e.message);
@ -84,7 +84,7 @@ var ShellObject = function() {
//c += " 2>&1"; //c += " 2>&1";
c += " 2> " + stdErrPath; c += " 2> " + stdErrPath;
this.interface.Run(c, 0, true); this.interface.Run(c, 0, true);
console.log("ShellObject.exec() ->", c); console.info("ShellObject.exec() ->", c);
sleep(1); sleep(1);
if (FILE.fileExists(stdOutPath)) { if (FILE.fileExists(stdOutPath)) {
@ -97,7 +97,6 @@ var ShellObject = function() {
FILE.deleteFile(stdErrPath); FILE.deleteFile(stdErrPath);
} }
console.log(c);
//console.log("[stdout] " + stdout); //console.log("[stdout] " + stdout);
//console.log("[stderr] " + stderr); //console.log("[stderr] " + stderr);
@ -107,14 +106,14 @@ var ShellObject = function() {
this.run = function(cmd, fork) { this.run = function(cmd, fork) {
var fork = (typeof(fork) !== "undefined") ? fork : true; var fork = (typeof(fork) !== "undefined") ? fork : true;
var c = "%comspec% /q /c (" + this.build(cmd) + ")"; var c = "%comspec% /q /c (" + this.build(cmd) + ")";
console.log("ShellObject.run() ->", c); console.info("ShellObject.run() ->", c);
this.interface.Run(c, (!this.isVisibleWindow ? 0 : 1), !fork); this.interface.Run(c, (!this.isVisibleWindow ? 0 : 1), !fork);
}; };
this.runAs = function(FN, args) { this.runAs = function(FN, args) {
var oShell = CreateObject("Shell.Application"); var oShell = CreateObject("Shell.Application");
var _args = null; var _args = null;
console.log("ShellObject.runAs() ->", FN); console.info("ShellObject.runAs() ->", FN);
if (typeof(args) !== "undefined") { if (typeof(args) !== "undefined") {
_args = args.join(' '); _args = args.join(' ');
} }
@ -146,7 +145,7 @@ var ShellObject = function() {
}; };
this.release = function() { this.release = function() {
console.log("ShellObject.release() ->", this.currentDirectory); console.info("ShellObject.release() ->", this.currentDirectory);
this.interface.CurrentDirectory = this.currentDirectory; this.interface.CurrentDirectory = this.currentDirectory;
this.interface = null; this.interface = null;
}; };
@ -176,7 +175,7 @@ exports.runVisibleWindow = function(cmd, fork) {
exports.createProcess = function(cmd, workingDirectory) { exports.createProcess = function(cmd, workingDirectory) {
if (typeof(workingDirectory) !== "undefined") { if (typeof(workingDirectory) !== "undefined") {
console.log("Working directory: " + workingDirectory); console.info("Working directory: " + workingDirectory);
} }
return (new ShellObject()).setWorkingDirectory(workingDirectory).createProcess(cmd); return (new ShellObject()).setWorkingDirectory(workingDirectory).createProcess(cmd);
}; };

View File

@ -10,7 +10,6 @@
///////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////
// Polyfills // Polyfills
///////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////
if (!Function.prototype.GetResource) { if (!Function.prototype.GetResource) {
Function.prototype.GetResource = function(ResourceName) { Function.prototype.GetResource = function(ResourceName) {
if (!this.Resources) { if (!this.Resources) {
@ -28,37 +27,16 @@ if (!Function.prototype.GetResource) {
} }
} }
// MS JScript Enumerator to Array
if (!Enumerator.prototype.toArray) { if (!Enumerator.prototype.toArray) {
Enumerator.prototype.toArray = function() { Enumerator.prototype.toArray = function() {
var a = []; var items = [];
for (; !this.atEnd(); this.moveNext()) { for (; !this.atEnd(); this.moveNext()) {
var x = {}; var item = this.item();
var b = new Enumerator(this.item().Properties_); try {
for (; !b.atEnd(); b.moveNext()) { items.push(item);
var c = b.item(); } catch (e) {}
if (typeof c.value !== "unknown") {
try {
x[c.name] = c.value.toString();
} catch (e) {
x[c.name] = c.value;
}
} else {
var i = 0, d = [];
while (true) {
try {
d.push(c.value(i));
i++;
} catch (e) {
break;
}
}
x[c.name] = d;
}
}
a.push(x);
} }
return a; return items;
}; };
} }
@ -66,163 +44,27 @@ if (!Enumerator.prototype.toArray) {
// Global APIs // Global APIs
///////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////
function GetResource(ResourceName) { global.GetResource = function(ResourceName) {
return arguments.callee.caller.GetResource(ResourceName); return arguments.callee.caller.GetResource(ResourceName);
} }
// [lib/std] the time of `sleep()' function is not accuracy #34 global.sleep = function(ms, callback) {
function sleep(ms, callback) { if (typeof(WScript) !== "undefined") {
var handler = null;
var cur = Date.now();
var end = cur + ms;
if (typeof WScript !== "undefined") {
/*
while (cur < end) {
//WScript.Sleep(1);
cur = Date.now();
}
end = Date.now();
*/
WScript.Sleep(ms); WScript.Sleep(ms);
if (typeof callback === "function") if (typeof(callback) === "function") {
callback() callback();
; }
} else if (typeof window !== "undefined") { } else {
if (typeof callback === "function") if (typeof(callback) === "function") {
handler = setTimeout(callback, ms); setTimeout(callback, ms);
;
}
return { 'ms': end, 'handler': handler };
};
function repeat(target, callback, onError) {
switch (typeof target) {
case "number":
case "boolean":
var ms = target;
var i = 0;
var result = null;
var handler = null;
var cur = Date.now();
var end = cur + ms;
if (typeof WScript !== "undefined") {
while (ms === true ? true : (cur < end)) {
try {
if (typeof callback === "function")
var result = callback(i);
if (typeof result === "number") {
i += result;
} else if (result === false) {
break;
}
;
} catch (e) {
if (typeof onError === "function")
if (onError(e) === false)
break
;
;
}
cur = Date.now();
}
end = Date.now();
} else if (typeof window !== "undefined") {
if (typeof callback === "function")
handler = setInterval(callback, ms);
;
}
return { 'ms': end, 'handler': handler };
case "object":
var arr = target;
for (var i = 0; i < arr.length; i++) {
try {
if (typeof callback === "function")
if (callback(i, arr[i]) === false)
break
;
;
} catch (e) {
if (typeof onError === "function")
if (onError(e) === false)
break
;
;
}
}
break;
}
};
function rotate(target, callback, onError) {
var arr = target;
var i = 0;
var stop = false;
while (!stop) {
try {
if (typeof callback === "function") {
stop = callback(i, arr[i]);
} else {
stop = true;
}
} catch (e) {
if (typeof onError === "function")
stop = onError(e);
;
} }
i++;
i = i % keywords.length;
} }
}; };
function range() { global.CHR = function(ord) {
var args = arguments;
var N = [], start, end, step;
switch(args.length) {
case 3:
start = args[0];
end = args[1];
step = args[2];
break;
case 2:
start = args[0];
end = args[1];
step = 1;
break;
case 1:
start = 0;
end = args[0];
step = 1;
break;
}
for (var i = start; i < end; i = i + step)
N.push(i)
;
return N;
};
function CHR(ord) {
return String.fromCharCode(ord); return String.fromCharCode(ord);
}; };
function splitLn(s) {
return s.split(/\r?\n/);
};
///////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////
// Private APIs / Utility functions // Private APIs / Utility functions
///////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////
@ -231,7 +73,7 @@ function splitLn(s) {
// Emulate Server.CreateObject // Emulate Server.CreateObject
///////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////
function CreateObject(progId, serverName, callback) { exports.CreateObject = function(progId, serverName, callback) {
var progIds = []; var progIds = [];
var _CreateObject = function(p, s) { var _CreateObject = function(p, s) {
if (typeof(WScript) !== "undefined") { if (typeof(WScript) !== "undefined") {
@ -260,22 +102,42 @@ function CreateObject(progId, serverName, callback) {
} }
}; };
/////////////////////////////////////////////////////////////////////////////////
// Standard Event Object
///////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////
function StdEvent(eventName) { var StdEvent = function(eventName) {
this.bubbles = false; // Not supported
this.cancelable = false; // Not supported
this.composed = false; // Not supported
this.currentTarget = null; // Not supported
this.defaultPrevented = false; this.defaultPrevented = false;
this.eventPhase = null; // TODO
this.isTrusted = true; // Not supported
this.timeStamp = new Date(); this.timeStamp = new Date();
this.eventName = eventName; this.eventName = eventName;
this.target = null; this.target = null;
// Not supported
this.composedPath = function() {
return null;
};
this.preventDefault = function() { this.preventDefault = function() {
this.defaultPrevented = true; this.defaultPrevented = true;
}; };
// Not supported
this.stopImmediatePropagation = function() {
return null;
};
// Not supported
this.setPropagation = function() {
return null;
};
}; };
function StdEventableObject() { var StdEventableObject = function() {
this.dispatchEvent = function(event) { this.dispatchEvent = function(event) {
event.target = this; event.target = this;
if(('on' + event.eventName) in this) this['on' + event.eventName](event); if(('on' + event.eventName) in this) this['on' + event.eventName](event);
@ -290,15 +152,7 @@ function StdEventableObject() {
}; };
}; };
global.GetResource = GetResource; exports.VERSIONINFO = "Standard Lib (std.js) version 0.3";
global.sleep = sleep;
global.repeat = repeat;
global.rotate = rotate;
global.range = range;
global.CHR = CHR;
global.splitLn = splitLn;
exports.VERSIONINFO = "Standard Lib (std.js) version 0.4";
exports.global = global; exports.global = global;
exports.require = global.require; exports.require = global.require;