Skip to content
Shinji Yamada edited this page Jun 26, 2017 · 4 revisions

Table of Contents

How to launch multiple chromy instances.

Use different port number with each chromy instances.

const Chromy = require('chromy')

let port = 9222
let promises = []
for (let i = 0; i < 4; i++) {
    let chromy = new Chromy({port: port++})
    let p = chromy.chain().goto('http://example.com').end().then(() => chromy.close())
    promises.push(p)
}
Promise.all(promises).then(() => {
  console.log('done')
})

How to access to the variable from function passed to evaluate command.

The function passed to evaluate() is evaluated in browser context. So, cannot access to the variable on nodejs context. If the variable is used for read access only, you can define the variable in browser context in advance to resolve this problem.

const Chromy = require('chromy')

const the_variable = 'The Value'
let chromy = new Chromy()
chromy.chain()
    .goto('http://example.com/')
    .console(msg => console.log(msg))
    .evaluate(`the_value_in_browser = '${the_variable}'`)
    .evaluate(function() {
      console.log(the_value_in_browser)
    })
    .end()
    .then(e => {
      chromy.close()
    })
    .catch(e => {
      console.log(e)
      chromy.close()
    })

How to disable the headless browser to load images?

You can use blockUrls() to prevent to load images.

const Chromy = require('chromy')

let chromy = new Chromy({visible: true})
chromy.chain()
      .blockUrls(['*.png', '*.jpg', '*.jpeg', '*.gif'])
      .goto('https://example.com/')
      .sleep(10000)
      .end()
      .catch(e => console.error(e))
      .then(_ => chromy.close())