mobile-demo-set / static / sdk / command / vendor / phantomjs / win / examples / render_multi_url.coffee

# Render Multiple URLs to file
# FIXME: For now it is fine with pure domain names: don't think it would work with paths and stuff like that

# Extend the Array Prototype with a 'foreach'
Array.prototype.forEach = (action) ->
    for i, j in this
        action j, i, _len

# Render a given url to a given file
# @param url URL to render
# @param file File to render to
# @param callback Callback function
renderUrlToFile = (url, file, callback) ->
    page = require('webpage').create()
    page.viewportSize = { width: 800, height : 600 }
    page.settings.userAgent = 'Phantom.js bot'

    page.open url, (status) ->
       if status isnt 'success'
           console.log "Unable to render '#{url}'"
       else
           page.render file

       delete page
       callback url, file

# Read the passed args
if phantom.args.length > 0
    arrayOfUrls = phantom.args
else
    # Default (no args passed)
    console.log 'Usage: phantomjs render_multi_url.coffee [domain.name1, domain.name2, ...]'
    arrayOfUrls = [
      'www.google.com',
      'www.bbc.co.uk',
      'www.phantomjs.org'
    ]

# For each URL
arrayOfUrls.forEach (pos, url, total) ->
    file_name = "./#{url}.png"

    # Render to a file
    renderUrlToFile "http://#{url}", file_name, (url, file) ->
        console.log "Rendered '#{url}' at '#{file}'"
        if pos is total - 1
            # Close Phantom if it's the last URL
            phantom.exit()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.