welsonjs/lib/chrome.js

110 lines
3.2 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");
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-18 10:55:06 +00:00
// 파일이 없는 경우, 32비트 설치 폴더에 위치하는지 한번 더 확인
if (!FILE.fileExists(this.binPath)) {
this.workingDirectory = SYS.getEnvString("PROGRAMFILES(X86)") + "\\Google\\:profileName\\Application";
this.binPath = this.workingDirectory + "\\chrome.exe";
this.setProfileName(this.profileName);
}
// 파일 찾기
2020-11-18 08:46:26 +00:00
if (!FILE.fileExists(this.binPath)) {
console.error("ChromeObject.open() -> '" + this.profileName + "' 존재하지 않는 프로파일입니다. 생성 후 사용해주세요.");
return this;
}
2020-11-18 10:55:06 +00:00
// 바로가기 생성
SHELL.createDesktopIcon("Chrome (" + this.profileName + ")", [
"cscript",
"app.js",
"shoutcut",
"chrome",
this.profileName
], SYS.getCurrentScriptDirectory());
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 + "\"",
2020-11-18 10:55:06 +00:00
"--user-data-dir=\"" + SYS.getCurrentScriptDirectory() + "\\UserData_Chrome_" + this.profileName + "\"",
2020-11-18 04:13:38 +00:00
"\"" + url + "\""
].join(" "));
sleep(1000);
2020-11-15 04:31:35 +00:00
this.processID = process.ProcessID;
2020-11-18 10:55:06 +00:00
sleep(1000);
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-18 08:46:26 +00:00
sleep(1000);
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
};