welsonjs/lib/chrome.js

154 lines
4.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-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-20 08:44:50 +00:00
this.workingDirectory = SYS.getEnvString("PROGRAMFILES") + "\\Google\\Chrome\\Application";
this.binPath = SYS.getEnvString("PROGRAMFILES") + "\\Google\\:installedDir\\Application\\chrome.exe";
2020-11-15 04:31:35 +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;
2020-11-09 19:06:58 +00:00
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-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;
//this.workingDirectory = this.workingDirectory.replace(":installedDir", this.installedDir);
this.binPath = this.binPath.replace(":installedDir", this.installedDir);
return this;
};
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
};
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-20 08:44:50 +00:00
this.createShoutcut = function(url) {
if (!this.userDataDir) {
this.userDataDir = SHELL.getPathOfMyDocuments() + "\\UserData_Chrome_" + this.profileName;
}
SHELL.createDesktopIcon("Chrome Prototype (" + this.installedDir + ")", [
"cscript",
"app.js",
"shoutcut",
"chrome",
this.installedDir,
"--profile-directory=\"" + this.profileName + "\"",
"--user-data-dir=\"" + this.userDataDir + "\"",
url
].join(' '), SYS.getCurrentScriptDirectory());
};
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
2020-11-18 10:55:06 +00:00
// 파일이 없는 경우, 32비트 설치 폴더에 위치하는지 한번 더 확인
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
}
// 파일 찾기
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
// 바로가기 생성
2020-11-20 08:44:50 +00:00
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(" "));
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
}
}
2020-11-20 08:44:50 +00:00
*/
try {
if (!this.userDataDir) {
this.userDataDir = SHELL.getPathOfMyDocuments() + "\\UserData_Chrome_" + this.profileName;
}
var shell = SHELL.create().setWorkingDirectory(this.workingDirectory);
shell.runAs(this.binPath, [
"--profile-directory=\"" + this.profileName + "\"",
"--proxy-server=\"socks5://127.0.0.1:" + this.proxyPort + "\"",
"--user-data-dir=\"" + this.userDataDir + "\"",
"\"" + url + "\""
]);
sleep(3000);
shell.release();
} catch (e) {
console.error("ChromeObject.open() -> " + e.message);
sleep(1000);
}
2020-11-15 04:31:35 +00:00
return this;
2020-11-09 19:06:58 +00:00
};
};
2020-11-20 08:44:50 +00:00
exports.create = function() {
return new ChromeObject();
};
2020-11-15 04:31:35 +00:00
exports.getProcessIDs = function() {
return (new ChromeObject()).getProcessIDs();
};
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)
.open(url)
.processID
;
2020-11-09 19:06:58 +00:00
};