page-evaluate

1.1.0 • Public • Published

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.

  1. Get the path of Node.js on your system with $ which node

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

Package Sidebar

Install

npm i page-evaluate

Weekly Downloads

1

Version

1.1.0

License

MIT

Unpacked Size

13.1 kB

Total Files

11

Last publish

Collaborators

  • zvakanaka