Home

Awesome

chrome-page-eval

NPM Version License Build Status

Evaluate a script function on a page with Chrome

This module let you evaluate a script function on a page using Chrome (controlled with puppeteer) and get the return value of the evaluation in node.

Usage

<!-- 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 puppeteer = require('puppeteer')
const chromePageEval = require('chrome-page-eval')
const chromeEval = chromePageEval({ puppeteer })

(async () => {
  try {
    const result = await chromeEval({
      html: '/path/to/sample.html',
      scriptFn: `
        function () {
          let title = document.title

          let content = Array.from(document.querySelectorAll('.content'), (node) => {
            return node.textContent
          })

          return {
            title,
            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)
  }
})()

Constructor options

const chromePageEval = require('chrome-page-eval')
const chromeEval = chromePageEval({ /*[constructor options here]*/ })

Evaluate options

const puppeteer = require('puppeteer')
const chromePageEval = require('chrome-page-eval')
const chromeEval = chromePageEval({ puppeteer })

(async () => {
  const result = await chromeEval({ /*[evaluate options here]*/ })
})()

Requirements

Caveats

Debugging

License

See license