Skip to content

Commit

Permalink
initial extern, not tested
Browse files Browse the repository at this point in the history
  • Loading branch information
Justin Donaldson committed Jul 12, 2012
0 parents commit 1fea249
Show file tree
Hide file tree
Showing 8 changed files with 389 additions and 0 deletions.
Empty file added build.hxml
Empty file.
40 changes: 40 additions & 0 deletions src/js/phantom/FileSystem.hx
@@ -0,0 +1,40 @@
package js.phantom;

@:native("require('fs')");
extern class FileSystem {
//Read-only properties
public static var separator(default,null):String; // is the path separator (forward slash or backslash, depending on the operating system).
public static var workingDirectory(default,null):String; // is the current working directory.

//Query functions
public static function list(path):Array<String>; // returns the list of all the names of all the files in a specified path.
public static function absolute(path):String // returns the absolute path starting from the root file system, resolved from the current working directory.
public static function exists(path):Bool // returns true if a file or a directory exists.
public static function isDirectory(path):Bool // returns true if the specified path is a directory.
public static function isFile(path):Bool // returns true if the specified path is a file.
public static function isAbsolute(path):Bool // returns true if the specified path is an absolute path.
public static function isExecutable(path):Bool // returns true if the specified file can be executed.
public static function isReadable(path):Bool // returns true if a file or a directory is readable.
public static function isWritable(path):Bool // returns true if a file or a directory is writeable.
public static function isLink(path):Bool // returns true if the specified path is a symbolic link.
public static function readLink(path):String // returns the target of a symbolic link.

//Directory-related functions:
public static function changeWorkingDirectory(path):Void; // changes the current working directory to the specified path.
public static function makeDirectory(path):Void; // creates a new directory.
public static function makeTree(path):Void; // creates a directory including any missing parent directories.
public static function removeDirectory(path):Void; // removes a directory if it is empty
public static function removeTree(path):Void; // removes the specified path, regardless of whether it is a file or a directory.
public static function copyTree(source, destination):Void; // copies the entire files from a source path to the destination path.

//File-related functions:
public static function open(path, mode):StreamObject; // returns a stream object representing the stream interface to the specified file (mode can be r for read, w for write, or a for append).
public static function read(path):String; // returns the entire content of a file.
public static function write(path, content, mode):Void; // writes content to a file (mode can be w for write or a for append).
public static function size(path):Int; // returns the size (in bytes) of the file specified by the path.
public static function remove(path):Void; // removes the file specified by the path.
public static function copy(source, destination):Void; // copies a file to another.
public static function move(source, destination):Void; // movies a file to another, effectively renaming it.
public static function touch(path):Void; // touches a file (i.e. changes its access timestamp).

}
15 changes: 15 additions & 0 deletions src/js/phantom/PhantomJs.hx
@@ -0,0 +1,15 @@
package js.phantom;

@:native("phantom")
extern class PhantomJs {
public static var args:Array<String>; // (array) This read-only property is an array of the arguments passed to the script. Deprecated: Please use system.args from the System module.
public static var libraryPath:String; // (string) This property stores the path which is used by injectJs function to resolve the script name. Initially it is set to the location of the script invoked by PhantomJS.
public static var scriptName:String; //(string) This read-only property stores the name of the invoked script file. Deprecated: Please use system.args[0] from the System module.
public static var version:{major:Int, minor:Int, patch:Int}; // (object) This read-only property holds PhantomJS version. Example value: { major:1, minor:0, patch:0 }.

public static function exit(returnValue:Dynamic); // Exits the program with the specified return value. If no return value is specified, it is set to 0.
public static function injectJs(filename:String):Bool; // Injects external script code from the specified file. If the file can not be found in the current directory, libraryPath is used for additional look up. This function returns true if injection is successful, otherwise it returns false.

}


35 changes: 35 additions & 0 deletions src/js/phantom/Request.hx
@@ -0,0 +1,35 @@

