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
////////////////////////////////////////////////////////////////////////
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() {
this.interface = null;
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) {
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) {
try {
this.interface[functionName].apply(args);
} catch (e) {
console.error("AutoItObject.callFunction() ->", e.message);
}
try {
//this.interface[functionName].apply(args);
eval("this.interface." + functionName + "(\"" + args.map(addslashes).join("\", \"") + "\")");
} catch (e) {
console.error("AutoItObject.callFunction() ->", e.message);
}
} else {
console.warn("AutoItX is disabled");
}
};
this.create();
this.create();
}
exports.create = function() {
return new AutoItObject();
};
};

View File

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

View File

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

View File

@ -10,7 +10,6 @@
/////////////////////////////////////////////////////////////////////////////////
// Polyfills
/////////////////////////////////////////////////////////////////////////////////
if (!Function.prototype.GetResource) {
Function.prototype.GetResource = function(ResourceName) {
if (!this.Resources) {
@ -28,37 +27,16 @@ if (!Function.prototype.GetResource) {
}
}
// MS JScript Enumerator to Array
if (!Enumerator.prototype.toArray) {
Enumerator.prototype.toArray = function() {
var a = [];
var items = [];
for (; !this.atEnd(); this.moveNext()) {
var x = {};
var b = new Enumerator(this.item().Properties_);
for (; !b.atEnd(); b.moveNext()) {
var c = b.item();
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);
var item = this.item();
try {
items.push(item);
} catch (e) {}
}
return a;
return items;
};
}
@ -66,163 +44,27 @@ if (!Enumerator.prototype.toArray) {
// Global APIs
/////////////////////////////////////////////////////////////////////////////////
function GetResource(ResourceName) {
global.GetResource = function(ResourceName) {
return arguments.callee.caller.GetResource(ResourceName);
}
// [lib/std] the time of `sleep()' function is not accuracy #34
function sleep(ms, callback) {
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();
*/
global.sleep = function(ms, callback) {
if (typeof(WScript) !== "undefined") {
WScript.Sleep(ms);
if (typeof callback === "function")
callback()
;
} else if (typeof window !== "undefined") {
if (typeof callback === "function")
handler = 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);
;
if (typeof(callback) === "function") {
callback();
}
} else {
if (typeof(callback) === "function") {
setTimeout(callback, ms);
}
i++;
i = i % keywords.length;
}
};
function range() {
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) {
global.CHR = function(ord) {
return String.fromCharCode(ord);
};
function splitLn(s) {
return s.split(/\r?\n/);
};
/////////////////////////////////////////////////////////////////////////////////
// Private APIs / Utility functions
/////////////////////////////////////////////////////////////////////////////////
@ -231,7 +73,7 @@ function splitLn(s) {
// Emulate Server.CreateObject
/////////////////////////////////////////////////////////////////////////////////
function CreateObject(progId, serverName, callback) {
exports.CreateObject = function(progId, serverName, callback) {
var progIds = [];
var _CreateObject = function(p, s) {
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.eventPhase = null; // TODO
this.isTrusted = true; // Not supported
this.timeStamp = new Date();
this.eventName = eventName;
this.target = null;
// Not supported
this.composedPath = function() {
return null;
};
this.preventDefault = function() {
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) {
event.target = this;
if(('on' + event.eventName) in this) this['on' + event.eventName](event);
@ -290,15 +152,7 @@ function StdEventableObject() {
};
};
global.GetResource = GetResource;
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.VERSIONINFO = "Standard Lib (std.js) version 0.3";
exports.global = global;
exports.require = global.require;