From e8e9174f1f3a33650176ab41cc76b330af5c6c04 Mon Sep 17 00:00:00 2001 From: "Namhyeon, Go" Date: Mon, 19 Jul 2021 21:24:30 +0900 Subject: [PATCH] Update chrome.js --- lib/chrome.js | 70 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 66 insertions(+), 4 deletions(-) diff --git a/lib/chrome.js b/lib/chrome.js index fa3936e..9de3508 100644 --- a/lib/chrome.js +++ b/lib/chrome.js @@ -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(); };