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...