3 var serializeFn = require("./serializeFn.js");
4 var phantomMethods = require("./phantom/methods.js");
6 var slice = Array.prototype.slice;
9 * A wrapper to run code within the context of a specific PhantomJS webpage.
11 * @see http://phantomjs.org/api/webpage/
12 * @param {Phantom} phantom the parent PhantomJS instance
13 * @param {number} id internal page id
16 function Page(phantom, id) {
17 Page.prototype.constructor.apply(this, arguments);
21 * The parent phantom instance.
25 Page.prototype.phantom = null;
28 * The internal page id.
33 Page.prototype._id = null;
36 * Initializes the page instance.
38 * @param {Phantom} phantom
41 Page.prototype.constructor = function (phantom, id) {
42 this.phantom = phantom;
47 * Stringifies the given function fn, sends it to PhantomJS and runs it in the context of a particular PhantomJS webpage.
48 * The PhantomJS webpage will be available as `this`. You may prepend any number of arguments which will be passed
49 * to fn inside of PhantomJS. Please note that all arguments should be stringifyable with JSON.stringify().
52 * @param {Function} fn
55 Page.prototype.run = function (args, fn) {
56 args = slice.call(arguments);
60 return this.phantom._send({
61 action: "run-on-page",
63 src: serializeFn(fn, args),
66 }, args.length === fn.length);
70 * Runs a function inside of PhantomJS to cleanup memory. Call this function if you intent to not use the page-object
73 * @see http://msdn.microsoft.com/en-us/library/system.idisposable.aspx
76 Page.prototype.dispose = function () {
79 return this.run(this._id, phantomMethods.disposePage)
85 module.exports = Page;