Sha256: 0efb33f8eeedf02b5d7f761b4ee4242275b2998ccd34ef10282a4ad1ce45e109

Contents?: true

Size: 1.58 KB

Versions: 17

Compression:

Stored size: 1.58 KB

Contents

# Render Multiple URLs to file

system = require("system")

# Render given urls
# @param array of URLs to render
# @param callbackPerUrl Function called after finishing each URL, including the last URL
# @param callbackFinal Function called after finishing everything
RenderUrlsToFile = (urls, callbackPerUrl, callbackFinal) ->
  urlIndex = 0 # only for easy file naming
  webpage = require("webpage")
  page = null
  getFilename = ->
    "rendermulti-" + urlIndex + ".png"

  next = (status, url, file) ->
    page.close()
    callbackPerUrl status, url, file
    retrieve()

  retrieve = ->
    if urls.length > 0
      url = urls.shift()
      urlIndex++
      page = webpage.create()
      page.viewportSize =
        width: 800
        height: 600

      page.settings.userAgent = "Phantom.js bot"
      page.open "http://" + url, (status) ->
        file = getFilename()
        if status is "success"
          window.setTimeout (->
            page.render file
            next status, url, file
          ), 200
        else
          next status, url, file

    else
      callbackFinal()

  retrieve()
arrayOfUrls = null
if system.args.length > 1
  arrayOfUrls = Array::slice.call(system.args, 1)
else
  # Default (no args passed)
  console.log "Usage: phantomjs render_multi_url.js [domain.name1, domain.name2, ...]"
  arrayOfUrls = ["www.google.com", "www.bbc.co.uk", "www.phantomjs.org"]

RenderUrlsToFile arrayOfUrls, ((status, url, file) ->
  if status isnt "success"
    console.log "Unable to render '" + url + "'"
  else
    console.log "Rendered '" + url + "' at '" + file + "'"
), ->
  phantom.exit()

Version data entries

17 entries across 17 versions & 2 rubygems

Version Path
hooch-0.4.2 jasmine/node_modules/karma-phantomjs-launcher/node_modules/phantomjs/lib/phantom/examples/render_multi_url.coffee
hooch-0.4.1 jasmine/node_modules/karma-phantomjs-launcher/node_modules/phantomjs/lib/phantom/examples/render_multi_url.coffee
hooch-0.4.0 jasmine/node_modules/karma-phantomjs-launcher/node_modules/phantomjs/lib/phantom/examples/render_multi_url.coffee
hooch-0.3.0 jasmine/node_modules/karma-phantomjs-launcher/node_modules/phantomjs/lib/phantom/examples/render_multi_url.coffee
hooch-0.2.1 jasmine/node_modules/karma-phantomjs-launcher/node_modules/phantomjs/lib/phantom/examples/render_multi_url.coffee
hooch-0.2.0 jasmine/node_modules/karma-phantomjs-launcher/node_modules/phantomjs/lib/phantom/examples/render_multi_url.coffee
hooch-0.1.0 jasmine/node_modules/karma-phantomjs-launcher/node_modules/phantomjs/lib/phantom/examples/render_multi_url.coffee
hooch-0.0.8 jasmine/node_modules/karma-phantomjs-launcher/node_modules/phantomjs/lib/phantom/examples/render_multi_url.coffee
hooch-0.0.7 jasmine/node_modules/karma-phantomjs-launcher/node_modules/phantomjs/lib/phantom/examples/render_multi_url.coffee
hooch-0.0.6 jasmine/node_modules/karma-phantomjs-launcher/node_modules/phantomjs/lib/phantom/examples/render_multi_url.coffee
entangled-0.0.16 spec/dummy/public/node_modules/phantomjs/lib/phantom/examples/render_multi_url.coffee
entangled-0.0.15 spec/dummy/public/node_modules/phantomjs/lib/phantom/examples/render_multi_url.coffee
entangled-0.0.14 spec/dummy/public/node_modules/phantomjs/lib/phantom/examples/render_multi_url.coffee
entangled-0.0.13 spec/dummy/public/node_modules/phantomjs/lib/phantom/examples/render_multi_url.coffee
entangled-0.0.12 spec/dummy/public/node_modules/phantomjs/lib/phantom/examples/render_multi_url.coffee
entangled-0.0.11 spec/dummy/public/node_modules/phantomjs/lib/phantom/examples/render_multi_url.coffee
entangled-0.0.10 spec/dummy/public/node_modules/phantomjs/lib/phantom/examples/render_multi_url.coffee