1 Poltergeist = (function () {
4 * The MAIN class of the project
11 function Poltergeist(port, width, height, jsErrors) {
13 this.browser = new Poltergeist.Browser(this, width, height, jsErrors);
15 this.commandServer = new Poltergeist.Server(this, port);
16 this.commandServer.start();
20 phantom.onError = function (message, stack) {
21 return self.onError(message, stack);
28 * Tries to execute a command send by a client and returns the command response
29 * or error if something happened
31 * @param serverResponse
34 Poltergeist.prototype.serverRunCommand = function (command, serverResponse) {
38 return this.browser.serverRunCommand(command, serverResponse);
41 if (error instanceof Poltergeist.Error) {
42 return this.serverSendError(error, serverResponse);
44 return this.serverSendError(new Poltergeist.BrowserError(error.toString(), error.stack), serverResponse);
49 * Sends error back to the client
51 * @param serverResponse
54 Poltergeist.prototype.serverSendError = function (error, serverResponse) {
58 name: error.name || 'Generic',
59 args: error.args && error.args() || [error.toString()]
62 return this.commandServer.sendError(serverResponse, 500, errorObject);
66 * Send the response back to the client
67 * @param response Data to send to the client
68 * @param serverResponse Phantomjs response object associated to the client request
71 Poltergeist.prototype.serverSendResponse = function (response, serverResponse) {
72 return this.commandServer.send(serverResponse, {response: response});
78 window.Poltergeist = Poltergeist;