diff --git a/lib/chrome.js b/lib/chrome.js index 4d28ac3..4f674f0 100644 --- a/lib/chrome.js +++ b/lib/chrome.js @@ -362,13 +362,15 @@ var ChromeObject = function() { this.terminate = function() { try { - this.oAutoIt.WinKill(this.title); + this.oAutoIt.WinKill(this.getTitle()); } catch (e) { console.error("ChromeObject.terminate() ->", e.message); } }; this.focus = function() { + var title = ""; + if (this.debuggingPort > 0) { try { // set page id @@ -379,25 +381,28 @@ var ChromeObject = function() { // get current title this.title = this.getTitle(); - // new title - var _title = this.title + " " + this.pageId.substring(0, 6); + // will be change title + title = this.title + " " + this.pageId.substring(0, 6); // change webpage title for focusing window - this.setTitle(_title); + this.setTitle(title); // find window by title var pageList = this.getPageList(); if (pageList.length > 0) { - this.oAutoIt.WinActivate(_title); + this.oAutoIt.WinActivate(title); } } catch (e) { console.error("ChromeObject._focus() ->", e.message); } } + + return title; }; + this.blur = function() { - this.setTitle(this.title); + return this.setTitle(this.title); }; this.getScrollHeight = function(selector) { @@ -423,24 +428,38 @@ var ChromeObject = function() { } }; - this.autoAdjust = function(sX, sY) { - // get current title - var _title = this.getTitle(); - - // change webpage title - this.setTitle(this.pageId); + this.autoAdjustByScreen = function(sX, sY, divX, divY) { + // catch focus + var title = this.focus(); + sleep(300); // adjust window position and size - var bX = Math.floor(sX / 6); - var bY = Math.floor(sY / 3); + var bX = Math.floor(sX / divX); + var bY = Math.floor(sY / divY); var x = this.getRandomInt(0, bX); var y = this.getRandomInt(0, bY); var w = this.getRandomInt(bX * 3, sX - bX); var h = this.getRandomInt(bY, sY - bY); - this.oAutoIt.WinMove(this.pageId, "", x, y, w, h); + this.oAutoIt.WinMove(title, "", x, y, w, h); - // change webpage title to original - this.setTitle(_title); + // release focus + this.blur(); + }; + + this.autoAdjustByWindow = function(sX, sY, w1, w2, h1, h2) { + // catch focus + var title = this.focus(); + sleep(300); + + // adjust window position and size + var w = this.getRandomInt(w1, w2); + var h = this.getRandomInt(h1, h2); + var x = this.getRandomInt(0, (sX - w < 0 ? parseInt(sX * 0.2) : (sX - w))); + var y = this.getRandomInt(0, (sY - h < 0 ? parseInt(sY * 0.2) : (sY - h))); + this.oAutoIt.WinMove(title, "", x, y, w, h); + + // release focus + this.blur(); }; this.blur = function() {