Baking some cookies

How about creating our own cookies? Normally, users do not push the creation of cookies, but in PhantomJS, we can do that in our script to change the browser behavior.

Let us modify our previous code, and before displaying the list of cookies, we will add a "username" property to the cookie.

page.open(url, function(status) {
  if ( status === "success" ) {
    phantom.addCookie({
      'name'    : 'username',
      'value'   : 'aries',
      'domain'  : '.yahoo.com'
    });
    console.log(JSON.stringify(phantom.cookies, null, 4));
    phantom.exit(0);
  }
});

We will use the addCookie function from the phantom object. The function expects an object to be passed in JSON format. However, the name, value, and domain property are the only properties required to be present; all other properties are optional.

Cookie property

Expected value

name

The name of the cookie must be present. As per cookie specification, a cookie name must not start with $, which is reserved.

value

This is the value of the cookie; it is required.

domain

The domain name that this cookie is valid is required. The domain must start with a dot.

path

The path of the document that this cookie is applied. This defaults to the current path if not specified.

httponly

true or false, states that when set to true, the value of the cookie should be used only with HTTP, and that any other client side should prevent access to this cookie. For more information about this property, we can check out the HttpOnly working group site:

http://groups.google.com/group/ietf-httponly-wg

secure

true or false, states that if the cookie is valid in secured connection, then this should be true.

expires

Time in milliseconds.

If we run this script, the newly added cookie will be on top and will be displayed first from the list.

Baking some cookies

We can add as much as we can, but technically, a cookie must not be more than 255 characters long and can only consume up to 4 KB of disk space.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.189.250