page-evaluate
v1.1.0
Published
Monitor web pages for changes
Downloads
3
Maintainers
Readme
Monitor web pages for changes from Node.js
Examples
Window
Get the jsdom window
object for a web page.
const { window } = require('page-evaluate');
Callback
window(
'https://example.com',
function ({ document }) { // callback function
const result = document.querySelector('h1').textContent;
console.log(result); // logs 'Example Domain'
}
);
Promise
window('https://example.com').then(({ document }) => {
const result = document.querySelector('h1').textContent;
console.log(result); // logs 'Example Domain'
});
Async/Await
const { document } = await window('https://example.com');
const result = document.querySelector('h1').textContent;
console.log(result); // logs 'Example Domain'
Evaluate JavaScript
Evaluates a string of JavaScript on a web page and returns the output.
Note: Sanitize any input, but this is safe - "fresh copies of all the JavaScript spec-provided globals [are] installed on window" - jsdom readme
const { evaluate } = require('page-evaluate');
Callback
evaluate(
'https://example.com',
`document.querySelector('h1').textContent`,
function (result) { // callback function
console.log(result); // logs 'Example Domain'
}
);
Promise
evaluate('https://example.com',`document.querySelector('h1').textContent`)
.then((result) => {
console.log(result); // logs 'Example Domain'
});
Async/Await
const result = await evaluate('https://example.com',
`document.querySelector('h1').textContent`);
console.log(result); // logs 'Example Domain'
Run Your Program on a Schedule with a Cron Job
Send an email, turn on a light, etc. when a web page changes.
Get the path of Node.js on your system with
$ which node
Edit your cron jobs with:
$ crontab -e
(replace/path/to/node
with the output from step 1)
NODE_PATH=/path/to/node
# Every 30 minutes (between 7am and 7pm, Mon-Sat)
*/30 7-19 * * 1-6 $NODE_PATH index.js >> /path/to/log.txt
<Put a new line at the end or it will not work>
See crontab.guru for help and examples
Dependencies
- body-snatchers (uses either puppeteer or node-fetch to get pages)
- jsdom