
Security News
MCP Community Begins Work on Official MCP Metaregistry
The MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.
phantom-page-eval
Advanced tools
Evaluate a script function on a page with PhantomJS
This module let you evaluate a script function on a page using PhantomJS and get the return value of the evaluation in node.
<!-- sample.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Test page</title>
</head>
<body>
<div class="content">1</div>
<div class="content">2</div>
<div class="content">3</div>
<div class="content">4</div>
</body>
</html>
const phantomPath = require('phantomjs').path
const phantomPageEval = require('phantom-page-eval')
const phantomEval = phantomPageEval({ phantomPath })
(async () => {
try {
const result = await phantomEval({
html: '/path/to/sample.html',
scriptFn: `
function () {
var title = document.title
var content = Array.prototype.slice.call(document.getElementsByClassName('content'))
content = content.map(function (node) {
return node.textContent
})
return {
title: title,
content: content
}
}
`
})
console.log(result.title) // -> Test page
console.log(result.content) // -> [1, 2, 3, 4]
} catch (e) {
console.error('Error while trying to evaluate script:', e)
}
})()
const phantomPageEval = require('phantom-page-eval')
const phantomEval = phantomPageEval({ /*[constructor options here]*/ })
phantomPath
string [required] - the path to the phantomjs executable that your app is going to usetmpDir
string - the directory path that the module is going to use to save temporary files needed during the evaluation. defaults to require('os').tmpdir()
clean
boolean - specifies if the module should delete any temp file automatically after an evaluation, if false
temp files will not be cleaned by the module and app developer will be responsible to clean them. defaults to true
launchArgs
array - array with any of the command line options supported by phantomjs. defaults to ['--ignore-ssl-errors=yes', '--web-security=false', '--ssl-protocol=any']
const phantomPath = require('phantomjs').path
const phantomPageEval = require('phantom-page-eval')
const phantomEval = phantomPageEval({ phantomPath })
(async () => {
const result = await phantomEval({ /*[evaluate options here]*/ })
})()
html
string [required] - the path to the html file to load in a phantom pagescriptFn
string [required] - the script to evaluate in the phantom page. the script must be a function that returns a value. ex: scriptFn: 'function () { return 1 + 2}'
styles
array - array of css strings to insert at the beginning of page's head element. ex: styles: ['* { font-family: 'Calibri'; font-size: 16px; }']
waitForJS
boolean - when true
the scriptFn
won't be executed until the variable specified in waitForJSVarName
option is set to true in page's javscript. defaults to false
waitForJSVarName
string - name of the variable that will be used as trigger of scriptFn
. defaults to "PHANTOM_PAGE_EVAL_READY"
viewport
object - object with any of the viewportSize options supported by phantomjsargs
array - a list of custom arguments to pass to the scriptFn
function. ex: args: [1, 2]
and with scriptFn: function (a, b) { return a + b}'
will produce 3
as resulttimeout
number - time in ms to wait for the script evaluation to complete, when the timeout is reached the evaluation is cancelled. defaults to 30000
npm install phantomjs --save
or npm install phantomjs-prebuilt --save
and then in your project call require('phantomjs').path
and pass it to phantom-page-eval
constructor function.scriptFn
option) must be a serializable value recognized by phantomjs in order to receive it properly (normal javascript primitives like object, arrays, string, booleans, etc are good), if a non serializable value is returned you will get undefined
as the result.DEBUG=phantom-page-eval* node [your-entry-file-here].js
(on Windows use set DEBUG=phantom-page-eval* && node [your-entry-file-here].js
). This will print out to the console some additional information about what's going on.See license
FAQs
Evaluate a script function on a page with PhantomJS
The npm package phantom-page-eval receives a total of 2,746 weekly downloads. As such, phantom-page-eval popularity was classified as popular.
We found that phantom-page-eval demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.
Research
Security News
Socket uncovers an npm Trojan stealing crypto wallets and BullX credentials via obfuscated code and Telegram exfiltration.
Research
Security News
Malicious npm packages posing as developer tools target macOS Cursor IDE users, stealing credentials and modifying files to gain persistent backdoor access.