1 | # Render Multiple URLs to file
|
---|
2 |
|
---|
3 | system = require("system")
|
---|
4 |
|
---|
5 | # Render given urls
|
---|
6 | # @param array of URLs to render
|
---|
7 | # @param callbackPerUrl Function called after finishing each URL, including the last URL
|
---|
8 | # @param callbackFinal Function called after finishing everything
|
---|
9 | RenderUrlsToFile = (urls, callbackPerUrl, callbackFinal) ->
|
---|
10 | urlIndex = 0 # only for easy file naming
|
---|
11 | webpage = require("webpage")
|
---|
12 | page = null
|
---|
13 | getFilename = ->
|
---|
14 | "rendermulti-" + urlIndex + ".png"
|
---|
15 |
|
---|
16 | next = (status, url, file) ->
|
---|
17 | page.close()
|
---|
18 | callbackPerUrl status, url, file
|
---|
19 | retrieve()
|
---|
20 |
|
---|
21 | retrieve = ->
|
---|
22 | if urls.length > 0
|
---|
23 | url = urls.shift()
|
---|
24 | urlIndex++
|
---|
25 | page = webpage.create()
|
---|
26 | page.viewportSize =
|
---|
27 | width: 800
|
---|
28 | height: 600
|
---|
29 |
|
---|
30 | page.settings.userAgent = "Phantom.js bot"
|
---|
31 | page.open "http://" + url, (status) ->
|
---|
32 | file = getFilename()
|
---|
33 | if status is "success"
|
---|
34 | window.setTimeout (->
|
---|
35 | page.render file
|
---|
36 | next status, url, file
|
---|
37 | ), 200
|
---|
38 | else
|
---|
39 | next status, url, file
|
---|
40 |
|
---|
41 | else
|
---|
42 | callbackFinal()
|
---|
43 |
|
---|
44 | retrieve()
|
---|
45 | arrayOfUrls = null
|
---|
46 | if system.args.length > 1
|
---|
47 | arrayOfUrls = Array::slice.call(system.args, 1)
|
---|
48 | else
|
---|
49 | # Default (no args passed)
|
---|
50 | console.log "Usage: phantomjs render_multi_url.js [domain.name1, domain.name2, ...]"
|
---|
51 | arrayOfUrls = ["www.google.com", "www.bbc.co.uk", "www.phantomjs.org"]
|
---|
52 |
|
---|
53 | RenderUrlsToFile arrayOfUrls, ((status, url, file) ->
|
---|
54 | if status isnt "success"
|
---|
55 | console.log "Unable to render '" + url + "'"
|
---|
56 | else
|
---|
57 | console.log "Rendered '" + url + "' at '" + file + "'"
|
---|
58 | ), ->
|
---|
59 | phantom.exit()
|
---|
60 |
|
---|