diff --git a/lib/chrome.js b/lib/chrome.js index 8a656d3..3be1448 100644 --- a/lib/chrome.js +++ b/lib/chrome.js @@ -291,6 +291,10 @@ var ChromeObject = function() { "url": url }); }; + + this.navigateEvaluately = function(url) { + return this.evaluate('location.href = "' + url + '"'); + }; this.evaluate = function(expression) { 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() { return this.sendPageRPC("Browser.close", {}); }; @@ -314,6 +327,23 @@ var ChromeObject = 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) { try { // if page ID is empty @@ -329,10 +359,8 @@ var ChromeObject = function() { // find window by title var pageList = this.getPageList(); - for (var i = 0; i < pageList.length; i++) { - if (pageList[i].id == this.pageId) { - this.oAutoIt.WinActivate(pageList[i].title); - } + if (pageList.length > 0) { + this.oAutoIt.WinActivate(this.pageId); } } catch (e) { console.error("ChromeObject.focus() -> " + e.message); @@ -412,14 +440,12 @@ var ChromeObject = function() { this.getTitle = function() { if (this.debuggingPort > 0) { - var response = this.evaluate('document.title'); - return JSON.parse(response).result.result.value; + return this.getEvaluatedValue('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 result = this.getEvaluatedValue('(function() { return [window.screenX, window.screenY].join(","); })();'); var pos = result.split(','); return { "x": parseInt(pos[0]), @@ -431,8 +457,7 @@ var ChromeObject = 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; + var result = this.getEvaluatedValue('(function(obj) { return Math.max(obj.scrollHeight, obj.clientHeight); })(document.querySelector("html"))'); height = parseInt(result); } @@ -467,7 +492,7 @@ var ChromeObject = function() { var page = this.getPageById(this.pageId); return page.url; }; - + this.triggerEvent = function(eventName, selector) { return this.evaluate('document.querySelector("' + selector + '").dispatchEvent(new Event("' + eventName + '"))'); }; @@ -481,6 +506,12 @@ var ChromeObject = function() { this.reload = function() { 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(); }; @@ -507,3 +538,7 @@ exports.startWithDebugging = function(url, proxy, profileName, debuggingPort) { .open(url) ; }; + +exports.VERSIONINFO = "Chrome Web Browser Debugging Interface (chrome.js) version 0.1"; +exports.global = global; +exports.require = global.require;