welsonjs/lib/chrome.js

269 lines
6.8 KiB
JavaScript
Raw Normal View History

2020-11-09 10:06:34 +00:00
//////////////////////////////////////////////////////////////////////////////////
// Google Chrome API
/////////////////////////////////////////////////////////////////////////////////
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");
2020-11-09 10:06:34 +00:00
2020-11-15 04:31:35 +00:00
var ChromeObject = function() {
2020-11-20 08:44:50 +00:00
this.workingDirectory = SYS.getEnvString("PROGRAMFILES") + "\\Google\\Chrome\\Application";
this.binPath = SYS.getEnvString("PROGRAMFILES") + "\\Google\\:installedDir\\Application\\chrome.exe";
2021-06-19 20:51:34 +00:00
//this.processID = 0;
2020-11-20 08:44:50 +00:00
this.installedDir = "Chrome";
2020-11-09 19:06:58 +00:00
this.profileName = "Default";
2020-11-20 08:44:50 +00:00
this.userDataDir = null;
2021-06-19 20:51:34 +00:00
this.proxy = {
"protocol": "socks5",
"host": "127.0.0.1",
"port": 1080
};
2020-12-10 01:26:33 +00:00
this.inPrivate = false;
2021-06-19 21:59:41 +00:00
// for remote debugging
2021-06-19 20:51:34 +00:00
this.debuggingPort = 0;
2021-06-19 21:59:41 +00:00
this.pageEventId = 1;
this.pageId = "";
this.ws = Websocket.create();
2020-11-15 04:31:35 +00:00
this.setBinPath = function(path) {
this.binPath = path;
return this;
};
2020-11-09 19:06:58 +00:00
2020-11-20 08:44:50 +00:00
this.setProfile = function(profileName, installedDir) {
2020-11-18 04:13:38 +00:00
this.profileName = (profileName == "Default" ? "Chrome" : profileName);
2020-11-20 08:44:50 +00:00
this.installedDir = installedDir;
2021-06-19 20:51:34 +00:00
this.workingDirectory = this.workingDirectory.replace(":installedDir", this.installedDir);
this.binPath = this.binPath.replace(":installedDir", this.installedDir);
//this.binPath = this.binPath.replace(":installedDir", "Chrome");
2020-11-20 08:44:50 +00:00
return this;
};
2021-06-19 20:51:34 +00:00
2020-11-20 08:44:50 +00:00
this.setUserDataDir = function(dirname) {
this.userDataDir = dirname;
return this;
};
this.setInstalledDir = function(dirname) {
this.installedDir = dirname;
2020-11-15 04:31:35 +00:00
return this;
2020-11-09 19:06:58 +00:00
};
2021-06-19 20:51:34 +00:00
this.setProxy = function(obj) {
this.proxy = obj;
return this;
};
2020-11-09 10:06:34 +00:00
2021-06-19 20:51:34 +00:00
this.setProxyProtocol = function(s) {
this.proxy.protocol = s;
2020-11-15 04:31:35 +00:00
return this;
2020-11-09 19:06:58 +00:00
};
2020-11-15 04:31:35 +00:00
2021-06-19 20:51:34 +00:00
this.setProxyHost = function(s) {
this.proxy.host = s;
return this;
};
this.setProxyPort = function(n) {
this.proxy.port = n;
return this;
2020-11-09 19:43:19 +00:00
};
2021-06-19 20:51:34 +00:00
this.setDebuggingPort = function(n) {
2021-06-19 21:11:44 +00:00
this.debuggingPort = (typeof(n) !== "number" ? this.debuggingPort : n);
2021-06-19 20:51:34 +00:00
}
2021-06-19 21:59:41 +00:00
this.setPageId = function(s) {
this.pageId = s;
};
2020-11-09 10:06:34 +00:00
2020-11-20 08:44:50 +00:00
this.createShoutcut = function(url) {
if (!this.userDataDir) {
this.userDataDir = SHELL.getPathOfMyDocuments() + "\\UserData_Chrome_" + this.profileName;
}
2021-06-19 20:51:34 +00:00
var cmd = [
"cscript",
"app.js",
"shoutcut",
"chrome",
this.installedDir,
];
if (this.inPrivate) {
cmd.push("--incognito");
}
if (this.debuggingPort > 0) {
cmd.push("--remote-debugging-port=" + this.debuggingPort);
2020-12-10 01:26:33 +00:00
}
2021-06-19 20:51:34 +00:00
cmd.push("--profile-directory=\"" + this.profileName + "\"");
cmd.push("--user-data-dir=\"" + this.userDataDir + "\"");
cmd.push("\"" + url + "\"");
SHELL.createDesktopIcon("Chrome Prototype (" + this.installedDir + ")", cmd.join(' '), SYS.getCurrentScriptDirectory());
2020-12-10 01:26:33 +00:00
};
this.setInPrivate = function(flag) {
this.inPrivate = flag;
return this;
2020-11-20 08:44:50 +00:00
};
2020-11-09 19:06:58 +00:00
this.open = function(url) {
2020-11-20 08:44:50 +00:00
this.setProfile(this.profileName, this.installedDir);
2020-11-18 04:13:38 +00:00
2021-06-19 20:56:20 +00:00
// if the file does not exists, Check the 32bit installation folder again
2020-11-18 10:55:06 +00:00
if (!FILE.fileExists(this.binPath)) {
2020-11-20 08:44:50 +00:00
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);
2020-11-18 10:55:06 +00:00
}
2021-06-19 20:56:20 +00:00
// find profile
2020-11-18 08:46:26 +00:00
if (!FILE.fileExists(this.binPath)) {
2021-06-19 20:56:20 +00:00
console.error("ChromeObject.open() -> '" + this.profileName + "' profile does not exists. You have to create it.");
2020-11-18 08:46:26 +00:00
return this;
}
2021-06-19 20:56:20 +00:00
// create shoutcut to desktop
2021-06-19 21:13:00 +00:00
if (this.debuggingPort == 0) {
this.createShoutcut();
}
2020-11-18 10:55:06 +00:00
2020-11-20 08:44:50 +00:00
/*
2020-11-15 04:31:35 +00:00
var process;
while (this.processID == 0) {
try {
2020-11-20 08:44:50 +00:00
if (!this.userDataDir) {
this.userDataDir = SHELL.getPathOfMyDocuments() + "\\UserData_Chrome_" + this.profileName;
}
2020-11-18 04:13:38 +00:00
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 + "\"",
2020-11-20 08:44:50 +00:00
"--user-data-dir=\"" + this.userDataDir + "\"",
2020-11-18 04:13:38 +00:00
"\"" + url + "\""
].join(" "));
2020-12-10 02:19:07 +00:00
sleep(1500);
2020-11-15 04:31:35 +00:00
this.processID = process.ProcessID;
2020-12-10 02:19:07 +00:00
sleep(1500);
2020-11-18 04:13:38 +00:00
shell.release();
2020-11-15 04:31:35 +00:00
} catch (e) {
2020-11-18 04:13:38 +00:00
console.error("ChromeObject.open() -> " + e.message);
2020-12-10 02:19:07 +00:00
sleep(1500);
2020-11-15 04:31:35 +00:00
}
}
2020-11-20 08:44:50 +00:00
*/
try {
if (!this.userDataDir) {
this.userDataDir = SHELL.getPathOfMyDocuments() + "\\UserData_Chrome_" + this.profileName;
}
2020-12-07 03:48:37 +00:00
2020-12-08 06:43:59 +00:00
var shell = SHELL.create();
shell.setWorkingDirectory(this.workingDirectory);
2021-06-19 20:51:34 +00:00
var cmd = [];
if (this.inPrivate) {
cmd.push("--incognito");
}
2021-06-19 21:11:44 +00:00
2021-06-19 20:51:34 +00:00
if (this.debuggingPort > 0) {
cmd.push("--remote-debugging-port=" + this.debuggingPort);
2020-12-10 01:26:33 +00:00
}
2021-06-19 20:51:34 +00:00
cmd.push("--profile-directory=\"" + this.profileName + "\"");
cmd.push("--proxy-server=\"" + this.proxy.protocol + "://" + this.proxy.host + ":" + this.proxyPort + "\"");
cmd.push("--user-data-dir=\"" + this.userDataDir + "\"");
cmd.push("\"" + url + "\"");
2021-06-19 20:53:58 +00:00
shell.runAs(this.binPath, cmd);
2021-06-19 20:51:34 +00:00
2020-12-10 02:19:07 +00:00
sleep(1500);
2020-11-20 08:44:50 +00:00
shell.release();
} catch (e) {
console.error("ChromeObject.open() -> " + e.message);
2020-12-10 02:19:07 +00:00
sleep(1500);
2020-11-20 08:44:50 +00:00
}
2020-11-15 04:31:35 +00:00
return this;
2020-11-09 19:06:58 +00:00
};
2021-06-19 20:51:34 +00:00
this.getPageList = function() {
if (this.debuggingPort > 0) {
return HTTP.get("http://localhost:" + this.debuggingPort + "/json");
} else {
console.error("Remote debugging unavailable");
return [];
}
};
2020-11-09 19:06:58 +00:00
2021-06-19 20:51:34 +00:00
this.getPageById = function(id) {
2021-06-19 20:52:30 +00:00
var pages = this.getPageList().filter(function(x) {
2021-06-19 21:14:29 +00:00
return (x.id = id);
2021-06-19 20:51:34 +00:00
});
return (pages.length > 0 ? pages[0] : null);
};
this.getPagesByUrl = function(url) {
2021-06-19 20:52:30 +00:00
return this.getPageList().filter(function(x) {
2021-06-19 20:51:34 +00:00
return (x.url == url);
});
};
this.getPagesByTitle = function(title) {
2021-06-19 20:52:30 +00:00
return this.getPageList().filter(function(x) {
2021-06-19 20:51:34 +00:00
return (x.title == title);
});
};
2021-06-19 21:59:41 +00:00
this.sendRPC = function(method, params) {
this.ws.send("ws://localhost:" + this.debuggingPort + "/devtools/page/" + this.pageId, JSON.stringify({
"id": this.pageEventId,
"method": method,
"params": params
}));
this.pageEventId++;
};
this.navigate = function(url) {
return this.sendRPC("Page.navigate", {
"url": url
});
};
this.evaluate = function(code) {
// todo
};
2020-11-20 08:44:50 +00:00
};
2021-06-19 20:51:34 +00:00
exports.create = function() {
return new ChromeObject();
2020-11-15 04:31:35 +00:00
};
2020-11-20 08:44:50 +00:00
exports.start = function(url, proxyPort, profileName, userDataDir, installedDir) {
return (new ChromeObject())
.setProxyPort(proxyPort)
.setProfile(profileName, installedDir)
.setUserDataDir(userDataDir)
.setInstalledDir(installedDir)
2021-06-19 20:57:17 +00:00
.open(url)
2020-11-20 08:44:50 +00:00
;
2020-11-09 19:06:58 +00:00
};
2021-06-19 20:51:34 +00:00
exports.startWithDebugging = function(url, proxy, profileName, userDataDir, installedDir, debuggingPort) {
return (new ChromeObject())
.setProxy(proxy)
.setProfile(profileName, installedDir)
.setUserDataDir(userDataDir)
.setInstalledDir(installedDir)
.setDebuggingPort(debuggingPort)
2021-06-19 20:57:17 +00:00
.open(url)
;
2021-06-19 20:51:34 +00:00
};