typedef Request={
/**
defines the request method (GET, POST, ...)
**/
public var method:String;

/**
contains the complete request URL, including the query string (if any)
**/
public var url:String;

/**
has the actual HTTP version
**/
public var httpVersion:String;

/**
stores all HTTP headers as key-value pair
**/
public var headers:Hash<String>;

/**
request body (only POST and PUT method)
**/
public var post:String;

/**
if Content-Type is set to
application/x-www-form-urlencoded (default for FORM submit), the content
will be URL-decoded and the undecoded content will be stored in an extra
property postRaw
**/
public var postRaw:String;
}
27 changes: 27 additions & 0 deletions src/js/phantom/Response.hx
@@ -0,0 +1,27 @@

extern class Response {
/**
stores all HTTP headers as key-value pair (set these BEFORE calling write for the first time)
**/
public var headers:Hash<String>;

/**
sets the returned status code
**/
public var statusCode:Int;

/**
sends a chunk for the response body (it can be called multiple times)
**/
public function write(data:String):Void;

/**
close the HTTP connection
To avoid Client detecting a connection drop,
remember to use write() at least once. Sending an empty string (i.e.
write("")) would be enough if the only aim is, for example, to return a
HTTP 200 Success
**/
public function close():Void;
}
13 changes: 13 additions & 0 deletions src/js/phantom/StreamObject.hx
@@ -0,0 +1,13 @@
package js.phantom;

class StreamObject{
//A stream object returned from the open() function has the following functions:

public function read():String; // returns the content of the stream.
public function write(data):Void; // writes the string to the stream.
public function readLine():String; // reads only a line from the stream and return it.
public function writeLine(data):Void; // writes the data as a line to the stream.
public function flush():Void; // flushes all pending input output.
public function close():Void; // completes the stream operation.

r}
252 changes: 252 additions & 0 deletions src/js/phantom/WebPage.hx
@@ -0,0 +1,252 @@
package js.phantom;

