welsonjs/lib/chrome.js

1412 lines
52 KiB
JavaScript
Raw Normal View History

2023-12-26 16:33:43 +00:00
// chrome.js
// Namhyeon Go <abuse@catswords.net>
// https://github.com/gnh1201/welsonjs
2022-01-08 14:25:11 +00:00
var STD = require("lib/std");
2022-01-09 17:40:18 +00:00
var RAND = require("lib/rand");
2020-11-09 10:06:34 +00:00
var SHELL = require("lib/shell");
2020-11-15 04:31:35 +00:00
var SYS = require("lib/system");
2020-11-18 08:46:26 +00:00
var FILE = require("lib/file");
2021-06-19 20:51:34 +00:00
var HTTP = require("lib/http");
2021-06-19 21:59:41 +00:00
var Websocket = require("lib/websocket");
2022-02-10 01:45:25 +00:00
var AutoIt = require("lib/autoit");
2022-02-14 06:46:59 +00:00
var Toolkit = require("lib/toolkit");
2022-11-25 17:16:46 +00:00
var ExtraMath = require("lib/extramath");
2020-11-09 10:06:34 +00:00
2021-06-20 17:54:46 +00:00
// for remote debugging
var pageEventId = 0;
2022-01-27 05:18:29 +00:00
var ChromeObject = function(interfaces) {
2022-09-18 19:44:58 +00:00
STD.EventTarget.apply(this, arguments); // Set event-attachable object
2022-01-25 17:40:19 +00:00
2022-01-27 05:19:04 +00:00
this.interfaces = (typeof interfaces !== "undefined" ? interfaces : []);
2022-01-25 17:40:19 +00:00
this.vendor = "Chrome";
2024-04-23 18:28:57 +00:00
this.workingDirectory = SYS.getEnvString("PROGRAMFILES") + "\\Google\\:installedDir\\Application";
2021-08-10 11:39:27 +00:00
this.binPath = SYS.getEnvString("PROGRAMFILES") + "\\Google\\:installedDir\\Application\\chrome.exe";
2023-10-30 18:13:02 +00:00
2021-08-10 11:39:27 +00:00
//this.processID = 0;
this.installedDir = "Chrome";
this.profileName = "Default";
this.userDataDir = null;
2022-01-09 17:41:57 +00:00
// proxy
this.isPreventProxy = false;
2021-08-10 11:39:27 +00:00
this.proxy = {
"protocol": "socks5",
"host": "127.0.0.1",
2022-09-05 04:54:47 +00:00
"port": 1080,
"ua": null
2021-08-10 11:39:27 +00:00
};
this.inPrivate = false;
2022-01-09 04:53:40 +00:00
2022-01-09 17:19:41 +00:00
// user agent
2022-01-08 17:04:06 +00:00
this.userAgent = null;
2022-01-09 17:19:41 +00:00
this.userAgents = [];
2021-08-10 14:15:57 +00:00
2021-08-10 11:39:27 +00:00
// dependencies
this.oAutoIt = null;
// for remote debugging
this.debuggingPort = 0;
this.pageId = "";
this.ws = Websocket.create();
this.isAttached = false;
this.pageList = [];
2021-08-10 14:15:57 +00:00
this.title = "";
2021-12-22 07:06:50 +00:00
this.frameIndex = -1;
2021-08-10 11:39:27 +00:00
2022-01-12 15:15:03 +00:00
this.isPreventEvaluate = false;
2022-09-05 05:11:29 +00:00
this.isAppMode = false;
2022-04-04 18:10:23 +00:00
this.baseScreenX = 0;
this.baseScreenY = 0;
2022-01-12 15:15:03 +00:00
2021-08-10 11:39:27 +00:00
this.create = function() {
2022-02-10 01:45:25 +00:00
this.oAutoIt = AutoIt.create();
2022-09-05 05:11:29 +00:00
this.baseScreenX = 1;
this.baseScreenY = (!this.isAppMode ? 84 : 32);
2021-08-10 14:15:57 +00:00
return this;
2021-08-10 11:39:27 +00:00
};
2022-01-12 15:15:03 +00:00
this.setIsPreventEvaluate = function(flag) {
2022-01-12 15:15:45 +00:00
this.isPreventEvaluate = flag;
2022-01-12 15:15:03 +00:00
};
2021-08-10 11:39:27 +00:00
this.setBinPath = function(path) {
this.binPath = path;
return this;
};
this.setProfile = function(profileName, installedDir) {
this.profileName = (profileName == "Default" ? "Chrome" : profileName);
this.setInstalledDir(installedDir);
this.workingDirectory = this.workingDirectory.replace(":installedDir", this.installedDir);
this.binPath = this.binPath.replace(":installedDir", this.installedDir);
//this.binPath = this.binPath.replace(":installedDir", "Chrome");
return this;
};
2021-08-16 20:35:45 +00:00
this.clearProfile = function() {
2024-04-23 18:28:57 +00:00
var FN = this.userDataDir;
2022-01-11 12:32:18 +00:00
while (FILE.folderExists(FN)) {
try {
return FILE.deleteFolder(FN);
} catch (e) {
console.warn("Can not clear the session! Resaon: " + e.message);
console.warn("Retrying clear the profile...");
}
}
};
this.clear = function() {
return this.clearProfile();
2021-08-16 20:35:45 +00:00
};
2021-08-10 11:39:27 +00:00
this.setUserDataDir = function(dirname) {
if (dirname != null) {
2024-04-23 18:33:12 +00:00
this.userDataDir = dirname;
2021-08-10 11:39:27 +00:00
} else {
this.userDataDir = SHELL.getPathOfMyDocuments() + "\\UserData_Chrome_" + this.profileName;
}
return this;
};
this.setInstalledDir = function(dirname) {
if (dirname != null) {
this.installedDir = dirname;
}
return this;
};
this.setProxy = function(obj) {
this.proxy = obj;
return this;
};
this.setProxyProtocol = function(s) {
this.proxy.protocol = s;
return this;
};
this.setProxyHost = function(s) {
this.proxy.host = s;
return this;
};
2021-08-10 14:15:57 +00:00
this.setProxyPort = function(port) {
this.proxy.port = port;
2021-08-10 11:39:27 +00:00
return this;
};
2022-01-09 17:41:57 +00:00
this.setIsPreventProxy = function(flag) {
this.isPreventProxy = flag;
return this;
};
2021-08-10 11:39:27 +00:00
this.setDebuggingPort = function(port) {
this.debuggingPort = port;
2021-08-10 14:15:57 +00:00
console.log("Enabled debugging port:", port);
2021-08-10 11:39:27 +00:00
return this;
2021-08-10 14:15:57 +00:00
};
2021-12-22 07:06:50 +00:00
this.setFrameIndex = function(idx) {
this.frameIndex = idx;
};
2021-08-10 11:39:27 +00:00
this.setPageId = function(s) {
2021-08-10 14:15:57 +00:00
if (s == null) {
var pageList = this.getPageList();
if (pageList instanceof Array && pageList.length > 0) {
this.pageId = pageList[0].id;
}
} else {
this.pageId = s;
}
2021-08-10 11:39:27 +00:00
};
this.createShoutcut = function(url) {
if (!this.userDataDir) {
this.userDataDir = SHELL.getPathOfMyDocuments() + "\\UserData_Chrome_" + this.profileName;
}
var cmd = [
"cscript",
"app.js",
2021-08-10 18:03:57 +00:00
"shoutcut.legacy",
2021-08-10 11:39:27 +00:00
"chrome",
this.installedDir,
];
// disable default browser check
cmd.push("--no-default-browser-check");
// disable popop blocking
cmd.push("--disable-popup-blocking");
2022-01-08 17:09:50 +00:00
2021-11-14 05:20:20 +00:00
// disable 3d
cmd.push("--disable-3d-apis");
// block non-proxyed webrtc traffic
cmd.push("--force-webrtc-ip-handling-policy=disable-non-proxied-udp");
2021-08-10 11:39:27 +00:00
// check incognito mode
if (this.inPrivate) {
cmd.push("--incognito");
}
// check debugging port
if (this.debuggingPort > 0) {
cmd.push("--remote-debugging-port=" + this.debuggingPort);
}
cmd.push("--profile-directory=\"" + this.profileName + "\"");
cmd.push("--user-data-dir=\"" + this.userDataDir + "\"");
cmd.push("\"" + url + "\"");
2021-08-10 18:03:57 +00:00
SHELL.createShoutcut("Chrome Prototype (" + this.installedDir + ")", cmd.join(' '), SYS.getCurrentScriptDirectory());
2021-08-10 11:39:27 +00:00
};
this.setInPrivate = function(flag) {
this.inPrivate = flag;
return this;
};
2022-01-08 17:04:06 +00:00
this.setUserAgent = function(ua) {
this.userAgent = ua;
return this;
};
2022-01-09 17:19:41 +00:00
this.addUserAgent = function(ua) {
this.userAgents.push(ua);
return this;
};
2022-01-09 17:41:57 +00:00
this.addUserAgentsFromFile = function(filename) {
var text = FILE.readFile(filename, FILE.CdoCharset.CdoUTF_8);
2022-01-09 17:41:57 +00:00
var lines = text.split(/\r?\n/);
for (var i = 0; i < lines.length; i++) {
2022-01-11 11:19:59 +00:00
var line = lines[i].trim();
if (line !== "") {
this.userAgents.push(line);
2022-01-09 17:41:57 +00:00
}
}
return this;
};
2021-08-10 11:39:27 +00:00
this.open = function(url) {
this.setProfile(this.profileName, this.installedDir);
2023-12-26 16:33:43 +00:00
2021-08-10 11:39:27 +00:00
// if the file does not exists, Check the 32bit installation folder again
if (!FILE.fileExists(this.binPath)) {
this.workingDirectory = SYS.getEnvString("PROGRAMFILES(X86)") + "\\Google\\Chrome\\Application";
this.binPath = SYS.getEnvString("PROGRAMFILES(X86)") + "\\Google\\:installedDir\\Application\\chrome.exe";
this.setProfile(this.profileName, this.installedDir);
}
// find profile
if (!FILE.fileExists(this.binPath)) {
console.error("ChromeObject.open() -> '" + this.profileName + "' profile does not exists. You have to create it.");
return this;
}
// create shoutcut to desktop
if (this.debuggingPort == 0) {
this.createShoutcut();
}
/*
var process;
while (this.processID == 0) {
try {
if (!this.userDataDir) {
this.userDataDir = SHELL.getPathOfMyDocuments() + "\\UserData_Chrome_" + this.profileName;
}
var shell = SHELL.create().setWorkingDirectory(this.workingDirectory);
var process = shell.createProcess([
"\"" + this.binPath + "\"",
"--profile-directory=\"" + this.profileName + "\"",
"--proxy-server=\"socks5://127.0.0.1:" + this.proxyPort + "\"",
"--user-data-dir=\"" + this.userDataDir + "\"",
"\"" + url + "\""
].join(" "));
sleep(1500);
this.processID = process.ProcessID;
sleep(1500);
shell.release();
} catch (e) {
2021-08-10 17:46:37 +00:00
console.error("ChromeObject.open() ->", e.message);
2021-08-10 11:39:27 +00:00
sleep(1500);
}
}
*/
try {
// get user data directory
if (!this.userDataDir) {
this.userDataDir = SHELL.getPathOfMyDocuments() + "\\UserData_Chrome_" + this.profileName;
}
// connect to shell
var shell = SHELL.create();
shell.setWorkingDirectory(this.workingDirectory);
// initialize
var cmd = [];
// enable inPrivate (incognito) mode
if (this.inPrivate) {
cmd.push("--incognito");
}
// enable debugging port
if (this.debuggingPort > 0) {
cmd.push("--remote-debugging-port=" + this.debuggingPort);
}
2022-01-08 17:09:50 +00:00
2021-08-10 11:39:27 +00:00
// disable default browser check
cmd.push("--no-default-browser-check");
// disable popop blocking
cmd.push("--disable-popup-blocking");
2022-01-08 17:09:50 +00:00
// disable 3D
cmd.push("--disable-3d-apis");
// block non-proxyed webrtc traffic
cmd.push("--force-webrtc-ip-handling-policy=disable-non-proxied-udp");
2022-04-04 18:10:23 +00:00
// disable session crashed bubble (
//cmd.push("--disable-session-crashed-bubble");
// enable restore the last session
//cmd.push("--restore-last-session");
2022-01-08 17:09:50 +00:00
2021-08-10 11:39:27 +00:00
// set profile directory
cmd.push("--profile-directory=\"" + this.profileName + "\"");
2022-01-08 17:09:50 +00:00
2021-08-10 11:39:27 +00:00
// set proxy configuration
2022-01-09 17:41:57 +00:00
if (this.proxy != null && this.isPreventProxy != true) {
2021-08-10 14:15:57 +00:00
console.log("Enabled proxy server:", this.proxy.protocol + "://" + this.proxy.host + ":" + this.proxy.port);
2021-08-10 11:39:27 +00:00
cmd.push("--proxy-server=\"" + this.proxy.protocol + "://" + this.proxy.host + ":" + this.proxy.port + "\"");
2022-09-05 04:54:47 +00:00
if (this.proxy.ua != null) {
this.setUserAgent(this.proxy.ua);
}
2021-08-10 11:39:27 +00:00
}
// set user data directory
cmd.push("--user-data-dir=\"" + this.userDataDir + "\"");
2022-01-08 17:04:06 +00:00
2022-01-09 04:53:40 +00:00
// choice user agent
2022-09-05 05:11:29 +00:00
if (this.userAgent == null && this.userAgents.length > 0) {
2022-01-09 17:19:41 +00:00
this.setUserAgent(RAND.one(this.userAgents));
}
2022-01-09 04:53:40 +00:00
2022-01-09 17:19:41 +00:00
// set user agent
2022-01-08 17:09:50 +00:00
if (this.userAgent != null) {
cmd.push("--user-agent=\"" + this.userAgent + "\"");
}
2022-04-04 18:10:23 +00:00
// set the URL
if (!this.isAppMode) {
cmd.push("\"" + url + "\"");
} else {
cmd.push("--app=\"" + url + "\"");
}
// build the command line
2022-01-09 17:41:57 +00:00
console.log(cmd.join(" "));
2022-01-08 17:09:50 +00:00
2021-08-10 11:39:27 +00:00
// run
shell.runAs(this.binPath, cmd);
sleep(300);
// release shell
shell.release();
} catch (e) {
console.error("ChromeObject.open() -> ", e.message);
sleep(300);
}
return this;
};
this.getPageList = function() {
var pageList = [];
if (this.debuggingPort > 0) {
try {
2022-04-04 18:10:23 +00:00
//var responseText = HTTP.get("http://127.0.0.1:" + this.debuggingPort + "/json");
2022-01-27 05:18:29 +00:00
//console.log(responseText);
2022-04-04 18:10:23 +00:00
//pageList = JSON.parse(responseText);
var pageList = HTTP.create("CURL")
.setDataType("json")
.open("GET", "http://127.0.0.1:" + this.debuggingPort + "/json")
.send()
.responseBody
;
2021-08-10 11:39:27 +00:00
this.pageList = pageList;
return pageList;
} catch (e) {
console.error("ChromeObject.getPageList() ->", e.message);
2022-04-04 18:10:23 +00:00
//return this.getPageList(); // 무한 루프 문제로 주석처리
return pageList; // 바로 넘김
2021-08-10 11:39:27 +00:00
}
} else {
console.error("Remote debugging unavailable");
return pageList;
}
};
this.getPageById = function(id) {
2021-08-10 16:45:56 +00:00
return this.getPageList().find(function(x) {
2021-08-16 16:54:53 +00:00
return (x.id == id);
2021-08-10 11:39:27 +00:00
});
};
2021-08-10 14:15:57 +00:00
2021-08-10 11:39:27 +00:00
this.getPagesByUrl = function(url) {
return this.getPageList().filter(function(x) {
2021-08-10 14:15:57 +00:00
return (x.url.indexOf(url) == 0);
2021-08-10 11:39:27 +00:00
});
};
2021-11-18 04:58:18 +00:00
this.getPagesByUrls = function(urls) {
return this.getPageList().reduce(function(acc, x) {
for (var i = 0; i < urls.length; i++) {
if (x.url.indexOf(urls[i]) == 0) {
acc.push(x);
}
}
return acc;
}, []);
};
2021-08-10 11:39:27 +00:00
this.getPagesByTitle = function(title) {
return this.getPageList().filter(function(x) {
2021-08-10 14:15:57 +00:00
return (x.title.indexOf(title) == 0);
2021-08-10 11:39:27 +00:00
});
};
2021-11-18 04:58:18 +00:00
this.getPagesByTitles = function(titles) {
return this.getPageList().reduce(function(acc, x) {
for (var i = 0; i < titles.length; i++) {
if (x.title.indexOf(titles[i]) == 0) {
acc.push(x);
}
}
return acc;
}, []);
};
2021-12-03 12:12:09 +00:00
this.getParameterValue = function(paramName, defaultValue) {
var paramValue = defaultValue;
2021-11-18 05:25:17 +00:00
var page = this.getPageById(this.pageId);
var params = page.url.split('&');
for (var i = 0; i < params.length; i++) {
if (params[i].indexOf(paramName + '=') == 0) {
paramValue = params[i].substring((paramName + '=').length);
2021-11-18 05:25:54 +00:00
//break; // Do not break because will be get the last value
2021-11-18 05:25:17 +00:00
}
}
return paramValue;
};
2021-11-18 04:58:18 +00:00
2021-08-10 11:39:27 +00:00
this.sendPageRPC = function(method, params) {
var result = null;
try {
if (this.pageId != "") {
result = this.ws.send("ws://127.0.0.1:" + this.debuggingPort + "/devtools/page/" + this.pageId, JSON.stringify({
"id": pageEventId,
"method": method,
"params": params
}));
pageEventId++;
2022-01-27 05:18:29 +00:00
console.log("ChromeObject().sendPageRPC() -> Sent");
2021-08-10 11:39:27 +00:00
} else {
2021-08-10 14:15:57 +00:00
this.setPageId(null);
if (this.pageId != "") {
result = this.sendPageRPC(method, params);
} else {
console.error("Page not found");
2021-08-10 11:39:27 +00:00
}
}
} catch (e) {
2021-08-10 14:15:57 +00:00
console.log("ChromeObject.sendPageRPC() ->", e.message);
2021-08-10 11:39:27 +00:00
}
return result;
};
this.navigate = function(url) {
return this.sendPageRPC("Page.navigate", {
"url": url
});
};
this.navigateEvaluately = function(url) {
return this.evaluate('location.href = "' + url + '"');
};
2021-12-22 07:06:50 +00:00
this.evaluate = function(expression, frameIndex) {
2022-01-12 15:15:03 +00:00
if (this.isPreventEvaluate) return;
2021-12-22 07:06:50 +00:00
var frameIndex = (typeof frameIndex !== "undefined" ? frameIndex : this.frameIndex);
if (frameIndex > -1) {
expression = 'var __getFrame=function(e){return document.getElementsByTagName("frame")[e]},__getDocument=function(){return __getFrame(' + frameIndex + ').contentDocument||__getFrame(' + frameIndex + ').contentWindow.document},__getWindow=function(){return __getFrame(' + frameIndex + ').contentWindow};' + expression;
} else {
expression = 'var __getDocument=function(){return document},__getWindow=function(){return window};' + expression;
}
2021-08-10 11:39:27 +00:00
try {
return this.sendPageRPC("Runtime.evaluate", {
"expression": expression
});
} catch (e) {
2021-08-10 17:46:37 +00:00
console.error("ChromeObject.evaluate() ->", e.message);
2021-08-10 11:39:27 +00:00
}
};
this.getEvaluatedValue = function(expression) {
try {
2021-08-10 14:15:57 +00:00
var responseText = this.evaluate(expression);
2022-01-27 05:18:29 +00:00
console.log(responseText);
2021-10-21 06:53:13 +00:00
var result = JSON.parse(responseText).result.result.value;
if (typeof(result) !== "undefined" && result != null) {
return result;
} else {
return "";
}
2021-08-10 11:39:27 +00:00
} catch (e) {
2021-08-10 17:46:37 +00:00
console.error("ChromeObject.getEvaluatedValue() ->", e.message);
2021-08-11 06:56:24 +00:00
return "";
2021-08-10 11:39:27 +00:00
}
};
2021-08-11 06:55:49 +00:00
this.exit = function() {
2021-08-10 11:39:27 +00:00
return this.sendPageRPC("Browser.close", {});
};
2021-08-11 06:56:24 +00:00
this.close = function() {
var response = this.sendPageRPC("Page.close", {});
this.setPageId(null);
return response;
};
2021-08-10 11:39:27 +00:00
this.terminate = function() {
try {
this.oAutoIt.callFunction("WinKill", [this.getTitle()]);
2021-08-10 11:39:27 +00:00
} catch (e) {
2021-08-10 17:46:37 +00:00
console.error("ChromeObject.terminate() ->", e.message);
2021-08-10 11:39:27 +00:00
}
};
this.focus = function() {
2021-08-16 15:14:01 +00:00
var title = "";
2021-08-10 11:39:27 +00:00
if (this.debuggingPort > 0) {
try {
2021-08-10 14:15:57 +00:00
// set page id
if (this.pageId == "") {
this.setPageId(null);
}
2021-08-11 07:31:27 +00:00
2022-01-25 18:46:17 +00:00
// calling _focus()
2022-01-25 17:40:19 +00:00
title = this._focus();
2021-08-10 11:39:27 +00:00
// find window by title
var pageList = this.getPageList();
if (pageList.length > 0) {
this.oAutoIt.callFunction("WinActivate", [title]);
2021-08-10 11:39:27 +00:00
}
} catch (e) {
console.error("ChromeObject.focus() ->", e.message);
2021-08-10 11:39:27 +00:00
}
}
2021-08-16 15:14:01 +00:00
2022-01-08 17:04:06 +00:00
// calling `onfocus` event
this.dispatchEvent(new STD.Event("focus"));
2022-01-08 14:24:43 +00:00
2021-08-16 15:14:01 +00:00
return title;
2021-08-10 11:39:27 +00:00
};
2022-01-25 17:40:19 +00:00
this._focus = function() {
var title = "";
2022-02-10 04:25:58 +00:00
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;
// 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
}
} catch (e) {
console.error("ChromeObject._focus() ->", e.message);
}
2022-01-25 17:40:19 +00:00
return title;
};
2021-08-11 07:31:27 +00:00
this.blur = function() {
2021-08-16 15:14:01 +00:00
return this.setTitle(this.title);
2021-08-11 07:31:27 +00:00
};
2021-08-10 14:15:57 +00:00
this.getScrollHeight = function(selector) {
2021-12-22 07:06:50 +00:00
return parseInt(this.getEvaluatedValue('__getDocument().querySelector("' + selector + '").scrollHeight'));
2021-08-10 11:39:27 +00:00
};
this.focusWithoutActivate = function() {
if (this.debuggingPort > 0) {
try {
// if page ID is empty
if (this.pageId == "") {
var pageList = this.getPageList();
if (pageList instanceof Array && pageList.length > 0) {
this.pageId = pageList[0].id;
}
}
// change webpage title for focusing window
this.setTitle(this.pageId);
} catch (e) {
2021-08-10 17:46:37 +00:00
console.error("ChromeObject.focusWithoutActivate() ->", e.message);
2021-08-10 11:39:27 +00:00
}
}
};
2021-08-16 15:14:01 +00:00
this.autoAdjustByScreen = function(sX, sY, divX, divY) {
2021-11-14 04:16:05 +00:00
// focus
2021-08-16 15:14:01 +00:00
var title = this.focus();
sleep(300);
2021-08-10 11:39:27 +00:00
// adjust window position and size
2021-08-16 15:14:01 +00:00
var bX = Math.floor(sX / divX);
var bY = Math.floor(sY / divY);
2021-08-10 11:39:27 +00:00
var x = this.getRandomInt(0, bX);
var y = this.getRandomInt(0, bY);
var w = this.getRandomInt(bX * 3, sX - bX);
var h = this.getRandomInt(bY, sY - bY);
this.oAutoIt.callFunction("WinMove", [title, "", x, y, w, h]);
2022-02-10 04:25:58 +00:00
2021-11-14 04:16:05 +00:00
// blur
2021-08-16 15:14:01 +00:00
this.blur();
};
this.autoAdjustByWindow = function(sX, sY, w1, w2, h1, h2) {
// catch focus
var title = this.focus();
sleep(300);
// adjust window position and size
var w = this.getRandomInt(w1, w2);
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)));
this.oAutoIt.callFunction("WinMove", [title, "", x, y, w, h]);
2021-08-16 15:14:01 +00:00
2021-11-14 04:16:05 +00:00
// blur
2021-08-16 15:14:01 +00:00
this.blur();
2021-08-10 11:39:27 +00:00
};
this.downMouseWheel = function(times) {
if (this.debuggingPort > 0) {
try {
var pos = this.getScreenPosition();
this.oAutoIt.callFunction("MouseMove" [pos.x + 100, pos.y + 100]);
this.oAutoIt.callFunction("MouseWheel", ["down", times]);
2021-08-10 11:39:27 +00:00
} catch (e) {
2021-08-10 17:46:37 +00:00
console.error("ChromeObject.downMouseWheel() ->", e.message);
2021-08-10 11:39:27 +00:00
}
}
};
this.upMouseWheel = function(times) {
if (this.debuggingPort > 0) {
try {
var pos = this.getScreenPosition();
this.oAutoIt.callFunction("MouseMove", [pos.x + 100, pos.y + 100]);
this.oAutoIt.callFunction("MouseWheel", ["up", times]);
2021-08-10 11:39:27 +00:00
} catch (e) {
2021-08-10 17:46:37 +00:00
console.error("ChromeObject.upMouseWheel() ->", e.message);
2021-08-10 11:39:27 +00:00
}
}
};
this.setTitle = function(title) {
2021-08-10 14:15:57 +00:00
var i = 0, repeat = 2;
2021-08-10 11:39:27 +00:00
if (this.debuggingPort > 0) {
2021-08-10 14:15:57 +00:00
while (i < repeat) {
2021-12-26 13:59:02 +00:00
this.evaluate('document.title = ' + this.__escape(title));
2021-08-10 14:15:57 +00:00
i++;
2021-08-10 11:39:27 +00:00
}
}
};
this.getTitle = function() {
if (this.debuggingPort > 0) {
return this.getEvaluatedValue('document.title');
}
};
this.getScreenPosition = function() {
2021-12-22 07:06:50 +00:00
var result = this.getEvaluatedValue('(function() { return [__getWindow().screenX, __getWindow().screenY].join(","); })();');
2021-08-10 11:39:27 +00:00
var pos = result.split(',');
return {
"x": parseInt(pos[0]),
"y": parseInt(pos[1])
};
};
2021-12-03 12:12:09 +00:00
this.getElementPosition = function(selector, startIndex) {
var startIndex = (typeof startIndex !== 'undefined' ? startIndex : 0);
var result;
var pos = -1;
if (startIndex > 0) {
2021-12-22 07:06:50 +00:00
result = this.getEvaluatedValue('(function(k) { var rect = __getDocument().querySelectorAll("' + selector + '")[k].getBoundingClientRect(); return [parseInt(rect.left), parseInt(rect.top), parseInt(__getWindow().pageXOffset + rect.left), parseInt(__getWindow().pageYOffset + rect.top), parseInt(rect.width), parseInt(rect.height)].join(","); })(' + startIndex + ');');
2021-12-03 12:12:09 +00:00
} else {
2021-12-22 07:06:50 +00:00
result = this.getEvaluatedValue('(function() { var rect = __getDocument().querySelector("' + selector + '").getBoundingClientRect(); return [parseInt(rect.left), parseInt(rect.top), parseInt(__getWindow().pageXOffset + rect.left), parseInt(__getWindow().pageYOffset + rect.top), parseInt(rect.width), parseInt(rect.height)].join(","); })();');
2021-12-03 12:12:09 +00:00
}
pos = result.split(',');
2021-08-10 14:15:57 +00:00
if (pos.length == 6) {
return {
"x": parseInt(pos[0]),
"y": parseInt(pos[1]),
"a": parseInt(pos[2]),
"b": parseInt(pos[3]),
2021-11-14 04:16:05 +00:00
"w": parseInt(pos[4]),
"h": parseInt(pos[5])
2021-08-10 14:15:57 +00:00
};
2021-08-10 16:45:56 +00:00
} else {
return {
"x": -1,
"y": -1,
"a": -1,
"b": -1,
"g": -1,
"d": -1
};
2021-08-11 06:56:24 +00:00
}
2021-08-10 14:15:57 +00:00
};
2021-12-03 17:17:01 +00:00
this.getElementPositionByText = function(selector, searchText) {
2021-12-22 07:06:50 +00:00
var result;
2021-12-03 17:17:01 +00:00
var pos = -1;
var s = '(function() {'
2021-12-22 07:06:50 +00:00
+ ' var element = Object.values(__getDocument().querySelectorAll("' + selector + '")).find(function(x) {'
2021-12-26 13:59:02 +00:00
+ ' return (x.innerText.indexOf(' + this.__escape() + ') > -1);'
2021-12-03 17:17:01 +00:00
+ ' });'
+ ' if (element) {'
+ ' var rect = element.getBoundingClientRect();'
2021-12-22 07:06:50 +00:00
+ ' return [parseInt(rect.left), parseInt(rect.top), parseInt(__getWindow().pageXOffset + rect.left), parseInt(__getWindow().pageYOffset + rect.top), parseInt(rect.width), parseInt(rect.height)].join(",");'
2021-12-03 17:17:01 +00:00
+ ' }'
+ '})()'
;
2021-12-22 07:06:50 +00:00
result = this.getEvaluatedValue(s);
2021-12-03 17:17:01 +00:00
pos = result.split(',');
if (pos.length == 6) {
return {
"x": parseInt(pos[0]),
"y": parseInt(pos[1]),
"a": parseInt(pos[2]),
"b": parseInt(pos[3]),
"w": parseInt(pos[4]),
"h": parseInt(pos[5])
};
} else {
return {
"x": -1,
"y": -1,
"a": -1,
"b": -1,
"g": -1,
"d": -1
};
}
};
2021-09-27 00:28:44 +00:00
this.getNestedElementPosition = function(selector, subSelector, searchText, startIndex) {
2021-08-10 16:45:56 +00:00
var s = '';
2021-11-14 04:16:05 +00:00
var startIndex = (typeof startIndex !== 'undefined' ? startIndex : 0);
2021-08-20 02:11:37 +00:00
2021-11-14 04:16:05 +00:00
if (searchText.indexOf(':tokenize(') == 0) {
searchText = searchText.substring(searchText.indexOf('(') + 1, searchText.lastIndexOf(')'));
2021-08-11 06:56:24 +00:00
s += '(function() {'
2021-12-22 07:06:50 +00:00
+ ' var elements = Object.values(__getDocument().querySelectorAll("' + selector + '")).filter(function(x) {'
2022-08-29 02:24:03 +00:00
+ ' var el = ' + (subSelector == ':self' ? 'x;' : 'x.querySelector("' + subSelector + '");')
2021-12-26 13:59:02 +00:00
+ ' var keywords = ' + this.__escape(searchText) + '.trim().split(" ");'
2021-08-31 04:53:11 +00:00
+ ' var text = el instanceof HTMLElement ? [el.innerText, el.getAttribute("aria-label"), el.getAttribute("class")].join(" ") : "";'
2021-08-29 00:21:58 +00:00
+ ' return (text.split(" ").filter(function(w) { return keywords.indexOf(w) > -1; }).length >= keywords.length);'
2021-09-27 00:28:44 +00:00
+ ' ' + (startIndex > 0 ? '}).slice(' + startIndex + ');' : '});')
2021-08-11 06:56:24 +00:00
+ ' if (elements.length > 0) {'
2021-12-03 17:17:01 +00:00
+ ' var element = elements[0];'
+ ' var rect = element.getBoundingClientRect();'
+ ' var elClassName = "welsonjs_" + parseInt(Math.random() * 1000000000);'
+ ' element.setAttribute("class", element.getAttribute("class") + " " + elClassName);'
2021-12-22 07:06:50 +00:00
+ ' return [parseInt(rect.left), parseInt(rect.top), parseInt(__getWindow().pageXOffset + rect.left), parseInt(__getWindow().pageYOffset + rect.top), parseInt(rect.width), parseInt(rect.height), "." + elClassName].join(",");'
2021-08-11 06:56:24 +00:00
+ ' } else {'
+ ' return "";'
+ ' }'
+ '})()'
;
2021-12-03 12:12:09 +00:00
}
2023-12-26 16:33:43 +00:00
else if (searchText.indexOf(':text(') == 0) {
this.evaluate(ExtraMath.export_measureSimilarity());
2022-11-25 17:16:46 +00:00
searchText = searchText.substring(searchText.indexOf('(') + 1, searchText.lastIndexOf(')'));
s += '(function() {'
+ ' var elements = Object.values(__getDocument().querySelectorAll("' + selector + '")).filter(function(x) {'
+ ' var el = ' + (subSelector == ':self' ? 'x;' : 'x.querySelector("' + subSelector + '");')
+ ' var searchText = ' + this.__escape(searchText) + '.trim();'
+ ' var text = el instanceof HTMLElement ? el.innerText : "";'
+ ' return ExtraMath.measureSimilarity(text, searchText) >= 0.9;'
+ ' ' + (startIndex > 0 ? '}).slice(' + startIndex + ');' : '});')
+ ' if (elements.length > 0) {'
+ ' var element = elements[0];'
+ ' var rect = element.getBoundingClientRect();'
+ ' var elClassName = "welsonjs_" + parseInt(Math.random() * 1000000000);'
+ ' element.setAttribute("class", element.getAttribute("class") + " " + elClassName);'
+ ' return [parseInt(rect.left), parseInt(rect.top), parseInt(__getWindow().pageXOffset + rect.left), parseInt(__getWindow().pageYOffset + rect.top), parseInt(rect.width), parseInt(rect.height), "." + elClassName].join(",");'
+ ' } else {'
+ ' return "";'
+ ' }'
+ '})()'
;
}
2021-12-03 12:12:09 +00:00
else if (searchText.indexOf(':p(') == 0) {
2021-09-27 00:28:44 +00:00
var p = parseFloat(searchText.substring(searchText.indexOf('(') + 1, searchText.lastIndexOf(')')));
2021-08-16 20:12:02 +00:00
if (p > 0) {
s += '(function() {'
2021-12-22 07:06:50 +00:00
+ ' var elements = Object.values(__getDocument().querySelectorAll("' + selector + '")).filter(function(x) {'
2021-08-16 20:12:02 +00:00
+ ' return (Math.random() < ' + p + ');'
2021-11-14 04:16:05 +00:00
+ ' ' + (startIndex > 0 ? '}).slice(' + startIndex + ');' : '});')
2021-08-16 20:12:02 +00:00
+ ' if (elements.length > 0) {'
2021-12-03 12:12:09 +00:00
+ ' var element = elements[0];'
+ ' var rect = element.getBoundingClientRect();'
+ ' var elClassName = "welsonjs_" + parseInt(Math.random() * 1000000000);'
+ ' element.setAttribute("class", element.getAttribute("class") + " " + elClassName);'
2021-12-22 07:06:50 +00:00
+ ' return [parseInt(rect.left), parseInt(rect.top), parseInt(__getWindow().pageXOffset + rect.left), parseInt(__getWindow().pageYOffset + rect.top), parseInt(rect.width), parseInt(rect.height), "." + elClassName].join(",");'
2021-08-16 20:12:02 +00:00
+ ' } else {'
+ ' return "";'
+ ' }'
+ '})()'
;
} else {
s += '(function() {'
2021-12-22 07:06:50 +00:00
+ ' var elements = Object.values(__getDocument().querySelectorAll("' + selector + '"));'
2021-11-14 04:16:05 +00:00
+ ' ' + (startIndex > 0 ? 'elements = elements.slice(' + startIndex + ');' : '')
2021-08-16 20:12:02 +00:00
+ ' if (elements.length > 0) {'
2021-08-20 02:11:37 +00:00
+ ' var k = Math.floor(Math.random() * elements.length);'
2021-12-03 12:12:09 +00:00
+ ' var element = elements[k];'
+ ' var rect = element.getBoundingClientRect();'
+ ' var elClassName = "welsonjs_" + parseInt(Math.random() * 1000000000);'
+ ' element.setAttribute("class", element.getAttribute("class") + " " + elClassName);'
2021-12-22 07:06:50 +00:00
+ ' return [parseInt(rect.left), parseInt(rect.top), parseInt(__getWindow().pageXOffset + rect.left), parseInt(__getWindow().pageYOffset + rect.top), parseInt(rect.width), parseInt(rect.height), "." + elClassName].join(",");'
2021-08-16 20:12:02 +00:00
+ ' } else {'
+ ' return "";'
+ ' }'
+ '})()'
;
}
2021-12-03 12:12:09 +00:00
}
else {
2021-09-27 00:28:44 +00:00
s += '(function() {'
2021-12-22 07:06:50 +00:00
+ ' var elements = Object.values(__getDocument().querySelectorAll("' + selector + '")).filter(function(x) {'
2022-08-29 02:24:03 +00:00
+ ' var el = ' + (subSelector == ':self' ? 'x;' : 'x.querySelector("' + subSelector + '");')
2021-12-26 13:59:02 +00:00
+ ' var searchText = ' + this.__escape(searchText) + '.trim();'
2021-09-27 00:28:44 +00:00
+ ' var text = el instanceof HTMLElement ? [el.innerText, el.getAttribute("aria-label"), el.getAttribute("class")].join(" ") : "";'
+ ' return (text.indexOf(searchText) > -1);'
+ ' ' + (startIndex > 0 ? '}).slice(' + startIndex + ');' : '});')
+ ' if (elements.length > 0) {'
2021-12-03 17:17:01 +00:00
+ ' var element = elements[0];'
+ ' var rect = element.getBoundingClientRect();'
+ ' var elClassName = "welsonjs_" + parseInt(Math.random() * 1000000000);'
+ ' element.setAttribute("class", element.getAttribute("class") + " " + elClassName);'
2021-12-22 07:06:50 +00:00
+ ' return [parseInt(rect.left), parseInt(rect.top), parseInt(__getWindow().pageXOffset + rect.left), parseInt(__getWindow().pageYOffset + rect.top), parseInt(rect.width), parseInt(rect.height), "." + elClassName].join(",");'
2021-09-27 00:28:44 +00:00
+ ' } else {'
+ ' return "";'
+ ' }'
+ '})()'
2021-11-14 04:16:05 +00:00
;
2021-08-11 06:56:24 +00:00
}
2021-08-10 16:45:56 +00:00
2021-08-10 14:15:57 +00:00
var result = this.getEvaluatedValue(s);
var pos = result.split(',');
2021-12-03 12:12:09 +00:00
if (pos.length == 7) {
2021-08-10 14:15:57 +00:00
return {
"x": parseInt(pos[0]),
"y": parseInt(pos[1]),
"a": parseInt(pos[2]),
"b": parseInt(pos[3]),
2021-09-27 00:28:44 +00:00
"w": parseInt(pos[4]),
2021-12-03 12:12:09 +00:00
"h": parseInt(pos[5]),
"s": pos[6]
2021-08-10 14:15:57 +00:00
};
2021-08-10 16:45:56 +00:00
} else {
return {
"x": -1,
"y": -1,
"a": -1,
"b": -1,
2021-09-27 00:28:44 +00:00
"w": -1,
2021-12-03 12:12:09 +00:00
"h": -1,
"s": ""
2021-08-10 16:45:56 +00:00
};
2021-08-11 06:56:24 +00:00
}
2021-08-10 14:15:57 +00:00
};
2021-12-03 12:12:09 +00:00
2021-09-27 00:28:44 +00:00
this.triggerEventOnNestedElement = function(eventName, selector, subSelector, searchText, startIndex) {
var s = '';
2021-11-14 04:16:05 +00:00
var startIndex = (typeof startIndex !== 'undefined' ? startIndex : 0);
2021-09-27 00:28:44 +00:00
2021-11-14 04:16:05 +00:00
if (searchText.indexOf(':tokenize(') == 0) {
searchText = searchText.substring(searchText.indexOf('(') + 1, searchText.lastIndexOf(')'));
2021-09-27 00:28:44 +00:00
s += '(function() {'
2021-12-22 07:06:50 +00:00
+ ' var elements = Object.values(__getDocument().querySelectorAll("' + selector + '")).filter(function(x) {'
2022-08-29 02:24:03 +00:00
+ ' var el = ' + (subSelector == ':self' ? 'x;' : 'x.querySelector("' + subSelector + '");')
2021-12-26 13:59:02 +00:00
+ ' var keywords = ' + this.__escape(searchText) + '.trim().split(" ");'
2021-09-27 00:28:44 +00:00
+ ' var text = el instanceof HTMLElement ? [el.innerText, el.getAttribute("aria-label"), el.getAttribute("class")].join(" ") : "";'
+ ' return (text.split(" ").filter(function(w) { return keywords.indexOf(w) > -1; }).length >= keywords.length);'
+ ' ' + (startIndex > 0 ? '}).slice(' + startIndex + ');' : '});')
+ ' if (elements.length > 0) {'
+ ' elements[0].' + (eventName == 'click' ? 'click()' : 'dispatchEvent(new Event("' + eventName + '"))') + ';'
+ ' }'
+ '})()'
;
2021-11-14 04:16:05 +00:00
} else if (searchText.indexOf(':p(') == 0) {
2021-09-27 00:28:44 +00:00
var p = parseFloat(searchText.substring(searchText.indexOf('(') + 1, searchText.lastIndexOf(')')));
if (p > 0) {
s += '(function() {'
2021-12-22 07:06:50 +00:00
+ ' var elements = Object.values(__getDocument().querySelectorAll("' + selector + '")).filter(function(x) {'
2021-09-27 00:28:44 +00:00
+ ' return (Math.random() < ' + p + ');'
2021-11-14 04:16:05 +00:00
+ ' ' + (startIndex > 0 ? '}).slice(' + startIndex + ');' : '});')
2021-09-27 00:28:44 +00:00
+ ' if (elements.length > 0) {'
+ ' elements[0].' + (eventName == 'click' ? 'click()' : 'dispatchEvent(new Event("' + eventName + '"))') + ';'
+ ' }'
+ '})()'
;
} else {
s += '(function() {'
2021-12-22 07:06:50 +00:00
+ ' var elements = Object.values(__getDocument().querySelectorAll("' + selector + '"));'
2021-11-14 04:16:05 +00:00
+ ' ' + (startIndex > 0 ? 'elements = elements.slice(' + startIndex + ');' : '')
2021-09-27 00:28:44 +00:00
+ ' if (elements.length > 0) {'
+ ' var k = Math.floor(Math.random() * elements.length);'
+ ' elements[k].' + (eventName == 'click' ? 'click()' : 'dispatchEvent(new Event("' + eventName + '"))') + ';'
+ ' }'
+ '})()'
;
}
} else {
s += '(function() {'
2021-12-22 07:06:50 +00:00
+ ' var elements = Object.values(__getDocument().querySelectorAll("' + selector + '")).filter(function(x) {'
2022-08-29 02:24:03 +00:00
+ ' var el = ' + (subSelector == ':self' ? 'x;' : 'x.querySelector("' + subSelector + '");')
2021-12-26 13:59:02 +00:00
+ ' var searchText = ' + this.__escape(searchText) + '.trim();'
2021-09-27 00:28:44 +00:00
+ ' var text = el instanceof HTMLElement ? [el.innerText, el.getAttribute("aria-label"), el.getAttribute("class")].join(" ") : "";'
+ ' return (text.indexOf(searchText) > -1);'
+ ' ' + (startIndex > 0 ? '}).slice(' + startIndex + ');' : '});')
+ ' if (elements.length > 0) {'
+ ' elements[0].' + (eventName == 'click' ? 'click()' : 'dispatchEvent(new Event("' + eventName + '"))') + ';'
+ ' }'
+ '})()'
2021-11-14 04:16:05 +00:00
;
2021-09-27 00:28:44 +00:00
}
return this.evaluate(s);
};
2021-08-20 07:53:12 +00:00
this.getNestedElementIndex = function(selector, subSelector, searchText) {
var s = '';
s += '(function() {'
2021-12-22 07:06:50 +00:00
+ ' var elements = Object.values(__getDocument().querySelectorAll("' + selector + '"));'
2021-08-20 07:53:12 +00:00
+ ' var result = -1;'
+ ' for (var i = 0; i < elements.length; i++) {'
2021-12-26 13:59:02 +00:00
+ ' if (x.querySelector("' + subSelector + '").innerText.indexOf(' + this.__escape(searchText) + ') > -1) {'
2021-08-20 07:53:12 +00:00
+ ' result = i;'
+ ' break;'
+ ' }'
+ ' }'
+ ' return result;'
+ '})()'
;
return parseInt(this.getEvaluatedValue(s));
};
2022-01-02 09:22:45 +00:00
this.getElementCount = function(selector) {
return this.getEvaluatedValue('document.querySelectorAll("' + selector + '").length');
};
2021-08-10 11:39:27 +00:00
this.getPageHeight = function() {
var height = 0;
if (this.debuggingPort > 0) {
2021-12-22 07:06:50 +00:00
var result = this.getEvaluatedValue('(function(obj) { return Math.max(obj.scrollHeight, obj.clientHeight); })(__getDocument().querySelector("html"))');
2021-08-10 11:39:27 +00:00
height = parseInt(result);
}
return height;
};
this.setIsAttached = function(isAttached) {
this.isAttached = isAttached;
return this;
};
2021-11-14 04:16:05 +00:00
2021-08-10 11:39:27 +00:00
this.getRandomInt = function(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
};
this.checkDebuggingPort = function() {
var isChecked = false;
var isDone = false;
while (!isDone) {
try {
if (this.debuggingPort > 0) {
var result = SHELL.exec("netstat -ano | findstr :" + this.debuggingPort);
if (result.indexOf(":" + this.debuggingPort) > -1) {
isChecked = true;
}
}
isDone = true;
} catch(e) {
sleep(1);
2021-08-10 17:46:37 +00:00
console.error("ChromeObject.checkDebuggingPort() ->", e.message);
2021-08-10 11:39:27 +00:00
}
}
return isChecked;
};
this.getCurrentUrl = function() {
var page = this.getPageById(this.pageId);
return page.url;
};
this.getCurrentDomain = function() {
2021-12-22 07:06:50 +00:00
return this.getEvaluatedValue('__getDocument().domain') || '';
2021-08-10 11:39:27 +00:00
};
this.triggerEvent = function(eventName, selector) {
if (selector.indexOf(':p(') < 0) {
if (eventName == 'click') {
2021-12-22 07:06:50 +00:00
return this.evaluate('__getDocument().querySelector("' + selector + '").click()');
2021-08-10 11:39:27 +00:00
} else {
2021-12-22 07:06:50 +00:00
return this.evaluate('__getDocument().querySelector("' + selector + '").dispatchEvent(new Event("' + eventName + '"))');
2021-08-10 11:39:27 +00:00
}
} else {
2021-08-11 06:55:49 +00:00
var p = parseFloat(selector.substring(selector.indexOf('(') + 1, selector.indexOf(')')));
2021-08-11 06:56:24 +00:00
var _selector = selector.substring(0, selector.indexOf(':'));
2021-08-16 15:04:26 +00:00
if (p > 0) {
2021-12-22 07:06:50 +00:00
return this.evaluate('(function(obj, p) { var element = Object.values(obj).find(function() { return (Math.random() < p); }); if(element) element.click(); })(__getDocument().querySelectorAll("' + _selector + '"), ' + p + ')');
2021-08-16 15:04:26 +00:00
} else {
2021-12-22 07:06:50 +00:00
return this.evaluate('(function(obj) { var elements = Object.values(obj); var element = elements[Math.floor(Math.random() * elements.length)]; if(element) element.click(); })(__getDocument().querySelectorAll("' + _selector + '"))');
2021-08-16 15:04:26 +00:00
}
2021-08-10 11:39:27 +00:00
}
};
2022-02-10 02:16:12 +00:00
2021-08-11 06:55:49 +00:00
this.triggerEventByFind = function(eventName, selector, searchText) {
var s = '(function() {'
2021-12-22 07:06:50 +00:00
+ ' var element = Object.values(__getDocument().querySelectorAll("' + selector + '")).find(function(x) {'
2021-12-26 13:59:02 +00:00
+ ' return (x.innerText.indexOf(' + this.__escape(searchText) + ') > -1);'
2021-08-11 06:55:49 +00:00
+ ' });'
+ ' if (element) {'
+ ' element.' + (eventName == 'click' ? 'click()' : 'dispatchEvent(new Event("' + eventName + '"))') + ';'
+ ' }'
+ '})()'
;
return this.evaluate(s);
};
this.triggerEventOnNestedFind = function(eventName, selector, subSelector, searchText) {
var s = '(function() {'
2021-12-22 07:06:50 +00:00
+ ' var element = Object.values(__getDocument().querySelectorAll("' + selector + '")).find(function(x) {'
2021-12-26 13:59:02 +00:00
+ ' return (x.querySelector("' + subSelector + '").innerText.indexOf(' + this.__escape(searchText) + ') > -1);'
2021-08-11 06:55:49 +00:00
+ ' });'
+ ' if (element) {'
+ ' element.' + (eventName == 'click' ? 'click()' : 'dispatchEvent(new Event("' + eventName + '"))') + ';'
+ ' }'
+ '})()'
;
return this.evaluate(s);
2021-08-10 11:39:27 +00:00
};
2021-08-10 14:15:57 +00:00
this.scrollTo = function(x, y) {
2021-12-22 07:06:50 +00:00
return this.evaluate('__getWindow().scrollTo(parseInt(' + x + '), parseInt(' + y + '))');
2021-08-10 14:15:57 +00:00
};
2021-08-10 11:39:27 +00:00
this.scrollBy = function(dx, dy) {
2021-12-22 07:06:50 +00:00
return this.evaluate('__getWindow().scrollBy(parseInt(' + dx + '), parseInt(' + dy + '))');
2021-08-10 11:39:27 +00:00
};
2021-08-11 06:55:49 +00:00
this.scrollToElement = function(selector, dx, dy) {
2021-12-22 07:06:50 +00:00
return this.evaluate('(function(rect, dx, dy) { __getWindow().scrollTo(rect.x + dx, rect.y + dy); })(__getDocument().querySelector("' + selector + '").getBoundingClientRect(), parseInt("' + dx + '"), parseInt("' + dy + '"))');
2021-08-11 06:55:49 +00:00
};
2021-08-10 11:39:27 +00:00
this.reload = function() {
2021-11-14 04:16:05 +00:00
//return this.sendPageRPC("Page.reload", {});
2021-12-22 07:06:50 +00:00
return this.evaluate("__getWindow().reload()");
2021-08-10 11:39:27 +00:00
};
this.hasClass = function(seletctor, className) {
try {
2021-12-22 07:06:50 +00:00
var result = this.getEvaluatedValue('__getDocument().querySelector("' + seletctor + '").getAttribute("class")');
2021-08-10 11:39:27 +00:00
if (typeof(result) === "string") {
return (result.split(' ').indexOf(className) > -1);
} else {
return false;
}
} catch (e) {
2021-08-10 17:46:37 +00:00
console.error("ChromeObject.hasClass() ->", e.message);
2021-08-10 11:39:27 +00:00
}
};
this.getAttribute = function(selector, attributeName) {
2021-12-22 07:06:50 +00:00
return this.getEvaluatedValue('__getDocument().querySelector("' + selector + '").getAttribute("' + attributeName + '")');
2021-08-10 11:39:27 +00:00
};
2022-02-10 02:16:12 +00:00
this.sendKeys = function(s) {
this.oAutoIt.callFunction("Send", [s]);
2022-02-10 02:16:12 +00:00
};
2021-08-10 11:39:27 +00:00
this.sendSpaceKey = function() {
this.oAutoIt.callFunction("Send", ["{SPACE}"]);
2021-08-10 11:39:27 +00:00
};
2021-11-14 04:16:05 +00:00
this.setValue = function(selector, value, repeat, searchIndex) {
2021-12-26 13:59:02 +00:00
var s = value,
2021-11-14 04:16:05 +00:00
i = 0,
2021-12-22 07:06:50 +00:00
searchIndex = (typeof searchIndex !== "undefined" ? searchIndex : 0),
repeat = (typeof repeat !== "undefined" ? repeat : 1)
2021-11-14 04:16:05 +00:00
;
2021-12-22 07:06:50 +00:00
2021-11-14 04:16:05 +00:00
while (i < repeat) {
if (searchIndex > 0) {
2021-12-26 13:59:02 +00:00
this.evaluate('Object.values(__getDocument().querySelectorAll("' + selector + '"))[' + searchIndex + '].value = ' + this.__escape(s));
2021-11-14 04:16:05 +00:00
} else {
2021-12-26 13:59:02 +00:00
this.evaluate('__getDocument().querySelector("' + selector + '").value = ' + this.__escape(s));
2021-11-14 04:16:05 +00:00
}
i++;
2021-08-11 06:55:49 +00:00
}
};
2022-02-10 02:16:12 +00:00
2021-08-11 06:55:49 +00:00
this.getText = function(selector) {
2021-12-22 07:06:50 +00:00
return this.getEvaluatedValue('__getDocument().querySelector("' + selector + '").innerText');
};
this.setHTML = function(selector, value, repeat, searchIndex) {
2021-12-26 13:59:02 +00:00
var s = value,
2021-12-22 07:06:50 +00:00
i = 0,
searchIndex = (typeof searchIndex !== "undefined" ? searchIndex : 0),
repeat = (typeof repeat !== "undefined" ? repeat : 1)
;
while (i < repeat) {
if (searchIndex > 0) {
2021-12-26 13:59:02 +00:00
this.evaluate('Object.values(__getDocument().querySelectorAll("' + selector + '"))[' + searchIndex + '].value = ' + this.__escape(s));
2021-12-22 07:06:50 +00:00
} else {
2021-12-26 13:59:02 +00:00
this.evaluate('__getDocument().querySelector("' + selector + '").innerHTML = ' + this.__escape(s));
2021-12-22 07:06:50 +00:00
}
i++;
}
};
this.getHTML = function(selector) {
return this.getEvaluatedValue('__getDocument().querySelector("' + selector + '").innerHTML');
2021-08-10 14:15:57 +00:00
};
2021-08-10 11:39:27 +00:00
2021-11-14 04:16:05 +00:00
this.traceMouseClick = function() {
2021-12-22 07:06:50 +00:00
return this.evaluate('__getWindow().addEventListener("click",function(e){var t=e.clientX,n=e.clientY,l=__getDocument().createElement("div");l.style.position="absolute",l.style.width="20px",l.style.height="20px",l.style.backgroundColor="#00ff00",l.style.zIndex=99999,l.style.top=__getWindow().pageYOffset+n-10+"px",l.style.left=__getWindow().pageXOffset+t-10+"px",__getDocument().body.appendChild(l)});');
2021-11-14 04:16:05 +00:00
};
2021-11-19 09:06:42 +00:00
2023-12-26 18:51:35 +00:00
// Added in 2023-12-27
2023-12-26 18:31:54 +00:00
this.markPosition = function(x, y) {
return this.evaluate('(function(x, y){var t=x,n=y,l=__getDocument().createElement("div");l.style.position="absolute",l.style.width="20px",l.style.height="20px",l.style.backgroundColor="#ff0000",l.style.zIndex=99999,l.style.top=__getWindow().pageYOffset+n-10+"px",l.style.left=__getWindow().pageXOffset+t-10+"px",__getDocument().body.appendChild(l)})(' + parseInt(x) + ', ' + parseInt(y) + ');');
};
2023-12-26 16:33:43 +00:00
// Added in 2023-12-27
this.getTextsBySelectorAll = function(selector) {
return JSON.parse(this.getEvaluatedValue('JSON.stringify(Object.values(__getDocument().querySelectorAll("' + selector + '")).reduce(function(a, x) { a.push(x.innerText); return a; }, []))'));
};
2021-11-14 04:16:05 +00:00
this.getWindowInnerHeight = function() {
2021-12-22 07:06:50 +00:00
return parseInt(this.getEvaluatedValue('__getWindow().innerHeight'));
2021-11-14 04:16:05 +00:00
};
2023-12-26 16:33:43 +00:00
this.getWindowPageYOffset = function() {
return this.getEvaluatedValue('__getWindow().pageYOffset');
};
this.getDocumentBodyOffsetHeight = function() {
return this.getEvaluatedValue('__getDocument().body.offsetHeight');
};
2023-10-30 20:41:52 +00:00
2021-12-03 20:49:38 +00:00
this.getDocumentScrollTop = function() {
2021-12-22 07:06:50 +00:00
return parseInt(this.getEvaluatedValue('__getDocument().documentElement.scrollTop'));
2021-12-03 20:49:38 +00:00
};
2021-11-19 09:06:42 +00:00
// formula: y > 0 and y + h < ih
this.isVisibleElementInViewport = function(elementPosition) {
2021-12-03 12:12:09 +00:00
return (elementPosition.y > 0 && (elementPosition.y + elementPosition.h < this.getWindowInnerHeight()));
2021-11-19 09:06:42 +00:00
};
2023-12-26 16:33:43 +00:00
this.isPageScrollEnded = function() {
return (this.getWindowInnerHeight() + this.getWindowPageYOffset()) >= this.getDocumentBodyOffsetHeight;
};
2023-10-30 20:41:52 +00:00
2021-12-26 13:59:02 +00:00
this.__escape = function(value) {
2022-08-28 14:04:47 +00:00
var pos = value.indexOf("__escaped:");
if (pos === 0)
return 'decodeURIComponent("' + value.substring(10) + '")';
else
2024-01-07 16:41:35 +00:00
return 'decodeURIComponent("' + this.encodeURIComponent(value)[0] + '")';
};
// Added in 2024-01-08
this.encodeURI = function(s) {
return [encodeURI(s), s];
};
// Added in 2024-01-08
this.decodeURI = function(s) {
if (s instanceof Array && s.length == 2) {
return s[1];
} else {
return decodeURI(s);
}
};
// Added in 2024-01-08
this.encodeURIComponent = function(s) {
return [encodeURIComponent(s), s];
};
// Added in 2024-01-08
this.decodeURIComponent = function(s) {
if (s instanceof Array && s.length == 2) {
return s[1];
} else {
return decodeURIComponent(s);
}
2021-12-26 13:59:02 +00:00
};
2022-01-25 18:46:17 +00:00
this.prompt = function(s) {
return this.getEvaluatedValue('prompt("' + s + '")');
};
2022-05-02 14:56:57 +00:00
this.confirm = function(s) {
return this.getEvaluatedValue('(confirm("' + s + '") ? "true" : "false")');
};
2022-01-25 18:46:17 +00:00
2022-01-25 17:40:19 +00:00
this.setVendor = function(vendor) {
var vendor = vendor.toLowerCase();
switch (vendor) {
case "msedge":
this.workingDirectory = SYS.getEnvString("PROGRAMFILES(X86)") + "\\Microsoft\\Edge\\Application";
2022-01-25 17:41:32 +00:00
this.binPath = SYS.getEnvString("PROGRAMFILES(X86)") + "\\Microsoft\\Edge\\Application\\msedge.exe";
2022-01-25 17:40:19 +00:00
break;
case "chrome":
this.workingDirectory = SYS.getEnvString("PROGRAMFILES") + "\\Google\\Chrome\\Application";
this.binPath = SYS.getEnvString("PROGRAMFILES") + "\\Google\\:installedDir\\Application\\chrome.exe";
break;
2022-09-05 05:11:29 +00:00
2022-05-10 04:41:42 +00:00
case "chromium":
this.workingDirectory = SYS.getEnvString("LOCALAPPDATA") + "\\Chromium\\Application";
this.binPath = SYS.getEnvString("LOCALAPPDATA") + "\\Chromium\\Application\\chrome.exe";
break;
case "opera":
this.workingDirectory = SYS.getEnvString("LOCALAPPDATA") + "\\Programs\\Opera";
this.binPath = SYS.getEnvString("LOCALAPPDATA") + "\\Programs\\Opera\\opera.exe";
break;
2022-01-25 17:40:19 +00:00
}
return this;
};
2021-12-26 13:59:02 +00:00
2022-02-14 06:46:59 +00:00
this.vMouseClick = function(x, y) {
2022-04-04 18:10:23 +00:00
Toolkit.sendClick(this.pageId.substring(0, 6), this.baseScreenX + x, this.baseScreenY + y, 1);
2022-02-14 06:46:59 +00:00
};
2022-05-02 14:56:57 +00:00
this.vSendKeys = function(s) {
Toolkit.sendKeys(this.pageId.substring(0, 6), s);
};
2022-02-10 02:16:12 +00:00
this.mouseClick = function(x, y) {
2022-02-10 04:25:58 +00:00
var screenPosition = this.getScreenPosition();
2022-04-04 18:10:23 +00:00
this.oAutoIt.callFunction("MouseMove", [screenPosition.x + this.baseScreenX + x, screenPosition.y + this.baseScreenY + y]);
this.oAutoIt.callFunction("MouseClick", ["left"]);
2022-02-10 02:16:12 +00:00
};
2022-02-10 05:44:23 +00:00
2022-02-14 06:07:53 +00:00
this.mouseWheelUp = function() {
this.oAutoIt.callFunction("MouseWheel", ["up"]);
};
this.mouseWheelDown = function() {
this.oAutoIt.callFunction("MouseWheel", ["down"]);
};
2022-02-10 06:06:11 +00:00
2022-02-14 06:07:53 +00:00
this.getReadyState = function() {
return this.getEvaluatedValue("document.readyState");
};
2022-04-25 10:30:26 +00:00
this.getCookie = function() {
return this.getEvaluatedValue("document.cookie");
};
2022-02-10 02:16:12 +00:00
2022-05-02 14:56:57 +00:00
this.getNumberOfSelectorAll = function(selector) {
return parseInt(this.getEvaluatedValue('document.querySelectorAll("' + selector + '").length'));
};
2022-04-29 07:12:22 +00:00
2022-05-04 03:15:09 +00:00
this.setValueOfSelectorAll = function(selector, s) {
this.evaluate('document.querySelectorAll("' + selector + '").forEach(function(x){x.value = "' + s + '";})');
2022-04-29 07:12:22 +00:00
};
2022-09-05 05:11:29 +00:00
2022-04-29 07:12:22 +00:00
this.sendEnterKey = function() {
this.evaluate('var ev=new KeyboardEvent("keydown",{bubbles:!0,cancelable:!0,keyCode:13});document.body.dispatchEvent(ev);');
};
2022-08-29 02:24:03 +00:00
this.getShadowRootSelector = function(selectors) {
var s = "').shadowRoot.querySelector('";
return ".querySelector('" + selectors.join(s) + "')";
};
2022-04-29 07:12:22 +00:00
2021-08-10 11:39:27 +00:00
this.create();
2020-11-20 08:44:50 +00:00
};
2023-02-01 02:02:55 +00:00
ChromeObject.prototype = new STD.EventTarget();
2022-01-08 14:24:43 +00:00
ChromeObject.prototype.constructor = ChromeObject;
2020-11-20 08:44:50 +00:00
2021-08-16 20:30:30 +00:00
exports.create = function(profileName) {
return (new ChromeObject()).setProfile(profileName, null);
2020-11-15 04:31:35 +00:00
};
2020-11-20 08:44:50 +00:00
exports.start = function(url, proxyPort, profileName, userDataDir, installedDir) {
2021-08-10 11:39:27 +00:00
return (new ChromeObject())
.setProxyPort(proxyPort)
.setProfile(profileName, installedDir)
.setUserDataDir(userDataDir)
.open(url)
;
2020-11-09 19:06:58 +00:00
};
2021-06-19 20:51:34 +00:00
2021-06-24 20:47:31 +00:00
exports.startWithDebugging = function(url, proxy, profileName, debuggingPort) {
2021-08-10 11:39:27 +00:00
return (new ChromeObject())
.setProxy(proxy)
.setProfile(profileName, null)
.setUserDataDir(null)
.setDebuggingPort(debuggingPort)
.open(url)
;
2021-06-19 20:51:34 +00:00
};
2021-07-27 20:16:38 +00:00
2022-09-05 05:11:29 +00:00
exports.startWithDebuggingUA = function(url, proxy, profileName, debuggingPort) {
return (new ChromeObject())
.setProxy(proxy)
.setProfile(profileName, null)
.setUserDataDir(null)
.setDebuggingPort(debuggingPort)
.addUserAgentsFromFile("data\\UserAgents.txt")
.open(url)
;
};
2022-01-09 17:41:57 +00:00
exports.startDebug = function(url, proxy, profileName, debuggingPort, isPreventProxy) {
return (new ChromeObject())
.setProxy(proxy)
.setProfile(profileName, null)
.setUserDataDir(null)
.setDebuggingPort(debuggingPort)
.setIsPreventProxy(isPreventProxy)
2022-03-01 20:39:36 +00:00
//.addUserAgentsFromFile("data\\Chrome.txt")
//.addUserAgentsFromFile("data\\Edge.txt")
//.addUserAgentsFromFile("data\\Safari.txt")
.open(url)
;
};
exports.startDebugInPrivate = function(url, proxy, profileName, debuggingPort, isPreventProxy) {
return (new ChromeObject())
.setProxy(proxy)
.setProfile(profileName, null)
.setUserDataDir(null)
.setDebuggingPort(debuggingPort)
.setIsPreventProxy(isPreventProxy)
2022-04-04 18:10:23 +00:00
.setInPrivate(true)
2022-03-01 20:39:36 +00:00
//.addUserAgentsFromFile("data\\Chrome.txt")
//.addUserAgentsFromFile("data\\Edge.txt")
2022-02-10 05:44:23 +00:00
//.addUserAgentsFromFile("data\\Safari.txt")
2022-01-09 17:41:57 +00:00
.open(url)
;
};
2024-04-23 18:28:57 +00:00
exports.VERSIONINFO = "Chrome Web Browser Debugging Interface (chrome.js) version 0.4.7";
2022-11-25 17:16:46 +00:00
exports.AUTHOR = "abuse@catswords.net";
2021-07-27 20:16:38 +00:00
exports.global = global;
exports.require = global.require;