From 1e95b79056704f55bd03e4b548356f93875b55e2 Mon Sep 17 00:00:00 2001 From: "Namhyeon, Go" Date: Sun, 14 Nov 2021 13:16:05 +0900 Subject: [PATCH] Update chrome.js --- lib/chrome.js | 81 ++++++++++++++++++++++++++++----------------------- 1 file changed, 45 insertions(+), 36 deletions(-) diff --git a/lib/chrome.js b/lib/chrome.js index e368ba1..a66b4b4 100644 --- a/lib/chrome.js +++ b/lib/chrome.js @@ -266,7 +266,6 @@ var ChromeObject = function() { try { var responseText = HTTP.get("http://127.0.0.1:" + this.debuggingPort + "/json"); //console.info(responseText); - console.log(responseText); pageList = JSON.parse(responseText); this.pageList = pageList; return pageList; @@ -448,7 +447,7 @@ var ChromeObject = function() { }; this.autoAdjustByScreen = function(sX, sY, divX, divY) { - // catch focus + // focus var title = this.focus(); sleep(300); @@ -461,7 +460,7 @@ var ChromeObject = function() { var h = this.getRandomInt(bY, sY - bY); this.oAutoIt.WinMove(title, "", x, y, w, h); - // release focus + // blur this.blur(); }; @@ -477,13 +476,9 @@ var ChromeObject = function() { var y = this.getRandomInt(0, (sY - h < 0 ? parseInt(sY * 0.2) : (sY - h))); this.oAutoIt.WinMove(title, "", x, y, w, h); - // release focus + // blur this.blur(); }; - - this.blur = function() { - return this.evaluate("window.blur()"); - }; this.downMouseWheel = function(times) { if (this.debuggingPort > 0) { @@ -536,7 +531,7 @@ var ChromeObject = function() { }; this.getElementPosition = function(selector) { - var result = this.getEvaluatedValue('(function() { var rect = document.querySelector("' + selector + '").getBoundingClientRect(); return [parseInt(rect.left), parseInt(rect.top), parseInt(rect.x), parseInt(rect.y), parseInt(rect.right), parseInt(rect.bottom)].join(","); })();'); + var result = this.getEvaluatedValue('(function() { var rect = document.querySelector("' + selector + '").getBoundingClientRect(); return [parseInt(rect.left), parseInt(rect.top), parseInt(rect.x), parseInt(rect.y), parseInt(rect.width), parseInt(rect.height)].join(","); })();'); var pos = result.split(','); if (pos.length == 6) { return { @@ -544,8 +539,8 @@ var ChromeObject = function() { "y": parseInt(pos[1]), "a": parseInt(pos[2]), "b": parseInt(pos[3]), - "g": parseInt(pos[4]), - "d": parseInt(pos[5]) + "w": parseInt(pos[4]), + "h": parseInt(pos[5]) }; } else { return { @@ -561,10 +556,10 @@ var ChromeObject = function() { this.getNestedElementPosition = function(selector, subSelector, searchText, startIndex) { var s = ''; - var startIndex = (typeof startIndex !== 'undefined' ? startIndex : 0); + var startIndex = (typeof startIndex !== 'undefined' ? startIndex : 0); - if (searchText.indexOf(':tokenize(') == 0) { - searchText = searchText.substring(searchText.indexOf('(') + 1, searchText.lastIndexOf(')')); + if (searchText.indexOf(':tokenize(') == 0) { + searchText = searchText.substring(searchText.indexOf('(') + 1, searchText.lastIndexOf(')')); s += '(function() {' + ' var elements = Object.values(document.querySelectorAll("' + selector + '")).filter(function(x) {' + ' var el = x.querySelector("' + subSelector + '");' @@ -580,13 +575,13 @@ var ChromeObject = function() { + ' }' + '})()' ; - } else if (searchText.indexOf(':p(') == 0) { + } else if (searchText.indexOf(':p(') == 0) { var p = parseFloat(searchText.substring(searchText.indexOf('(') + 1, searchText.lastIndexOf(')'))); if (p > 0) { s += '(function() {' + ' var elements = Object.values(document.querySelectorAll("' + selector + '")).filter(function(x) {' + ' return (Math.random() < ' + p + ');' - + ' ' + (startIndex > 0 ? '}).slice(' + startIndex + ');' : '});') + + ' ' + (startIndex > 0 ? '}).slice(' + startIndex + ');' : '});') + ' if (elements.length > 0) {' + ' var rect = elements[0].getBoundingClientRect();' + ' return [parseInt(rect.left), parseInt(rect.top), window.pageXOffset + parseInt(rect.left), window.pageYOffset + parseInt(rect.top), parseInt(rect.width), parseInt(rect.height)].join(",");' @@ -598,7 +593,7 @@ var ChromeObject = function() { } else { s += '(function() {' + ' var elements = Object.values(document.querySelectorAll("' + selector + '"));' - + ' ' + (startIndex > 0 ? 'elements = elements.slice(' + startIndex + ');' : '') + + ' ' + (startIndex > 0 ? 'elements = elements.slice(' + startIndex + ');' : '') + ' if (elements.length > 0) {' + ' var k = Math.floor(Math.random() * elements.length);' + ' var rect = elements[k].getBoundingClientRect();' @@ -624,7 +619,7 @@ var ChromeObject = function() { + ' return "";' + ' }' + '})()' - ; + ; } var result = this.getEvaluatedValue(s); @@ -649,13 +644,13 @@ var ChromeObject = function() { }; } }; - + this.triggerEventOnNestedElement = function(eventName, selector, subSelector, searchText, startIndex) { var s = ''; - var startIndex = (typeof startIndex !== 'undefined' ? startIndex : 0); + var startIndex = (typeof startIndex !== 'undefined' ? startIndex : 0); - if (searchText.indexOf(':tokenize(') == 0) { - searchText = searchText.substring(searchText.indexOf('(') + 1, searchText.lastIndexOf(')')); + if (searchText.indexOf(':tokenize(') == 0) { + searchText = searchText.substring(searchText.indexOf('(') + 1, searchText.lastIndexOf(')')); s += '(function() {' + ' var elements = Object.values(document.querySelectorAll("' + selector + '")).filter(function(x) {' + ' var el = x.querySelector("' + subSelector + '");' @@ -668,13 +663,13 @@ var ChromeObject = function() { + ' }' + '})()' ; - } else if (searchText.indexOf(':p(') == 0) { + } else if (searchText.indexOf(':p(') == 0) { var p = parseFloat(searchText.substring(searchText.indexOf('(') + 1, searchText.lastIndexOf(')'))); if (p > 0) { s += '(function() {' + ' var elements = Object.values(document.querySelectorAll("' + selector + '")).filter(function(x) {' + ' return (Math.random() < ' + p + ');' - + ' ' + (startIndex > 0 ? '}).slice(' + startIndex + ');' : '});') + + ' ' + (startIndex > 0 ? '}).slice(' + startIndex + ');' : '});') + ' if (elements.length > 0) {' + ' elements[0].' + (eventName == 'click' ? 'click()' : 'dispatchEvent(new Event("' + eventName + '"))') + ';' + ' }' @@ -683,7 +678,7 @@ var ChromeObject = function() { } else { s += '(function() {' + ' var elements = Object.values(document.querySelectorAll("' + selector + '"));' - + ' ' + (startIndex > 0 ? 'elements = elements.slice(' + startIndex + ');' : '') + + ' ' + (startIndex > 0 ? 'elements = elements.slice(' + startIndex + ');' : '') + ' if (elements.length > 0) {' + ' var k = Math.floor(Math.random() * elements.length);' + ' elements[k].' + (eventName == 'click' ? 'click()' : 'dispatchEvent(new Event("' + eventName + '"))') + ';' @@ -703,7 +698,7 @@ var ChromeObject = function() { + ' elements[0].' + (eventName == 'click' ? 'click()' : 'dispatchEvent(new Event("' + eventName + '"))') + ';' + ' }' + '})()' - ; + ; } return this.evaluate(s); @@ -743,7 +738,7 @@ var ChromeObject = function() { this.isAttached = isAttached; return this; }; - + this.getRandomInt = function(min, max) { min = Math.ceil(min); max = Math.floor(max); @@ -839,7 +834,8 @@ var ChromeObject = function() { }; this.reload = function() { - return this.sendPageRPC("Page.reload", {}); + //return this.sendPageRPC("Page.reload", {}); + return this.evaluate("window.reload()"); }; this.hasClass = function(seletctor, className) { @@ -863,14 +859,19 @@ var ChromeObject = function() { this.oAutoIt.Send("{SPACE}"); }; - this.setValue = function(selector, value, searchIndex) { - var searchIndex = (typeof searchIndex !== "undefined" ? searchIndex : 0); - var s = encodeURIComponent(value); - - if (searchIndex > 0) { - this.evaluate('Object.values(document.querySelectorAll("' + selector + '"))[' + searchIndex + '].value = decodeURIComponent("' + s + '");'); - } else { - this.evaluate('document.querySelector("' + selector + '").value = decodeURIComponent("' + s + '")'); + this.setValue = function(selector, value, repeat, searchIndex) { + var s = encodeURIComponent(value), + i = 0, + searchIndex = (typeof searchIndex !== "undefined" ? searchIndex : 0) + ; + + while (i < repeat) { + if (searchIndex > 0) { + this.evaluate('Object.values(document.querySelectorAll("' + selector + '"))[' + searchIndex + '].value = decodeURIComponent("' + s + '");'); + } else { + this.evaluate('document.querySelector("' + selector + '").value = decodeURIComponent("' + s + '")'); + } + i++; } }; @@ -878,6 +879,14 @@ var ChromeObject = function() { return this.getEvaluatedValue('document.querySelector("' + selector + '").innerText'); }; + this.traceMouseClick = function() { + return this.evaluate('window.addEventListener("click",function(e){var t=e.clientX,n=e.clientY,l=document.createElement("div");l.style.position="absolute",l.style.width="20px",l.style.height="20px",l.style.backgroundColor="#00ff00",l.style.zIndex=99999,l.style.top=window.pageYOffset+n-10+"px",l.style.left=window.pageXOffset+t-10+"px",document.body.appendChild(l)});'); + }; + + this.getWindowInnerHeight = function() { + return this.getEvaluatedValue('window.innerHeight'); + }; + this.create(); };