Book Image

PhantomJS Cookbook

By : Rob Friesel
Book Image

PhantomJS Cookbook

By: Rob Friesel

Overview of this book

Table of Contents (15 chapters)
PhantomJS Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Specifying a path for external scripts


In this recipe, we will introduce the libraryPath property on the phantom object and discuss how to use it to control the source of scripts that are loaded in the runtime via injectJs.

Getting ready

To run this recipe, we will need at least one injectable script and the script that we want to run.

The script in this recipe is available in the downloadable code repository as recipe03.js under chapter02. If we run the provided example script, we must change to the root directory for the book's sample code.

How to do it…

Consider the following script:

console.log('Initial libraryPath: ' + phantom.libraryPath);

phantom.libraryPath = phantom.libraryPath.replace(/chapter02$/,
  'lib');

console.log('Updated libraryPath: ' + phantom.libraryPath);

var isInjected = phantom.injectJs('hemingway.js');

if (isInjected) {
  console.log('Script was successfully injected.');
  console.log('Give me some Fibonacci numbers! ' +
    fibonacci(Math.round(Math.random() * 10...