Update chrome.js

This commit is contained in:
Namhyeon Go 2021-07-19 21:24:30 +09:00 committed by GitHub
parent cd9d2d2939
commit e8e9174f1f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -29,6 +29,7 @@ var ChromeObject = function() {
this.debuggingPort = 0;
this.pageId = "";
this.ws = Websocket.create();
this.isAttached = false;
this.create = function() {
try {
@ -219,7 +220,11 @@ var ChromeObject = function() {
this.getPageList = function() {
if (this.debuggingPort > 0) {
return JSON.parse(HTTP.get("http://127.0.0.1:" + this.debuggingPort + "/json"));
try {
return JSON.parse(HTTP.get("http://127.0.0.1:" + this.debuggingPort + "/json"));
} catch (e) {
console.error("ChromeObject.getPageList() -> " + e.message);
}
} else {
console.error("Remote debugging unavailable");
return [];
@ -244,7 +249,7 @@ var ChromeObject = function() {
return (x.title == title);
});
};
this.sendPageRPC = function(method, params) {
var result = null;
@ -294,13 +299,27 @@ var ChromeObject = function() {
this.close = function() {
return this.sendPageRPC("Browser.close", {});
};
this.terminate = function() {
var pageList = this.getPageList();
for (var i = 0; i < pageList.length; i++) {
this.oAutoIt.WinKill(pageList[i].title);
}
};
this.focus = function() {
if (this.debuggingPort > 0) {
try {
// if page ID is empty
if (this.pageId == "") {
var pageList = this.getPageList();
if (pageList instanceof Array && pageList.length > 0) {
this.pageId = pageList[0].id;
}
}
// change webpage title for focusing window
this.setTitle(this.pageId);
sleep(1500);
// find window by title
var pageList = this.getPageList();
@ -315,6 +334,36 @@ var ChromeObject = function() {
}
};
this.focusWithoutActivate = function() {
if (this.debuggingPort > 0) {
try {
// if page ID is empty
if (this.pageId == "") {
var pageList = this.getPageList();
if (pageList instanceof Array && pageList.length > 0) {
this.pageId = pageList[0].id;
}
}
// change webpage title for focusing window
this.setTitle(this.pageId);
} catch (e) {
console.error("ChromeObject.focusWithoutActivate() -> " + e.message);
}
}
};
this.arrange = function() {
// resolution: 1920x1080
// 화면 하단은 작업표시줄 또는 알림창이 떠있을 수 있음. 클릭 방지를 위해 높이(h) 값은 -200 으로 조정함.
var x = this.getRandomInt(0, 960);
var y = this.getRandomInt(0, 540);
var w = this.getRandomInt(960, 1920 - x);
var h = this.getRandomInt(540, 1080 - y - 200);
this.oAutoIt.WinMove(this.pageId, "", x, y, w, h);
};
this.blur = function() {
return this.evaluate("window.blur()");
};
@ -345,7 +394,9 @@ var ChromeObject = function() {
this.setTitle = function(title) {
if (this.debuggingPort > 0) {
this.evaluate('document.title = "' + title + ' " + document.title');
for (var i = 0; i < 10; i++) {
this.evaluate('document.title = "' + title + '"');
}
}
};
@ -370,6 +421,17 @@ var ChromeObject = function() {
return height;
};
this.setIsAttached = function(isAttached) {
this.isAttached = isAttached;
return this;
};
this.getRandomInt = function(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
};
this.create();
};