Update chrome.js

This commit is contained in:
Namhyeon Go 2021-08-11 15:55:49 +09:00 committed by GitHub
parent d015816a12
commit c9c5c83c8c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -350,9 +350,15 @@ var ChromeObject = function() {
} }
}; };
this.close = function() { this.exit = function() {
return this.sendPageRPC("Browser.close", {}); return this.sendPageRPC("Browser.close", {});
}; };
this.close = function() {
var response = this.sendPageRPC("Page.close", {});
this.setPageId(null);
return response;
};
this.terminate = function() { this.terminate = function() {
try { try {
@ -640,15 +646,40 @@ var ChromeObject = function() {
return this.evaluate('document.querySelector("' + selector + '").dispatchEvent(new Event("' + eventName + '"))'); return this.evaluate('document.querySelector("' + selector + '").dispatchEvent(new Event("' + eventName + '"))');
} }
} else { } else {
var probability = parseFloat(selector.substring(selector.indexOf('(') + 1, selector.indexOf(')'))); var p = parseFloat(selector.substring(selector.indexOf('(') + 1, selector.indexOf(')')));
return this.evaluate('(function(obj, p) { var a = Array.from(obj).filter(function() { return (Math.random() < p); }); if(a.length > 0) a[0].click(); })(document.querySelectorAll("' + selector.substring(0, selector.indexOf(':')) + '"), ' + probability + ')'); var _selector = selector.substring(0, selector.indexOf(':'));
return this.evaluate('(function(obj, p) { var element = Object.values(obj).find(function() { return (Math.random() < p); }); if(element) element.click(); })(document.querySelectorAll("' + _selector + '"), ' + p + ')');
} }
}; };
this.scrollBySelector = function(selector, dx, dy) { this.triggerEventByFind = function(eventName, selector, searchText) {
return this.evaluate('(function(rect, dx, dy) { window.scrollTo(rect.x + dx, rect.y + dy); })(document.querySelector("' + selector + '").getBoundingClientRect(), parseInt("' + (dx || 0) + '"), parseInt("' + (dy || 0) + '"))'); var s = '(function() {'
+ ' var element = Object.values(document.querySelectorAll("' + selector + '")).find(function(x) {'
+ ' return (x.innerText.indexOf(decodeURIComponent("' + encodeURIComponent(searchText) + '")) > -1);'
+ ' });'
+ ' if (element) {'
+ ' element.' + (eventName == 'click' ? 'click()' : 'dispatchEvent(new Event("' + eventName + '"))') + ';'
+ ' }'
+ '})()'
;
return this.evaluate(s);
}; };
this.triggerEventOnNestedFind = function(eventName, selector, subSelector, searchText) {
var s = '(function() {'
+ ' var element = Object.values(document.querySelectorAll("' + selector + '")).find(function(x) {'
+ ' return (x.querySelector("' + subSelector + '").innerText.indexOf(decodeURIComponent("' + encodeURIComponent(searchText) + '")) > -1);'
+ ' });'
+ ' if (element) {'
+ ' element.' + (eventName == 'click' ? 'click()' : 'dispatchEvent(new Event("' + eventName + '"))') + ';'
+ ' }'
+ '})()'
;
return this.evaluate(s);
};
this.scrollTo = function(x, y) { this.scrollTo = function(x, y) {
return this.evaluate('window.scrollTo(parseInt(' + x + '), parseInt(' + y + '))'); return this.evaluate('window.scrollTo(parseInt(' + x + '), parseInt(' + y + '))');
}; };
@ -657,6 +688,10 @@ var ChromeObject = function() {
return this.evaluate('window.scrollBy(parseInt(' + dx + '), parseInt(' + dy + '))'); return this.evaluate('window.scrollBy(parseInt(' + dx + '), parseInt(' + dy + '))');
}; };
this.scrollToElement = function(selector, dx, dy) {
return this.evaluate('(function(rect, dx, dy) { window.scrollTo(rect.x + dx, rect.y + dy); })(document.querySelector("' + selector + '").getBoundingClientRect(), parseInt("' + dx + '"), parseInt("' + dy + '"))');
};
this.reload = function() { this.reload = function() {
return this.sendPageRPC("Page.reload", {}); return this.sendPageRPC("Page.reload", {});
}; };
@ -684,15 +719,15 @@ var ChromeObject = function() {
this.setValue = function(selector, value, repeat) { this.setValue = function(selector, value, repeat) {
var s = encodeURIComponent(value), i = 0; var s = encodeURIComponent(value), i = 0;
while (i < repeat) { while (i < repeat) {
this.evaluate('document.querySelector("' + selector + '").value = decodeURIComponent("' + s + '")'); this.evaluate('document.querySelector("' + selector + '").value = decodeURIComponent("' + s + '")');
i++; i++;
} }
};
this.getText = function(selector) {
return this.getEvaluatedValue('document.querySelector("' + selector + '").innerText');
}; };
this.getText = function(selector) {
return this.getEvaluatedValue('document.querySelector("' + selector + '").innerText');
};
this.create(); this.create();
}; };