diff --git a/lib/chrome.js b/lib/chrome.js index 5e99bbc..3f8c88c 100644 --- a/lib/chrome.js +++ b/lib/chrome.js @@ -4,17 +4,22 @@ var SHELL = require("lib/shell"); var SYS = require("lib/system"); var FILE = require("lib/file"); +var HTTP = require("lib/http"); var ChromeObject = function() { this.workingDirectory = SYS.getEnvString("PROGRAMFILES") + "\\Google\\Chrome\\Application"; this.binPath = SYS.getEnvString("PROGRAMFILES") + "\\Google\\:installedDir\\Application\\chrome.exe"; - this.processID = 0; + //this.processID = 0; this.installedDir = "Chrome"; this.profileName = "Default"; this.userDataDir = null; - this.proxyPort = 1080; + this.proxy = { + "protocol": "socks5", + "host": "127.0.0.1", + "port": 1080 + }; this.inPrivate = false; - this.processList = []; + this.debuggingPort = 0; this.setBinPath = function(path) { this.binPath = path; @@ -24,12 +29,12 @@ var ChromeObject = function() { this.setProfile = function(profileName, installedDir) { this.profileName = (profileName == "Default" ? "Chrome" : profileName); this.installedDir = installedDir; - //this.workingDirectory = this.workingDirectory.replace(":installedDir", this.installedDir); - //this.binPath = this.binPath.replace(":installedDir", this.installedDir); - this.binPath = this.binPath.replace(":installedDir", "Chrome"); + this.workingDirectory = this.workingDirectory.replace(":installedDir", this.installedDir); + this.binPath = this.binPath.replace(":installedDir", this.installedDir); + //this.binPath = this.binPath.replace(":installedDir", "Chrome"); return this; }; - + this.setUserDataDir = function(dirname) { this.userDataDir = dirname; return this; @@ -39,45 +44,57 @@ var ChromeObject = function() { this.installedDir = dirname; return this; }; - - this.setProxyPort = function(s) { - this.proxyPort = s; + + this.setProxy = function(obj) { + this.proxy = obj; return this; }; - this.getProcessList = function() { - return this.processList; + this.setProxyProtocol = function(s) { + this.proxy.protocol = s; + return this; }; + this.setProxyHost = function(s) { + this.proxy.host = s; + return this; + }; + + this.setProxyPort = function(n) { + this.proxy.port = n; + return this; + }; + + this.setDebuggingPort = function(n) { + this.debuggingPort = n; + } + this.createShoutcut = function(url) { if (!this.userDataDir) { this.userDataDir = SHELL.getPathOfMyDocuments() + "\\UserData_Chrome_" + this.profileName; } - if (!this.inPrivate) { - SHELL.createDesktopIcon("Chrome Prototype (" + this.installedDir + ")", [ - "cscript", - "app.js", - "shoutcut", - "chrome", - this.installedDir, - "--profile-directory=\"" + this.profileName + "\"", - "--user-data-dir=\"" + this.userDataDir + "\"", - "\"" + url + "\"" - ].join(' '), SYS.getCurrentScriptDirectory()); - } else { - SHELL.createDesktopIcon("Chrome Prototype (" + this.installedDir + ")", [ - "cscript", - "app.js", - "shoutcut", - "chrome", - this.installedDir, - "--incognito", - "--profile-directory=\"" + this.profileName + "\"", - "--user-data-dir=\"" + this.userDataDir + "\"", - "\"" + url + "\"" - ].join(' '), SYS.getCurrentScriptDirectory()); + var cmd = [ + "cscript", + "app.js", + "shoutcut", + "chrome", + this.installedDir, + ]; + + if (this.inPrivate) { + cmd.push("--incognito"); } + + if (this.debuggingPort > 0) { + cmd.push("--remote-debugging-port=" + this.debuggingPort); + } + + cmd.push("--profile-directory=\"" + this.profileName + "\""); + cmd.push("--user-data-dir=\"" + this.userDataDir + "\""); + cmd.push("\"" + url + "\""); + + SHELL.createDesktopIcon("Chrome Prototype (" + this.installedDir + ")", cmd.join(' '), SYS.getCurrentScriptDirectory()); }; this.setInPrivate = function(flag) { @@ -138,23 +155,21 @@ var ChromeObject = function() { var shell = SHELL.create(); shell.setWorkingDirectory(this.workingDirectory); - - if (!this.inPrivate) { - shell.runAs(this.binPath, [ - "--profile-directory=\"" + this.profileName + "\"", - "--proxy-server=\"socks5://127.0.0.1:" + this.proxyPort + "\"", - "--user-data-dir=\"" + this.userDataDir + "\"", - "\"" + url + "\"" - ]); - } else { - shell.runAs(this.binPath, [ - "--incognito", - "--profile-directory=\"" + this.profileName + "\"", - "--proxy-server=\"socks5://127.0.0.1:" + this.proxyPort + "\"", - "--user-data-dir=\"" + this.userDataDir + "\"", - "\"" + url + "\"" - ]); + + var cmd = []; + + if (this.inPrivate) { + cmd.push("--incognito"); } + + if (this.debuggingPort > 0) { + cmd.push("--remote-debugging-port=" + this.debuggingPort); + } + + cmd.push("--profile-directory=\"" + this.profileName + "\""); + cmd.push("--proxy-server=\"" + this.proxy.protocol + "://" + this.proxy.host + ":" + this.proxyPort + "\""); + cmd.push("--user-data-dir=\"" + this.userDataDir + "\""); + cmd.push("\"" + url + "\""); sleep(1500); shell.release(); @@ -165,14 +180,38 @@ var ChromeObject = function() { return this; }; + + this.getPageList = function() { + if (this.debuggingPort > 0) { + return HTTP.get("http://localhost:" + this.debuggingPort + "/json"); + } else { + console.error("Remote debugging unavailable"); + return []; + } + }; + + this.getPageById = function(id) { + var pages = pageList.filter(function(x) { + return (pageList[k].id = id); + }); + return (pages.length > 0 ? pages[0] : null); + }; + + this.getPagesByUrl = function(url) { + return pageList.filter(function(x) { + return (x.url == url); + }); + }; + + this.getPagesByTitle = function(title) { + return pageList.filter(function(x) { + return (x.title == title); + }); + }; }; exports.create = function() { - return new ChromeObject(); -}; - -exports.getProcessIDs = function() { - return (new ChromeObject()).getProcessIDs(); + return new ChromeObject(); }; exports.start = function(url, proxyPort, profileName, userDataDir, installedDir) { @@ -181,7 +220,16 @@ exports.start = function(url, proxyPort, profileName, userDataDir, installedDir) .setProfile(profileName, installedDir) .setUserDataDir(userDataDir) .setInstalledDir(installedDir) - .open(url) - .processID + .open(url); ; }; + +exports.startWithDebugging = function(url, proxy, profileName, userDataDir, installedDir, debuggingPort) { + return (new ChromeObject()) + .setProxy(proxy) + .setProfile(profileName, installedDir) + .setUserDataDir(userDataDir) + .setInstalledDir(installedDir) + .setDebuggingPort(debuggingPort) + .open(url); +};