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 |
---|---|
|
The name of the cookie must be present. As per cookie specification, a cookie name must not start with $, which is reserved. |
|
This is the value of the cookie; it is required. |
|
The domain name that this cookie is valid is required. The domain must start with a dot. |
|
The path of the document that this cookie is applied. This defaults to the current path if not specified. |
|
|
|
|
|
Time in milliseconds. |
If we run this script, the newly added cookie will be on top and will be displayed first from the list.
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.
3.15.189.250