Update chrome.js

This commit is contained in:
Namhyeon Go 2021-06-25 05:47:31 +09:00 committed by GitHub
parent e277141bf4
commit 7b6bfce156
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -36,7 +36,7 @@ var ChromeObject = function() {
this.setProfile = function(profileName, installedDir) { this.setProfile = function(profileName, installedDir) {
this.profileName = (profileName == "Default" ? "Chrome" : profileName); this.profileName = (profileName == "Default" ? "Chrome" : profileName);
this.installedDir = installedDir; this.setInstalledDir(installedDir);
this.workingDirectory = this.workingDirectory.replace(":installedDir", this.installedDir); this.workingDirectory = this.workingDirectory.replace(":installedDir", this.installedDir);
this.binPath = this.binPath.replace(":installedDir", this.installedDir); this.binPath = this.binPath.replace(":installedDir", this.installedDir);
//this.binPath = this.binPath.replace(":installedDir", "Chrome"); //this.binPath = this.binPath.replace(":installedDir", "Chrome");
@ -44,12 +44,18 @@ var ChromeObject = function() {
}; };
this.setUserDataDir = function(dirname) { this.setUserDataDir = function(dirname) {
this.userDataDir = dirname; if (dirname != null) {
this.userDataDir = dirname;
} else {
this.userDataDir = SHELL.getPathOfMyDocuments() + "\\UserData_Chrome_" + this.profileName;
}
return this; return this;
}; };
this.setInstalledDir = function(dirname) { this.setInstalledDir = function(dirname) {
this.installedDir = dirname; if (dirname != null) {
this.installedDir = dirname;
}
return this; return this;
}; };
@ -75,6 +81,7 @@ var ChromeObject = function() {
this.setDebuggingPort = function(n) { this.setDebuggingPort = function(n) {
this.debuggingPort = (typeof(n) !== "number" ? this.debuggingPort : n); this.debuggingPort = (typeof(n) !== "number" ? this.debuggingPort : n);
return this;
} }
this.setPageId = function(s) { this.setPageId = function(s) {
@ -181,7 +188,11 @@ var ChromeObject = function() {
} }
cmd.push("--profile-directory=\"" + this.profileName + "\""); cmd.push("--profile-directory=\"" + this.profileName + "\"");
cmd.push("--proxy-server=\"" + this.proxy.protocol + "://" + this.proxy.host + ":" + this.proxyPort + "\"");
if (this.proxy != null) {
cmd.push("--proxy-server=\"" + this.proxy.protocol + "://" + this.proxy.host + ":" + this.proxyPort + "\"");
}
cmd.push("--user-data-dir=\"" + this.userDataDir + "\""); cmd.push("--user-data-dir=\"" + this.userDataDir + "\"");
cmd.push("\"" + url + "\""); cmd.push("\"" + url + "\"");
shell.runAs(this.binPath, cmd); shell.runAs(this.binPath, cmd);
@ -198,7 +209,7 @@ var ChromeObject = function() {
this.getPageList = function() { this.getPageList = function() {
if (this.debuggingPort > 0) { if (this.debuggingPort > 0) {
return HTTP.get("http://127.0.0.1:" + this.debuggingPort + "/json"); return JSON.parse(HTTP.get("http://127.0.0.1:" + this.debuggingPort + "/json"));
} else { } else {
console.error("Remote debugging unavailable"); console.error("Remote debugging unavailable");
return []; return [];
@ -225,19 +236,33 @@ var ChromeObject = function() {
}; };
this.sendPageRPC = function(method, params) { this.sendPageRPC = function(method, params) {
if (this.pageId != "") { var result = null;
this.ws.send("ws://127.0.0.1:" + this.debuggingPort + "/devtools/page/" + this.pageId, JSON.stringify({
"id": pageEventId, try {
"method": method, if (this.pageId != "") {
"params": params result = this.ws.send("ws://127.0.0.1:" + this.debuggingPort + "/devtools/page/" + this.pageId, JSON.stringify({
})); "id": pageEventId,
pageEventId++; "method": method,
console.info("ChromeObject().sendPageRPC() -> Sent"); "params": params
} else { }));
console.error("pageId not specified"); pageEventId++;
console.info("ChromeObject().sendPageRPC() -> Sent");
} else {
var pageList = this.getPageList();
if (pageList instanceof Array && pageList.length > 0) {
this.pageId = pageList[0].id;
if (this.pageId != "") {
result = this.sendPageRPC(method, params);
} else {
console.error("Got invaild list of pages");
}
}
}
} catch (e) {
console.log("ChromeObject.sendPageRPC() -> " + e.message);
} }
return this; return result;
}; };
this.navigate = function(url) { this.navigate = function(url) {
@ -247,14 +272,39 @@ var ChromeObject = function() {
}; };
this.evaluate = function(expression) { this.evaluate = function(expression) {
return this.sendPageRPC("Runtime.evaluate", { try {
"expression": expression return this.sendPageRPC("Runtime.evaluate", {
}); "expression": expression
});
} catch (e) {
console.error("ChromeObject.evaluate() -> " + e.message);
}
};
this.close = function() {
return this.sendPageRPC("Browser.close", {});
}; };
this.focus = function() { this.focus = function() {
return this.evaluate("window.focus()"); if (this.debuggingPort > 0) {
try {
var oAutoIt = CreateObject("AutoItX3.Control");
var pageList = this.getPageList();
for (var i = 0; i < pageList.length; i++) {
if (pageList[i].id == this.pageId) {
oAutoIt.WinActivate(pageList[i].title);
}
}
} catch (e) {
console.error("ChromeObject.focus() -> " + e.message);
}
}
}; };
this.blur = function() {
return this.evaluate("window.blur()");
};
}; };
exports.create = function() { exports.create = function() {
@ -266,17 +316,15 @@ exports.start = function(url, proxyPort, profileName, userDataDir, installedDir)
.setProxyPort(proxyPort) .setProxyPort(proxyPort)
.setProfile(profileName, installedDir) .setProfile(profileName, installedDir)
.setUserDataDir(userDataDir) .setUserDataDir(userDataDir)
.setInstalledDir(installedDir)
.open(url) .open(url)
; ;
}; };
exports.startWithDebugging = function(url, proxy, profileName, userDataDir, installedDir, debuggingPort) { exports.startWithDebugging = function(url, proxy, profileName, debuggingPort) {
return (new ChromeObject()) return (new ChromeObject())
.setProxy(proxy) .setProxy(proxy)
.setProfile(profileName, installedDir) .setProfile(profileName, null)
.setUserDataDir(userDataDir) .setUserDataDir(null)
.setInstalledDir(installedDir)
.setDebuggingPort(debuggingPort) .setDebuggingPort(debuggingPort)
.open(url) .open(url)
; ;