From bdb5141bf8db6fac36d0de1884cb30bd6896aebc Mon Sep 17 00:00:00 2001 From: "Namhyeon, Go" Date: Mon, 5 Jul 2021 20:09:01 +0900 Subject: [PATCH] Update chrome.js --- lib/chrome.js | 75 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 71 insertions(+), 4 deletions(-) diff --git a/lib/chrome.js b/lib/chrome.js index db3f5a8..fa3936e 100644 --- a/lib/chrome.js +++ b/lib/chrome.js @@ -23,12 +23,21 @@ var ChromeObject = function() { "port": 1080 }; this.inPrivate = false; - + this.oAutoIt = null; + // for remote debugging this.debuggingPort = 0; this.pageId = ""; this.ws = Websocket.create(); + this.create = function() { + try { + this.oAutoIt = CreateObject("AutoItX3.Control"); + } catch (e) { + console.log("ChromeObject.create() -> " + e.message); + } + }; + this.setBinPath = function(path) { this.binPath = path; return this; @@ -190,7 +199,8 @@ var ChromeObject = function() { cmd.push("--profile-directory=\"" + this.profileName + "\""); if (this.proxy != null) { - cmd.push("--proxy-server=\"" + this.proxy.protocol + "://" + this.proxy.host + ":" + this.proxyPort + "\""); + console.log("--proxy-server=\"" + this.proxy.protocol + "://" + this.proxy.host + ":" + this.proxy.port + "\""); + cmd.push("--proxy-server=\"" + this.proxy.protocol + "://" + this.proxy.host + ":" + this.proxy.port + "\""); } cmd.push("--user-data-dir=\"" + this.userDataDir + "\""); @@ -288,11 +298,15 @@ var ChromeObject = function() { this.focus = function() { if (this.debuggingPort > 0) { try { - var oAutoIt = CreateObject("AutoItX3.Control"); + // change webpage title for focusing window + this.setTitle(this.pageId); + sleep(1500); + + // find window by title var pageList = this.getPageList(); for (var i = 0; i < pageList.length; i++) { if (pageList[i].id == this.pageId) { - oAutoIt.WinActivate(pageList[i].title); + this.oAutoIt.WinActivate(pageList[i].title); } } } catch (e) { @@ -305,6 +319,59 @@ var ChromeObject = function() { return this.evaluate("window.blur()"); }; + this.downMouseWheel = function(times) { + if (this.debuggingPort > 0) { + try { + var pos = this.getScreenPosition(); + this.oAutoIt.MouseMove(pos.x + 100, pos.y + 100); + this.oAutoIt.MouseWheel("down", times); + } catch (e) { + console.error("ChromeObject.downMouseWheel() -> " + e.message); + } + } + }; + + this.upMouseWheel = function(times) { + if (this.debuggingPort > 0) { + try { + var pos = this.getScreenPosition(); + this.oAutoIt.MouseMove(pos.x + 100, pos.y + 100); + this.oAutoIt.MouseWheel("up", times); + } catch (e) { + console.error("ChromeObject.upMouseWheel() -> " + e.message); + } + } + }; + + this.setTitle = function(title) { + if (this.debuggingPort > 0) { + this.evaluate('document.title = "' + title + ' " + document.title'); + } + }; + + this.getScreenPosition = function() { + var response = this.evaluate('(function() { return [window.screenX, window.screenY].join(","); })();'); + var result = JSON.parse(response).result.result.value; + var pos = result.split(','); + return { + "x": parseInt(pos[0]), + "y": parseInt(pos[1]) + }; + }; + + this.getPageHeight = function() { + var height = 0; + + if (this.debuggingPort > 0) { + var response = this.evaluate('(function(obj) { return Math.max(obj.scrollHeight, obj.clientHeight); })(document.querySelector("html"))'); + var result = JSON.parse(response).result.result.value; + height = parseInt(result); + } + + return height; + }; + + this.create(); }; exports.create = function() {