diff --git a/lib/chrome.js b/lib/chrome.js index 09fafa1..db3f5a8 100644 --- a/lib/chrome.js +++ b/lib/chrome.js @@ -36,7 +36,7 @@ var ChromeObject = function() { this.setProfile = function(profileName, installedDir) { this.profileName = (profileName == "Default" ? "Chrome" : profileName); - this.installedDir = installedDir; + this.setInstalledDir(installedDir); this.workingDirectory = this.workingDirectory.replace(":installedDir", this.installedDir); this.binPath = this.binPath.replace(":installedDir", this.installedDir); //this.binPath = this.binPath.replace(":installedDir", "Chrome"); @@ -44,12 +44,18 @@ var ChromeObject = function() { }; this.setUserDataDir = function(dirname) { - this.userDataDir = dirname; + if (dirname != null) { + this.userDataDir = dirname; + } else { + this.userDataDir = SHELL.getPathOfMyDocuments() + "\\UserData_Chrome_" + this.profileName; + } return this; }; this.setInstalledDir = function(dirname) { - this.installedDir = dirname; + if (dirname != null) { + this.installedDir = dirname; + } return this; }; @@ -75,6 +81,7 @@ var ChromeObject = function() { this.setDebuggingPort = function(n) { this.debuggingPort = (typeof(n) !== "number" ? this.debuggingPort : n); + return this; } this.setPageId = function(s) { @@ -181,7 +188,11 @@ var ChromeObject = function() { } cmd.push("--profile-directory=\"" + this.profileName + "\""); - cmd.push("--proxy-server=\"" + this.proxy.protocol + "://" + this.proxy.host + ":" + this.proxyPort + "\""); + + if (this.proxy != null) { + cmd.push("--proxy-server=\"" + this.proxy.protocol + "://" + this.proxy.host + ":" + this.proxyPort + "\""); + } + cmd.push("--user-data-dir=\"" + this.userDataDir + "\""); cmd.push("\"" + url + "\""); shell.runAs(this.binPath, cmd); @@ -198,7 +209,7 @@ var ChromeObject = function() { this.getPageList = function() { if (this.debuggingPort > 0) { - return HTTP.get("http://127.0.0.1:" + this.debuggingPort + "/json"); + return JSON.parse(HTTP.get("http://127.0.0.1:" + this.debuggingPort + "/json")); } else { console.error("Remote debugging unavailable"); return []; @@ -225,19 +236,33 @@ var ChromeObject = function() { }; this.sendPageRPC = function(method, params) { - if (this.pageId != "") { - this.ws.send("ws://127.0.0.1:" + this.debuggingPort + "/devtools/page/" + this.pageId, JSON.stringify({ - "id": pageEventId, - "method": method, - "params": params - })); - pageEventId++; - console.info("ChromeObject().sendPageRPC() -> Sent"); - } else { - console.error("pageId not specified"); + var result = null; + + try { + if (this.pageId != "") { + result = this.ws.send("ws://127.0.0.1:" + this.debuggingPort + "/devtools/page/" + this.pageId, JSON.stringify({ + "id": pageEventId, + "method": method, + "params": params + })); + pageEventId++; + console.info("ChromeObject().sendPageRPC() -> Sent"); + } else { + var pageList = this.getPageList(); + if (pageList instanceof Array && pageList.length > 0) { + this.pageId = pageList[0].id; + if (this.pageId != "") { + result = this.sendPageRPC(method, params); + } else { + console.error("Got invaild list of pages"); + } + } + } + } catch (e) { + console.log("ChromeObject.sendPageRPC() -> " + e.message); } - return this; + return result; }; this.navigate = function(url) { @@ -247,14 +272,39 @@ var ChromeObject = function() { }; this.evaluate = function(expression) { - return this.sendPageRPC("Runtime.evaluate", { - "expression": expression - }); + try { + return this.sendPageRPC("Runtime.evaluate", { + "expression": expression + }); + } catch (e) { + console.error("ChromeObject.evaluate() -> " + e.message); + } + }; + + this.close = function() { + return this.sendPageRPC("Browser.close", {}); }; this.focus = function() { - return this.evaluate("window.focus()"); + if (this.debuggingPort > 0) { + try { + var oAutoIt = CreateObject("AutoItX3.Control"); + var pageList = this.getPageList(); + for (var i = 0; i < pageList.length; i++) { + if (pageList[i].id == this.pageId) { + oAutoIt.WinActivate(pageList[i].title); + } + } + } catch (e) { + console.error("ChromeObject.focus() -> " + e.message); + } + } }; + + this.blur = function() { + return this.evaluate("window.blur()"); + }; + }; exports.create = function() { @@ -266,17 +316,15 @@ exports.start = function(url, proxyPort, profileName, userDataDir, installedDir) .setProxyPort(proxyPort) .setProfile(profileName, installedDir) .setUserDataDir(userDataDir) - .setInstalledDir(installedDir) .open(url) ; }; -exports.startWithDebugging = function(url, proxy, profileName, userDataDir, installedDir, debuggingPort) { +exports.startWithDebugging = function(url, proxy, profileName, debuggingPort) { return (new ChromeObject()) .setProxy(proxy) - .setProfile(profileName, installedDir) - .setUserDataDir(userDataDir) - .setInstalledDir(installedDir) + .setProfile(profileName, null) + .setUserDataDir(null) .setDebuggingPort(debuggingPort) .open(url) ;