welsonjs/lib/chrome.js

85 lines
2.4 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-09 10:06:34 +00:00
2020-11-15 04:31:35 +00:00
var ChromeObject = function() {
2020-11-18 04:13:38 +00:00
this.workingDirectory = SYS.getEnvString("PROGRAMFILES") + "\\Google\\:profileName\\Application";
this.binPath = this.workingDirectory + "\\chrome.exe";
2020-11-15 04:31:35 +00:00
this.processID = 0;
2020-11-09 19:06:58 +00:00
this.profileName = "Default";
this.proxyPort = 1080;
2020-11-15 04:31:35 +00:00
this.processList = [];
this.setBinPath = function(path) {
this.binPath = path;
return this;
};
2020-11-09 19:06:58 +00:00
2020-11-18 04:13:38 +00:00
this.setProfileName = function(profileName) {
this.profileName = (profileName == "Default" ? "Chrome" : profileName);
this.workingDirectory = this.workingDirectory.replace(":profileName", this.profileName);
this.binPath = this.binPath.replace(":profileName", this.profileName);
2020-11-15 04:31:35 +00:00
return this;
2020-11-09 19:06:58 +00:00
};
2020-11-09 10:06:34 +00:00
2020-11-09 19:06:58 +00:00
this.setProxyPort = function(s) {
this.proxyPort = 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
this.getProcessList = function() {
return this.processList;
2020-11-09 19:43:19 +00:00
};
2020-11-09 10:06:34 +00:00
2020-11-09 19:06:58 +00:00
this.open = function(url) {
2020-11-18 04:13:38 +00:00
this.setProfileName(this.profileName);
2020-11-15 04:31:35 +00:00
var process;
while (this.processID == 0) {
try {
2020-11-18 04:13:38 +00:00
/*
2020-11-15 04:31:35 +00:00
process = SB.start(this.profileName, [
this.binPath,
"--profile-directory=" + this.profileName,
"--proxy-server=socks5://127.0.0.1:" + this.proxyPort,
url
]);
2020-11-18 04:13:38 +00:00
*/
/*
process = SHELL.createProcess([
this.binPath,
"--profile-directory=" + this.profileName,
"--proxy-server=socks5://127.0.0.1:" + this.proxyPort,
url
], this.workingDirectory);
*/
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.workingDirectory + "\"",
"\"" + url + "\""
].join(" "));
sleep(1000);
2020-11-15 04:31:35 +00:00
this.processID = process.ProcessID;
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-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
exports.getProcessIDs = function() {
return (new ChromeObject()).getProcessIDs();
};
2020-11-09 19:06:58 +00:00
exports.start = function(url, proxyPort, profileName) {
2020-11-15 04:31:35 +00:00
return (new ChromeObject()).setProxyPort(proxyPort).setProfileName(profileName).open(url).processID;
2020-11-09 19:06:58 +00:00
};