phantom

phantom is an object available automatically in scripts.

args

This is an array containing all arguments given on the command line.

var firstarg = phantom.args[0];

This property is deprecated. Use system.args instead.

clearCookies()

Delete all cookies that are stored in the current profile.

cookies

This is an array of all Cookie objects stored in the current profile.

Note: modifying an object in the array won’t modify the cookie. You should retrieve the array, modify it, and then set the cookies property with this array. Probably you would prefer to use the addCookie() method to modify a cookie.

If cookies are disabled, modifying this property does nothing.

Be careful about the inconsistent behavior of the expiry property.

cookiesEnabled

Indicates if the cookie manager is enabled (true) or disabled (false). You can modify this property to enable or disable the cookie manager.

By default, it is enabled.

defaultErrorHandler(message, stack)

This is the function used by default to handle errors appearing into your scripts or in the web page. Read only. Useful to set the onError callback to its initial value.

defaultPageSettings

This is an object that contains this following properties:

{
    javascriptEnabled: true,
    loadImages: true,                       // value of --load-images
    localToRemoteUrlAccessEnabled: false,   // value of --local-to-remote-url-access
    XSSAuditingEnabled : false,
    webSecurityEnabled: true,               // value of --web-security
    javascriptCanOpenWindows: true,
    javascriptCanCloseWindows: true,
    userAgent: 'SlimerJS',
    userName: undefined,
    password: undefined,
    maxAuthAttempts: undefined,
    resourceTimeout: undefined
}

This is a read-only property. It contains default values for webpage.settings.

deleteCookie(cookiename)

It deletes all cookies that have the given name, in any domain.

It returns true if some cookies have been deleted. It works only if cookies are enabled.

exit(code)

It stops the script and SlimerJS exit.

To be compatible with PhantomJS, it accepts an exit code but it is ignored because of a limitation in Firefox/XulRunner. This code is optional.

phantom.exit();

Note: your script may continue to be executed after the call of this method, because of the asynchronous behavior of this function.

injectJs(filename)

Use it if you want to “include” a javascript script into the main script, in other words, if you want to evaluate the given javascript file into the context of the main script.

Note that the file can be a Javascript script or a CoffeeScript script.

The method returns true if the injection is successful, or false if not (the file is not found for example).

If the path is not an absolute path, it should be a relative path to the libraryPath.

libraryPath

It represents the path of the directory where scripts indicated to injectJs() could be found. By default, this path is the directory of the main script, indicated on the command line.

You can change this path. You must then give an absolute path.

onError

This is the function called when an error occured in a script or in a web page. You can set this property to provide your own error handler. The function should accept a message and a stack as parameters.

phantom.onError = function (msg, stack) {
    var msg = "\nScript Error: "+msg+"\n";
    if (stack && stack.length) {
        msg += "       Stack:\n";
        stack.forEach(function(t) {
            msg += '         -> ' + (t.file || t.sourceURL) + ': ' + t.line + (t.function ? ' (in function ' + t.function + ')' : '')+"\n";
        })
    }
    console.error(msg+"\n");
}

scriptName

Contains the script name given to the command line.

This property is deprecated. Use system.args[0] instead.

version

Contain the version of PhantomJS to which SlimerJS is compatible (read-only). This is an object containing three properties, major, minor, patch:

var v = phantom.version;
console.log('version: ' + v.major + '.' + v.minor + '.' + v.patch);