/**
WebPage object encapsulates a web page. It is usually instantiated using the
new keyword. The properties, functions, and callbacks of the WebPage object
are described in the following sections.
**/
@:native("WebPage")
extern class WebPage {
/**
This property defines the rectangular area of the web page to be
rasterized when render() is invoked. If no clipping rectangle is set,
render() will process the entire web page. Example: page.clipRect = {
top: 14, left: 3, width: 400, height: 300 }
**/
public var clipRect:{top:Float, left:Float, width:Float, right:Float};

/**
This property stores the content of the web page, enclosed in HTML/XML
element. Setting the property will effectively reload the web page with
the new content.
**/
public var content:String;

/**
This property stores the path which is used by injectJs function to
resolve the script name. Initially it is set to the location of the
script invoked by PhantomJS.
**/
public var libraryPath:String;

/**
defines whether to execute the script in the page or not (default to
true)
**/
public var javascriptEnabled:Bool;

/**
defines whether to load the inlined images or not
**/
public var loadImages:Bool;


/**
defines whether local resource (e.g. from file) can access remote URLs or
not (default to false)
**/
public var localToRemoteUrlAccessEnabled:Bool;

/**
defines the user agent sent to server when the web page requests
resources.
**/
public var userAgent:String;

/**
sets the user name used for HTTP authentication
**/
public var userName:String;

/**
sets the password used for HTTP authentication
**/

public var password:String;

/**
defines whether load requests should be monitored for cross-site
scripting attempts (default to false)
**/
public var XSSAuditingEnabled:Bool;

/**
defines whether web security should be enabled or not (default to true)
**/
public var webSecurityEnabled:Bool;

/**
This property sets the size of the viewport for the layout process. It is
useful to set the preferred initial size before loading the page, e.g. to
choose between landscape vs portrait. Because PhantomJS is headless
(nothing is shown), !viewportSize effectively simulates the size of the
window like in a traditional browser.
Example: page.viewportSize = { width: 480, height: 800 }
**/
public var viewPortSize:{width:Float, height:Float};


/**
Evaluates the given function in the context of the web page. The
execution is sandboxed, the web page has no access to the phantom object
and it can't probe its own setting. Any return value must be of a simple
object, i.e. no function or closure.
Example:
console.log('Page title is ' + page.evaluate(function () {
return document.title;
}));
))
**/
public function evaluate(function:Dynamic):Void;

/**
Includes external script from the specified URL (usually remote
location) and executes the callback upon completion.
Example:
page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function() {
// jQuery is loaded, now manipulate the DOM
});
**/
public function includeJs(URL:String, callb:Void->Void):Void;

/**
Injects external script code from the specified file. If the file can not be
found in the current directory, libraryPath is used for additional look up.
This function returns true if injection is successful, otherwise it returns
false.
**/
public function injectJs(filename:String):Void;

/**
Opens the URL and loads it to the page. Once page is loaded, the optional
callback is called using onLoadFinished, and also provides the page
status to the function ('success' or 'fail').
Example:
page.open('http://www.google.com/', function(status) {
console.log(status);
// do something here
});)
**/
public function open(URL:String, ?optional_callback:String->Void):Void;

/**
Releases memory heap associated with this page. Do not use the page
instance after calling this.
Due to some technical limitation, the web page object might not be
completely garbage collected. This is often encountered when the same
object is used over and over again. Calling this function may stop the
increasing heap allocation.
**/
public function release():Void;


/**
Renders the web page to an image buffer and save it as the specified file.
Currently the output format is automatically set based on the file
extension. Supported formats are PNG, JPEG, and PDF.
**/
public function render(fileName:String):Void;

/**
Sends an event to the web page.
The first argument is the event type. Supported type are mouseup,
mousedown, mousemove, and click. The next two arguments represents the
mouse position.
As of now, left button is the only pressed button for the event. For
mousemove however, there is no button pressed (i.e. it is not dragging).
The events are not like synthetic DOM events. Each event is sent to the
web page as if it comes as part of user interaction.
**/
public function sendEvent(type:String, ?mouseposx:Float, ?mouseposy:Float):Void;

/**
Uploads the specified file to the form element associated with the
selector. This function is used to automate the upload of a file which
is usually handled with a file dialog in a traditional browser. Since
there is dialog in this headless mode, such an upload mechanism is
handled via this special function.
Example:
page.uploadFile('input[name=image]', '/path/to/some/photo.jpg');
**/
public function uploadFile(selector:String, fileName:String):Void;


/**
This callback is invoked when there is a JavaScript alert. The only
argument passed to the callback is the string for the message.
**/
public var onAlert:String->Void;

/**
This callback is invoked when there is a JavaScript console. The callback
may accept up to three arguments: the string for the message, the line
number, and the source identifier. By default any console message from
the web page is not displayed. Using this callback is a typical way to
redirect it, such as:
page.onConsoleMessage = function (msg) { console.log(msg); };
**/
public var onConsoleMessage:String->Int->String->Void;

/**
This callback is invoked when there is a JavaScript execution error. It
is a good way to catch problems when evaluating a script in the web page
context. The arguments passed to the callback are the error message and
the stack trace (as an array).
Example:
page.onError = function (msg, trace) {
console.log(msg);
trace.forEach(function(item) {
console.log(' ', item.file, ':', item.line);
})
}
**/
public var onError:String->Array<{file:String, line:String}>->Void;

/**
This callback is invoked after the web page is created and before
a URL is loaded. The callback may be used to change global
objects.
**/
public var onInitialized:Void->Void;

/**
This callback is invoked when the page finishes the loading. It may
accept an argument status which equals to "success" if there is no
error and "failed" is error has occurred.
**/
public var onLoadFinished:String->Void;

/**
This callback is invoked when the page starts the loading. There
is no argument passed to the callback.
**/
public var onLoadStarted:Void->Void;

/**
This callback is invoked when the page requests a resource. The only
argument to the callback is the request object.
**/
public var onResourceRequested:Request->Void;

/**
This callback is invoked when the a resource requested by the page is
received. The only argument to the callback is the request object.
If the resource is large and sent by the server in multiple chunks,
onResourceReceived will be invoked for every chunk received by PhantomJS.
**/
public var onResourceReceived:Request->Void;

@:native("require('webpage')")
public static function create():WebPage;
}
7 changes: 7 additions & 0 deletions src/js/phantom/WebServer.hx
@@ -0,0 +1,7 @@
import js.phantom.Request;
import js.phantom.Response;
class WebServer {
@:native("require('webserver')")
public static function create():WebServer;
public static function listen(port:Int, cb:Request->Response->Void):Void;
}

0 comments on commit 1fea249

Please sign in to comment.