Update chrome.js

This commit is contained in:
Namhyeon Go 2021-11-14 13:16:05 +09:00 committed by GitHub
parent cc36fffbd9
commit 1e95b79056
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -266,7 +266,6 @@ var ChromeObject = function() {
try { try {
var responseText = HTTP.get("http://127.0.0.1:" + this.debuggingPort + "/json"); var responseText = HTTP.get("http://127.0.0.1:" + this.debuggingPort + "/json");
//console.info(responseText); //console.info(responseText);
console.log(responseText);
pageList = JSON.parse(responseText); pageList = JSON.parse(responseText);
this.pageList = pageList; this.pageList = pageList;
return pageList; return pageList;
@ -448,7 +447,7 @@ var ChromeObject = function() {
}; };
this.autoAdjustByScreen = function(sX, sY, divX, divY) { this.autoAdjustByScreen = function(sX, sY, divX, divY) {
// catch focus // focus
var title = this.focus(); var title = this.focus();
sleep(300); sleep(300);
@ -461,7 +460,7 @@ var ChromeObject = function() {
var h = this.getRandomInt(bY, sY - bY); var h = this.getRandomInt(bY, sY - bY);
this.oAutoIt.WinMove(title, "", x, y, w, h); this.oAutoIt.WinMove(title, "", x, y, w, h);
// release focus // blur
this.blur(); this.blur();
}; };
@ -477,13 +476,9 @@ var ChromeObject = function() {
var y = this.getRandomInt(0, (sY - h < 0 ? parseInt(sY * 0.2) : (sY - h))); var y = this.getRandomInt(0, (sY - h < 0 ? parseInt(sY * 0.2) : (sY - h)));
this.oAutoIt.WinMove(title, "", x, y, w, h); this.oAutoIt.WinMove(title, "", x, y, w, h);
// release focus // blur
this.blur(); this.blur();
}; };
this.blur = function() {
return this.evaluate("window.blur()");
};
this.downMouseWheel = function(times) { this.downMouseWheel = function(times) {
if (this.debuggingPort > 0) { if (this.debuggingPort > 0) {
@ -536,7 +531,7 @@ var ChromeObject = function() {
}; };
this.getElementPosition = function(selector) { 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(','); var pos = result.split(',');
if (pos.length == 6) { if (pos.length == 6) {
return { return {
@ -544,8 +539,8 @@ var ChromeObject = function() {
"y": parseInt(pos[1]), "y": parseInt(pos[1]),
"a": parseInt(pos[2]), "a": parseInt(pos[2]),
"b": parseInt(pos[3]), "b": parseInt(pos[3]),
"g": parseInt(pos[4]), "w": parseInt(pos[4]),
"d": parseInt(pos[5]) "h": parseInt(pos[5])
}; };
} else { } else {
return { return {
@ -561,10 +556,10 @@ var ChromeObject = function() {
this.getNestedElementPosition = function(selector, subSelector, searchText, startIndex) { this.getNestedElementPosition = function(selector, subSelector, searchText, startIndex) {
var s = ''; var s = '';
var startIndex = (typeof startIndex !== 'undefined' ? startIndex : 0); var startIndex = (typeof startIndex !== 'undefined' ? startIndex : 0);
if (searchText.indexOf(':tokenize(') == 0) { if (searchText.indexOf(':tokenize(') == 0) {
searchText = searchText.substring(searchText.indexOf('(') + 1, searchText.lastIndexOf(')')); searchText = searchText.substring(searchText.indexOf('(') + 1, searchText.lastIndexOf(')'));
s += '(function() {' s += '(function() {'
+ ' var elements = Object.values(document.querySelectorAll("' + selector + '")).filter(function(x) {' + ' var elements = Object.values(document.querySelectorAll("' + selector + '")).filter(function(x) {'
+ ' var el = x.querySelector("' + subSelector + '");' + ' 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(')'))); var p = parseFloat(searchText.substring(searchText.indexOf('(') + 1, searchText.lastIndexOf(')')));
if (p > 0) { if (p > 0) {
s += '(function() {' s += '(function() {'
+ ' var elements = Object.values(document.querySelectorAll("' + selector + '")).filter(function(x) {' + ' var elements = Object.values(document.querySelectorAll("' + selector + '")).filter(function(x) {'
+ ' return (Math.random() < ' + p + ');' + ' return (Math.random() < ' + p + ');'
+ ' ' + (startIndex > 0 ? '}).slice(' + startIndex + ');' : '});') + ' ' + (startIndex > 0 ? '}).slice(' + startIndex + ');' : '});')
+ ' if (elements.length > 0) {' + ' if (elements.length > 0) {'
+ ' var rect = elements[0].getBoundingClientRect();' + ' 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(",");' + ' 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 { } else {
s += '(function() {' s += '(function() {'
+ ' var elements = Object.values(document.querySelectorAll("' + selector + '"));' + ' var elements = Object.values(document.querySelectorAll("' + selector + '"));'
+ ' ' + (startIndex > 0 ? 'elements = elements.slice(' + startIndex + ');' : '') + ' ' + (startIndex > 0 ? 'elements = elements.slice(' + startIndex + ');' : '')
+ ' if (elements.length > 0) {' + ' if (elements.length > 0) {'
+ ' var k = Math.floor(Math.random() * elements.length);' + ' var k = Math.floor(Math.random() * elements.length);'
+ ' var rect = elements[k].getBoundingClientRect();' + ' var rect = elements[k].getBoundingClientRect();'
@ -624,7 +619,7 @@ var ChromeObject = function() {
+ ' return "";' + ' return "";'
+ ' }' + ' }'
+ '})()' + '})()'
; ;
} }
var result = this.getEvaluatedValue(s); var result = this.getEvaluatedValue(s);
@ -649,13 +644,13 @@ var ChromeObject = function() {
}; };
} }
}; };
this.triggerEventOnNestedElement = function(eventName, selector, subSelector, searchText, startIndex) { this.triggerEventOnNestedElement = function(eventName, selector, subSelector, searchText, startIndex) {
var s = ''; var s = '';
var startIndex = (typeof startIndex !== 'undefined' ? startIndex : 0); var startIndex = (typeof startIndex !== 'undefined' ? startIndex : 0);
if (searchText.indexOf(':tokenize(') == 0) { if (searchText.indexOf(':tokenize(') == 0) {
searchText = searchText.substring(searchText.indexOf('(') + 1, searchText.lastIndexOf(')')); searchText = searchText.substring(searchText.indexOf('(') + 1, searchText.lastIndexOf(')'));
s += '(function() {' s += '(function() {'
+ ' var elements = Object.values(document.querySelectorAll("' + selector + '")).filter(function(x) {' + ' var elements = Object.values(document.querySelectorAll("' + selector + '")).filter(function(x) {'
+ ' var el = x.querySelector("' + subSelector + '");' + ' 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(')'))); var p = parseFloat(searchText.substring(searchText.indexOf('(') + 1, searchText.lastIndexOf(')')));
if (p > 0) { if (p > 0) {
s += '(function() {' s += '(function() {'
+ ' var elements = Object.values(document.querySelectorAll("' + selector + '")).filter(function(x) {' + ' var elements = Object.values(document.querySelectorAll("' + selector + '")).filter(function(x) {'
+ ' return (Math.random() < ' + p + ');' + ' return (Math.random() < ' + p + ');'
+ ' ' + (startIndex > 0 ? '}).slice(' + startIndex + ');' : '});') + ' ' + (startIndex > 0 ? '}).slice(' + startIndex + ');' : '});')
+ ' if (elements.length > 0) {' + ' if (elements.length > 0) {'
+ ' elements[0].' + (eventName == 'click' ? 'click()' : 'dispatchEvent(new Event("' + eventName + '"))') + ';' + ' elements[0].' + (eventName == 'click' ? 'click()' : 'dispatchEvent(new Event("' + eventName + '"))') + ';'
+ ' }' + ' }'
@ -683,7 +678,7 @@ var ChromeObject = function() {
} else { } else {
s += '(function() {' s += '(function() {'
+ ' var elements = Object.values(document.querySelectorAll("' + selector + '"));' + ' var elements = Object.values(document.querySelectorAll("' + selector + '"));'
+ ' ' + (startIndex > 0 ? 'elements = elements.slice(' + startIndex + ');' : '') + ' ' + (startIndex > 0 ? 'elements = elements.slice(' + startIndex + ');' : '')
+ ' if (elements.length > 0) {' + ' if (elements.length > 0) {'
+ ' var k = Math.floor(Math.random() * elements.length);' + ' var k = Math.floor(Math.random() * elements.length);'
+ ' elements[k].' + (eventName == 'click' ? 'click()' : 'dispatchEvent(new Event("' + eventName + '"))') + ';' + ' 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 + '"))') + ';' + ' elements[0].' + (eventName == 'click' ? 'click()' : 'dispatchEvent(new Event("' + eventName + '"))') + ';'
+ ' }' + ' }'
+ '})()' + '})()'
; ;
} }
return this.evaluate(s); return this.evaluate(s);
@ -743,7 +738,7 @@ var ChromeObject = function() {
this.isAttached = isAttached; this.isAttached = isAttached;
return this; return this;
}; };
this.getRandomInt = function(min, max) { this.getRandomInt = function(min, max) {
min = Math.ceil(min); min = Math.ceil(min);
max = Math.floor(max); max = Math.floor(max);
@ -839,7 +834,8 @@ var ChromeObject = function() {
}; };
this.reload = function() { this.reload = function() {
return this.sendPageRPC("Page.reload", {}); //return this.sendPageRPC("Page.reload", {});
return this.evaluate("window.reload()");
}; };
this.hasClass = function(seletctor, className) { this.hasClass = function(seletctor, className) {
@ -863,14 +859,19 @@ var ChromeObject = function() {
this.oAutoIt.Send("{SPACE}"); this.oAutoIt.Send("{SPACE}");
}; };
this.setValue = function(selector, value, searchIndex) { this.setValue = function(selector, value, repeat, searchIndex) {
var searchIndex = (typeof searchIndex !== "undefined" ? searchIndex : 0); var s = encodeURIComponent(value),
var s = encodeURIComponent(value); i = 0,
searchIndex = (typeof searchIndex !== "undefined" ? searchIndex : 0)
if (searchIndex > 0) { ;
this.evaluate('Object.values(document.querySelectorAll("' + selector + '"))[' + searchIndex + '].value = decodeURIComponent("' + s + '");');
} else { while (i < repeat) {
this.evaluate('document.querySelector("' + selector + '").value = decodeURIComponent("' + s + '")'); 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'); 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(); this.create();
}; };