-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
executable file
·859 lines (790 loc) · 26.1 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>PhantomJS - The Headles Webkit Browser</title>
<meta name="description" content="A framework for javascript testing, site scraping, pdf rendering and more">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<link href='http://fonts.googleapis.com/css?family=Lato:400,700,400italic,700italic' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="css/reveal.css">
<link rel="stylesheet" href="css/theme/default.css" id="theme">
<!-- For syntax highlighting -->
<link rel="stylesheet" href="lib/css/zenburn.css">
<link rel="stylesheet" href="css/index.css"/>
</head>
<body>
<div class="reveal">
<!-- Used to fade in a background when a specific slide state is reached -->
<div class="state-background"></div>
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<section>
<section>
<h1>PhantomJS</h1>
<h3>The Headless Webkit</h3>
<h5> </h5>
<img src="assets/headless_horseman.jpeg" width="400" height="400"/>
</section>
<section>
<h2>Overview</h2>
<ul style="margin:50px 0 0 0;">
<li>
The Tool
</li>
<li>
The Project
</li>
<li>
Examples
</li>
</ul>
</section>
</section>
<section>
<section>
<h1>The Tool</h1>
<img src="assets/tool.png" width="400" height="400"/>
</section>
<section>
<h2>Headless Webkit?</h2>
<img src="assets/confused.jpeg" width="500px"/>
</section>
<section>
<h2>Webkit with a head</h2>
<h5>e.g. Chrome or Safari</h5>
<div class="browser">
<h5> Browser (Head)</h5>
<ul style="margin:20px 0 20px 20px;">
<li>
Interprets layout information
</li>
<li>
Creates display (UI)
</li>
</ul>
<div class="webkit browser-part">
<h5>Webkit</h5>
<ul>
<li>Handles Network Requests</li>
<li>Parses HTML / CSS</li>
<li>Executes Javscript</li>
<li>Creates layout information</li>
</ul>
</div>
</div>
</section>
<section>
<h2>Who needs a head?</h2>
<img style="vertical-align: middle;" src="assets/giant-head.jpeg"/>
<ul style="vertical-align: middle;">
<li>
Unnecessary </br>
for many tasks
</li>
<li>
It's slow
</li>
<li>
CPU expensive
</li>
</ul>
</section>
<section>
<h2>Enter PhantomJS</h2>
<div class="browser" class="browser">
<h5> PhantomJS</h5>
<div class="webkit browser-part">
<h5>Webkit</h5>
<ul>
<li>Handles Network Requests</li>
<li>Parses HTML / CSS</li>
<li>Executes Javscript</li>
<li>Creates layout information</li>
</ul>
</div>
</div>
</section>
<section>
<h2>In One Sentence</h2>
<blockquote style="margin:60px auto;">
It’s a command line utility that runs JS scripts in a headless webkit.
</blockquote>
<pre><code contenteditable>
$ phantomjs [phantomjs options] my-script.js [script arguments]
</code></pre>
</section>
<section>
<h2>Also...</h2>
<blockquote style="margin:60px auto;">
It provides you with a few helpful modules that can be used in the scripts consumed by phantomjs
</blockquote>
<pre><code contenteditable>// my-script.js
//phantomjs gives you these
var sys = require('system');
var webPage = require('webpage');
var fileSystem = require('fs');
var server = require('webserver');
phantom;
// your amazing code goes here
</code></pre>
</section>
<section>
<h2>This is not an API review</h2>
<img src="assets/bored.jpeg" style="margin:10px 0 30px 0;"/>
<p>Docs are <a href="https://github.com/ariya/phantomjs/wiki/API-Reference" target="_blank">here</a>.
</section>
<section>
<h2>Yeah, yeah so what do I do with it?</h2>
<img src="assets/angry.jpeg" style="margin:10px 0 30px 0;"/>
</section>
<section>
<h2>Popular Applications</h2>
<ul>
<li>
Running javascript tests
</li>
<li>
Page automation / scraping
</li>
<li>
Screen captures
</li>
<li>
PDF rendering
</li>
<li>
Network / performance monitoring
</li>
</ul>
</section>
<section>
<h2>It's whatever you want it to be!</h2>
<img src="assets/abstract.jpeg"/>
</section>
</section>
<section>
<section>
<h1>The Project</h1>
<img src="assets/phantomjsghost.png" width="400" height="400"/>
</section>
<section>
<h2>I'm not just the creator</h2>
<img class="ariya-img" src="assets/ariya.jpeg" height="400"/>
<h2>I'm also a member</h2>
<a class="ariya-link" href="https://twitter.com/AriyaHidayat">
Ariya Hidayat
</a>
</section>
<section>
<h2>Project Info</h2>
<ul style="vertical-align:middle; margin-right:30px;">
<li>It's Free</li>
<li>Open Source</li>
<li>BSD license</li>
<li>Windows</li>
<li>Mac OS X</li>
<li>Linux</li>
</ul>
<img src="assets/awesomesauce.png" style="background: white; border: 10px solid white; vertical-align:middle;" />
</section>
<section>
<h2>The Cool Kids Are Doing It</h2>
<ul style="vertical-align:middle; margin: 0 5px 0 0;">
<li>Twitter</li>
<li>LinkedIn</li>
<li>Modernizr.js</li>
<li>Ember.js</li>
<li>Grunt</li>
<li>Yeoman</li>
<li>SurveyMonkey <br/>(The coolest)</li>
</ul>
<img style="vertical-align: middle;" src="assets/coolkid.jpeg" width="" height=""/>
</section>
<section>
<h2>Current Version</h2>
<ul style="margin: 20px 0 20px 1em;">
<li>1.7 aka "Blazing Star"</li>
<li>Released September 22</li>
<li>Releases every 2-3 months</li>
</ul>
<img style="display:block; margin: 0 auto;" src="assets/blazing-star.jpeg"/>
</section>
<section>
<h2>Easy Install</h2>
<img style="margin:10px auto 30px auto; width:700px;" src="assets/dumb.jpeg"/>
<p>Download <a target="_blank" href="http://phantomjs.org/download.html" >here</a></p>
</section>
</section>
<section>
<section>
<h1>Examples</h1>
<img src="assets/house.png" width="400" height="400"/>
</section>
<section>
<h2 style="color: #61B329;">Example #1</h2>
<h4 style="margin:50px 0 50px 0;">Scrape image sources from a URL.</h4>
<pre><code contenteditable>$ phantomjs phantom-img-srcs.js <url>
http://somesite.com/image1.jpeg
http://somesite.com/image2.jpeg
http://somesite.com/image3.jpeg
http://somesite.com/image4.jpeg
http://somesite.com/image5.jpeg</code></pre>
</section>
<section>
<h2>Design for phantom-img-srcs.js</h2>
<ol style="margin-top:50px;">
<li>Parse console arguments</li>
<li>Request HTML</li>
<li>Scrape image sources</li>
<li>Output to console</li>
</ol>
</section>
<section>
<h2>Step 1: Parse Console Arguments</h2>
<pre><code contenteditable>// PhantomJS provides you with the system module
var system = require('system');
// The first argument is the name of the
// script that is currently being invoked. phantom-img-srcs.js
scriptName = system.args[0];
// The second argument is the url that we will
// use to scrape image sources
url = system.args[1];</code></pre>
</section>
<section>
<h2>Step 2: Request The URL</h2>
<pre><code contenteditable>// PhantomJS provides you with the webpage module
var webPage = require('webpage').create();
// Pass a url and a callback that will be invoked
// when the a response is received
webPage.open( url, function(status){
// Status will be either 'success' or 'fail'
// Now you can start scripting on the DOM
});</code></pre>
</section>
<section>
<h2>Step 3: Scrape Image Sources</h2>
<pre><code contenteditable>// We can script on the DOM using webPage.evaluate
var imgSrcs = webPage.evaluate(function () {
var imgs = document.querySelectorAll('img');
// Convert nodeList to array
imgs = Array.prototype.slice.call(imgs, 0);
// Return array of the page's image sources out to imgSrcs
return imgs.map(function(img){
return img.src;
});
});</code></pre>
</section>
<section>
<h2>Scope of webPage.evaluate</h2>
<h4>( 1 of 3 )</h4>
<pre><code contenteditable>// Creates a sandboxed scripting environment
webPage.evaluate(function(){
// Code here runs in the context of the file that was opened
// No Access to variables in your PhantomJS script
});</code></pre>
</section>
<section>
<h2>Scope of webPage.evaluate</h2>
<h4>( 2 of 3 )</h4>
<pre><code contenteditable>var outside = 2;
function doSomething(){
return "something";
}
webPage.evaluate(function(){
//Throws an error
doSomething();
// 'outside' is also not available
var inner = outside;
});</code></pre>
</section>
<section>
<h2>Scope of webPage.evaluate</h2>
<h4>( 3 of 3 )</h4>
<pre><code contenteditable>// To have access to data inside evaluate, pass it as a
// second argument
var outer = { stuff: 'stuff'};
var fromInsidePage = webPage.evaluate(function(data){
data.stuff === 'stuff'
return 4;
}, outer);
fromInsidePage === 4;
// Anything that can be JSONified can be passed in or returned
// No functions, DOM nodes</code></pre>
</section>
<section>
<h2>Step 4: Output To Console</h2>
<pre><code contenteditable>// imgSrcs is a list of image sources from the requested page
imgSrcs.forEach(function(src){
// This is how we output to the console
console.log(src);
});
// phantom.exit ends our script and the PhantomJS process
phantom.exit(0);
// A successful program returns 0 and an unsuccessful one
// returns a non-zero value
</code></pre>
</section>
<section>
<h2>We Are Always In WebKit</h2><pre style="margin:10px 50px 10px 0; width:50%; display:inline-block; vertical-align:middle;"><code contenteditable>// New Selectors API
document.querySelectorAll('img');
// Javascript 1.6
imgs.map(function(img){
return img.src;
});
imgSrcs.forEach(function(src){
console.log(src);
});</code></pre>
<img style="vertical-align:middle; width:40%;" src="assets/happyplace.jpeg"/>
</section>
<section>
<h2>Demo Example #1</h2>
<img src="assets/double-rainbow.jpeg"/>
</section>
<section>
<h2>Good stuff, but I can't get my data</h2>
<div class="reach-image"></div>
</section>
<section>
<h2 style="color: #61B329;">Example #2</h2>
<h4 style="margin:50px 0 50px 0;">Create a web server that can scrape content and return JSON</h4>
<pre><code contenteditable>$ phantomjs phantom-scrape-server.js
$ curl 'localhost:8080/?url=http%3A//pinterest.com'
[{
"alt": "",
"src": "http://passets-ec.pinterest.com/images/search.gif",
"title": ""
},{
"alt": "beautiful dresses beautiful dresses",
"src": "http://media.pinterest.com/upload/fi0I_b.jpg",
"title": ""
},
...</code></pre>
</section>
<section>
<h2>Design for phantom-scrape-server.js</h2>
<ol style="margin-top:50px;">
<li>Start a web server</li>
<li>Parse requests</li>
<li>Scrape image data</li>
<li>Return JSON response</li>
</ol>
</section>
<section>
<h2>Step 1: Start A Web Server</h2>
<pre><code contenteditable>//PhantomJS provides you with a webserver module
var server = require('webserver').create();
// We will be requiring the webpage module but will not make
// an instance until a request comes in
var WebPage = require('webpage');
// Start listening on port 8080 (localhost:8080)
service = server.listen(8080, function (request, response) {
//here is where we will write our code to handle requests
});</code></pre>
</section>
<section>
<h2>Step 2: Parse Requests</h2>
<pre><code contenteditable>// Extract the url that we will use to scrape data from
var queryString = request.url.split("?")[1];
var url = unescape(queryString.split("=")[1]);
// Create new instance of webpage to make a request
var webPage = WebPage.create();
webPage.open(url, function(status){
// Write code to scrape the page and return a reponse
});</code></pre>
</section>
<section>
<h2>Step 3: Scrape Image Data</h2>
<pre><code contenteditable>var imgData = webPage.evaluate(function(){
var imgs = document.querySelectorAll("img");
imgs = Array.prototype.slice.call(imgs,0);
return imgs.map(function(img){
return{
src: img.src,
alt: img.alt,
title: img.title
};
});
});</code></pre>
</section>
<section>
<h2>Step 4: Return JSON Response</h2>
<pre><code contenteditable>// Need to release the memory heap of the webpage
// New as of 1.7. Used to be webPage.release();
webPage.close();
// Form the response
response.statusCode = 200;
response.headers = {
'Content-Type': "text/javascript"
};
response.write(JSON.stringify(imgData, null, 4));
response.close();</code></pre>
</section>
<section>
<h2>Webserver Module Still Experimental</h2>
<img src="assets/Experimental.jpeg"/>
</section>
<section>
<h2>Demo Example #2</h2>
<img src="assets/double-rainbow.jpeg"/>
</section>
<section>
<h2 style="color: #61B329;">Example #3</h2>
<h4 style="margin:50px 0 50px 0;">Run javascript tests</h4>
<pre><code contenteditable>$ phantomjs phantom-qunit.js <test-file>
Pass
Passing
Passed. You should probably drink a beer</code></pre>
</section>
<section>
<h2>Design for phantom-qunit.js</h2>
<ol style="margin-top:50px;">
<li>Validate console arguments</li>
<li>Configure webpage Instance</li>
<li>Run QUnit test</li>
<li>Determine test status</li>
<li>Output results</li>
</ol>
</section>
<section>
<h2>Why QUnit?</h2>
<h4>It was my first testing framework</h4>
<div class="nostalgia-image"></div>
</section>
<section>
<h2>Step 1: Validate Console Arguments</h2>
<pre><code contenteditable>var system = require('system');
var script = system.args[0];
if( system.args.length !== 2 ){
console.log("Usage: phantomjs " + script + " <test-file>");
phantom.exit(1);
}
var testFile = system.args[1];</code></pre>
</section>
<section>
<h2>Step 2: Configure Webpage Instance</h2>
<pre><code contenteditable>// If there is a javascript error in the test page
// onError function will be invoked and log erros to the console
webPage.onError = function(msg, trace){
console.log("Error on the page: " + msg);
trace.forEach(function(item) {
console.log(' ', item.file, ':', item.line);
});
};
// Relay console.log calls in the test page to the console
webPage.onConsoleMessage = function(msg){
console.log(msg);
};
// Set the size of the view port that test page will be run in
webPage.viewPortSize = { width: 980, height: 1000 };</code></pre>
</section>
<section>
<h2>Step 3: Run QUnit Tests</h2>
<pre><code contenteditable>webPage.open( testFile, function(status){
// This time let's validate the page loaded properly
if ( status !== 'success') {
console.log("Unable to load " + url);
phantom.exit(2);
}
setInterval(function() {
// Poll for test completion
}, 100);
});</code></pre>
</section>
<section>
<h2>Step 4: Determine Test Status</h2>
<pre><code contenteditable>//Inside setInterval so it will repeat until complete
var complete = webPage.evaluate(function () {
var banner = document.getElementById('qunit-banner');
var classes = banner.className;
var failed = classes.indexOf('qunit-fail') !== -1;
var passed = classes.indexOf('qunit-pass') !== -1;
if( !failed && !passed ) return false;
if(failed) return "failed";
return "passed";
});
</code></pre>
</section>
<section>
<h2>Tests Are Oblivious To PhantomJS</h2>
<img src="assets/peepingtom.jpeg"/>
</section>
<section>
<h2>Tempting but try not to do it</h2>
<h4 style="margin-top:50px;">HTML file</h4>
<pre><code contenteditable>console.log("PhantomJS: hey");
<div id="specialPhantomJSID"></div>
</code></pre>
<h4 style="margin-top:50px;">PhantomJS script</h4>
<pre><code contenteditable>webPage.onConsoleMessage = function(msg){
if( startsWith(msg, "PhantomJS") ){
//do something special
}
}
webPage.evaluate(function(){
document.getElementById('specialPhantomJSID');
})</code></pre>
<h4 style="margin-top:50px;"> Introduces more dependencies </h4>
</section>
<section>
<h2>Step 5: Output Results</h2>
<pre><code contenteditable>if(complete){
if(complete === "failed"){
console.log("Failed. You should probably drink a beer");
phantom.exit(3);
}
console.log("Passed. You should probably drink a beer");
phantom.exit(0);
}</code></pre>
</section>
<section>
<h2>Demo Example #3</h2>
<img src="assets/double-rainbow.jpeg"/>
</section>
<section>
<h2 style="color: #61B329;">Example #4</h2>
<h4 style="margin:50px 0 50px 0;">Create a PDF from a web page</h4>
<pre><code contenteditable>$ phantomjs phantom-pdf.js <arg1> <arg2> ...
Your file was rendered : /path/to/pdf/webpage.pdf</code></pre>
</section>
<section>
<h2>Design for phantom-pdf.js</h2>
<ol style="margin-top:50px;">
<li>Validate console arguments</li>
<li>Configure PDF page settings</li>
<li>Render PDF</li>
</ol>
</section>
<section>
<h2>Step 1: Validate Console Arguments</h2>
<h4>(1 of 2)</h4>
<pre><code contenteditable>var requiredArgs = [
'script', 'url', 'filename', 'zoomFactor', 'headerHeight',
'footerHeight' , 'marginTop', 'marginRight','marginBottom',
'marginLeft', 'orientation', 'format', 'header', 'footer'
];
if(system.args.length !== requiredArgs.length){
var msgs = requiredArgs.map(function(arg){
return "<" + arg + ">";
});
// the first arg isn't an option so no brackets
msgs[0] = system.args[0];
console.log("Usage: " + msgs.join());
phatom.exit(1);
}</code></pre>
</section>
<section>
<h2>Step 1: Validate Console Arguments</h2>
<h4>(2 of 2)</h4>
<pre><code contenteditable>//put all of our script args into an object
var settings = {};
requiredArgs.forEach(function(value, i){
settings[requiredArgs[i]] = system.args[i];
});
// settings.url
// settings.footer,
// settings.header
...</code></pre>
</section>
<section>
<h2>Step 2: Configure PDF Page Settings </h2>
<h4>(1 of 3)</h4>
<pre><code contenteditable>
webPage.paperSize = {
header: {
height: settings.headerHeight,
contents: phantom.callback(function(pageNum, numPages) {
return settings.header
.replace("#pageNum#", pageNum)
.replace("#numPages#", numPages);
})
},
...
}</code></pre>
</section>
<section>
<h2>Step 3: Configure PDF Page Settings </h2>
<h4>(2 of 3)</h4>
<pre><code contenteditable>webPage.paperSize = {
...
footer: {
height: settings.footerHeight,
contents: phantom.callback(function(pg, pgs) {
return settings.footer
.replace("#pg#", pg)
.replace("#pgs#", pgs);
})
},
...
}</code></pre>
</section>
<section>
<h2>Step 3: Configure PDF Page Settings</h2>
<h4>(3 of 3)</h4>
<pre><code contenteditable>webPage.paperSize = {
...
margin: {
top: settings.marginTop,
right: settings.marginRight,
bottom: settings.marginBottom,
left: settings.marginLeft
},
orientation: settings.orientation,
format: settings.format
};
webPage.zoomFactor = parseFloat(settings.zoomFactor);</code></pre>
</section>
<section>
<h2>Step 3: Render PDF</h2>
<pre><code contenteditable>webPage.open( settings.url, function(status){
// ensure that the filename ends in .pdf because
// this is the instruction to render a pdf vs. png or jpeg
if(settings.filename.substring(-4).toLowerCase() !== ".pdf"){
settings.filename += ".pdf";
}
//writes the pdf file to disk
webPage.render(filename);
phantom.exit(0);
});</code></pre>
</section>
<section>
<h2>Why Is PhantomJS Great At PDFs?</h2>
<blockquote style="margin:100px auto 100px auto;">
Since PhantomJS is using WebKit, it can render HTML/CSS, SVG and Canvas to PDF the way users see it on your website.
</blockquote>
<h6>psssst and it's free</h6>
</section>
<section>
<h2>Printed Pages Should Be Oblivious Too</h2>
<img src="assets/peepingtom.jpeg"/>
</section>
<section>
<h2>Demo Example #4<h2>
<img src="assets/double-rainbow.jpeg"/>
</section>
<section>
<h2 style="color: #61B329;">Example #5</h2>
<h4 style="margin:50px 0 50px 0;">Take a screen shot of every matching CSS selector.</h4>
<pre><code contenteditable>$ phantomjs phantom-selector.js 'http://pinterest.com' '.pin'</code></pre>
<img style="vertical-align:top;" src="assets/pinterest2.png"/>
<img style="vertical-align:top;" src="assets/pinterest3.png"/>
<img style="vertical-align:top;" src="assets/pinterest4.png"/>
</section>
<section>
<h2>Design for phantom-selector.js</h2>
<ol style="margin-top:50px;">
<li>Parse console arguments</li>
<li>Request HTML</li>
<li>Gather element coordinates</li>
<li>Render screen shots</li>
</ol>
</section>
<section>
<h2>Step 1: Parse Console Arguments</h2>
<pre><code contenteditable>var system = require('system');
var webPage = require('webpage').create();
var scriptName = system.args[0];
var url = system.args[1];
var selector = system.args[2];</code></pre>
</section>
<section>
<h2>Step 2: Request HTML</h2>
<pre style="width:100%;"><code contenteditable>webPage.open( url, function(status){
// After the url has been successfully loaded
// we want to add jQuery to it so we can use it in
// the webPage.evaluate function
var jQueryUrl = "http://ajax.googleapis.com/.../jquery.min.js";
webPage.includeJs( jQueryUrl, function(){
//jQuery has now loaded and available to use
});
});</code></pre>
</section>
<section>
<h2>Step 3: Gather Element Coordinates</h2>
<pre><code contenteditable>// Pass the css selector as the second argument
// to the webPage.evaluate function
elementData = webPage.evaluate(function (selector) {
var $nodes = $(selector);
// return the coordinates of the first 10 matching elements
return $nodes.map(function(){
var $node = $(this);
var coords = $node.offset();
coords.width = $node.outerWidth();
coords.height = $node.outerHeight();
return coords;
}).get().slice(0,10);
}, selector);</code></pre>
</section>
<section>
<h2>Step 4: Render Screen Shots</h2>
<pre><code contenteditable>elementData.forEach(function(data, i){
// Create a unique filename for each screen shot
var file = "selection_" + i + ".png";
// Set the clipRect property of webPage
// This limits the area that is rendered
webPage.clipRect = {
top: data.top,
left: data.left,
width: data.width,
height: data.height
};
// Write the file to disk
webPage.render(file);
});
phantom.exit(0);</code></pre>
</section>
<section>
<h2>Demo Example #5</h2>
<img src="assets/double-rainbow.jpeg"/>
</section>
</section>
<section>
<section>
<h2>Adrian Adkison</h2>
<ul>
<li>Engineer at SurveyMonkey</li>
<li>We are hiring!</li>
<li>[email protected]</li>
<li>@metalculus84</li>
</ul>
</section>
<section>
<h2>Thank You!</h2>
<img src="assets/beer_toast.jpeg" />
</section>
</section>
</div>
<!-- The navigational controls UI -->
<aside class="controls">
<a class="left" href="#">◄</a>
<a class="right" href="#">►</a>
<a class="up" href="#">▲</a>
<a class="down" href="#">▼</a>
</aside>
<!-- Presentation progress bar -->
<div class="progress"><span></span></div>
</div>
<script src="lib/js/head.min.js"></script>
<script src="js/reveal.min.js"></script>
<script>
// Full list of configuration options available here:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: true,
progress: true,
history: true,
theme: Reveal.getQueryHash().theme || 'default', // available themes are in /css/theme
transition: Reveal.getQueryHash().transition || 'default', // default/cube/page/concave/linear(2d)
// Optional libraries used to extend on reveal.js
dependencies: [
{ src: 'lib/js/highlight.js', async: true, callback: function() { window.hljs.initHighlightingOnLoad(); } },
{ src: 'lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'lib/js/showdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'lib/js/data-markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: '/socket.io/socket.io.js', async: true, condition: function() { return window.location.host === 'localhost:1947'; } },
{ src: 'plugin/speakernotes/client.js', async: true, condition: function() { return window.location.host === 'localhost:1947'; } },
]
});
</script>
</body>
</html>