Update chrome.js

This commit is contained in:
Namhyeon Go 2021-07-28 05:16:38 +09:00 committed by GitHub
parent f46b420bd1
commit 85fbab4d3a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -291,6 +291,10 @@ var ChromeObject = function() {
"url": url "url": url
}); });
}; };
this.navigateEvaluately = function(url) {
return this.evaluate('location.href = "' + url + '"');
};
this.evaluate = function(expression) { this.evaluate = function(expression) {
try { try {
@ -302,6 +306,15 @@ var ChromeObject = function() {
} }
}; };
this.getEvaluatedValue = function(expression) {
try {
var response = this.evaluate(expression);
return JSON.parse(response).result.result.value;
} catch (e) {
console.error("ChromeObject.getEvaluatedValue() -> " + e.message);
}
};
this.close = function() { this.close = function() {
return this.sendPageRPC("Browser.close", {}); return this.sendPageRPC("Browser.close", {});
}; };
@ -314,6 +327,23 @@ var ChromeObject = function() {
}; };
this.focus = function() { this.focus = function() {
if (this.debuggingPort > 0) {
try {
// get current title
var _title = this.getTitle();
// implementation of focus()
this._focus();
// change webpage title to original
this.setTitle(_title);
} catch (e) {
console.error("ChromeObject.focus() -> " + e.message);
}
}
};
this._focus = function() {
if (this.debuggingPort > 0) { if (this.debuggingPort > 0) {
try { try {
// if page ID is empty // if page ID is empty
@ -329,10 +359,8 @@ var ChromeObject = function() {
// find window by title // find window by title
var pageList = this.getPageList(); var pageList = this.getPageList();
for (var i = 0; i < pageList.length; i++) { if (pageList.length > 0) {
if (pageList[i].id == this.pageId) { this.oAutoIt.WinActivate(this.pageId);
this.oAutoIt.WinActivate(pageList[i].title);
}
} }
} catch (e) { } catch (e) {
console.error("ChromeObject.focus() -> " + e.message); console.error("ChromeObject.focus() -> " + e.message);
@ -412,14 +440,12 @@ var ChromeObject = function() {
this.getTitle = function() { this.getTitle = function() {
if (this.debuggingPort > 0) { if (this.debuggingPort > 0) {
var response = this.evaluate('document.title'); return this.getEvaluatedValue('document.title');
return JSON.parse(response).result.result.value;
} }
}; };
this.getScreenPosition = function() { this.getScreenPosition = function() {
var response = this.evaluate('(function() { return [window.screenX, window.screenY].join(","); })();'); var result = this.getEvaluatedValue('(function() { return [window.screenX, window.screenY].join(","); })();');
var result = JSON.parse(response).result.result.value;
var pos = result.split(','); var pos = result.split(',');
return { return {
"x": parseInt(pos[0]), "x": parseInt(pos[0]),
@ -431,8 +457,7 @@ var ChromeObject = function() {
var height = 0; var height = 0;
if (this.debuggingPort > 0) { if (this.debuggingPort > 0) {
var response = this.evaluate('(function(obj) { return Math.max(obj.scrollHeight, obj.clientHeight); })(document.querySelector("html"))'); var result = this.getEvaluatedValue('(function(obj) { return Math.max(obj.scrollHeight, obj.clientHeight); })(document.querySelector("html"))');
var result = JSON.parse(response).result.result.value;
height = parseInt(result); height = parseInt(result);
} }
@ -467,7 +492,7 @@ var ChromeObject = function() {
var page = this.getPageById(this.pageId); var page = this.getPageById(this.pageId);
return page.url; return page.url;
}; };
this.triggerEvent = function(eventName, selector) { this.triggerEvent = function(eventName, selector) {
return this.evaluate('document.querySelector("' + selector + '").dispatchEvent(new Event("' + eventName + '"))'); return this.evaluate('document.querySelector("' + selector + '").dispatchEvent(new Event("' + eventName + '"))');
}; };
@ -481,6 +506,12 @@ var ChromeObject = function() {
this.reload = function() { this.reload = function() {
return this.sendPageRPC("Page.reload", {}); return this.sendPageRPC("Page.reload", {});
}; };
this.hasClass = function(selector, className) {
var result = this.getEvaluatedValue('document.querySelector(".html5-video-player").getAttribute("class")');
var classNames = result.split(' ');
return (classNames.indexOf(className) > -1);
};
this.create(); this.create();
}; };
@ -507,3 +538,7 @@ exports.startWithDebugging = function(url, proxy, profileName, debuggingPort) {
.open(url) .open(url)
; ;
}; };
exports.VERSIONINFO = "Chrome Web Browser Debugging Interface (chrome.js) version 0.1";
exports.global = global;
exports.require = global.require;