@@ -3,6 +3,7 @@ var page = require("webpage").create();
3
3
var url = system . args [ 1 ] ;
4
4
var readabilityPath = system . args [ 2 ] ;
5
5
var userAgent = system . args [ 3 ] ;
6
+ var consoleLogs = [ ] ;
6
7
7
8
// Prevent page js errors to break JSON output
8
9
// XXX: should we log these instead?
@@ -17,6 +18,43 @@ function outputJSON(object) {
17
18
console . log ( JSON . stringify ( object , null , 2 ) ) ;
18
19
}
19
20
21
+ /**
22
+ * Note: This function runs within page environment.
23
+ */
24
+ function runReadability ( url , userAgent , pageContent ) {
25
+ var location = document . location ;
26
+ var uri = {
27
+ spec : location . href ,
28
+ host : location . host ,
29
+ prePath : location . protocol + "//" + location . host , // TODO This is incomplete, needs username/password and port
30
+ scheme : location . protocol . substr ( 0 , location . protocol . indexOf ( ":" ) ) ,
31
+ pathBase : location . protocol + "//" + location . host + location . pathname . substr ( 0 , location . pathname . lastIndexOf ( "/" ) + 1 )
32
+ } ;
33
+ try {
34
+ var result = new Readability ( uri , document ) . parse ( ) ;
35
+ if ( result ) {
36
+ result . userAgent = userAgent ;
37
+ } else {
38
+ result = {
39
+ error : {
40
+ message : "Empty result from Readability.js." ,
41
+ sourceHTML : pageContent || "Empty page content."
42
+ }
43
+ } ;
44
+ }
45
+ return result ;
46
+ } catch ( err ) {
47
+ return {
48
+ error : {
49
+ message : err . message ,
50
+ line : err . line ,
51
+ stack : err . stack ,
52
+ sourceHTML : pageContent || "Empty page content."
53
+ }
54
+ } ;
55
+ }
56
+ } ;
57
+
20
58
if ( ! url ) {
21
59
exitWithError ( "Missing url arg." ) ;
22
60
} else if ( ! readabilityPath ) {
@@ -33,45 +71,23 @@ page.settings.loadImages = false;
33
71
// ensure we don't waste time trying to load slow/missing resources
34
72
page . settings . resourceTimeout = 1000 ;
35
73
74
+ page . onConsoleMessage = function ( msg ) {
75
+ consoleLogs . push ( msg ) ;
76
+ } ;
77
+
36
78
page . open ( url , function ( status ) {
37
79
if ( status !== "success" ) {
38
80
return exitWithError ( "Unable to access " + url ) ;
39
81
}
40
82
if ( ! page . injectJs ( readabilityPath ) ) {
41
83
exitWithError ( "Couldn't inject " + readabilityPath ) ;
42
84
}
43
- outputJSON ( page . evaluate ( function ( url , userAgent , pageContent ) {
44
- var location = document . location ;
45
- var uri = {
46
- spec : location . href ,
47
- host : location . host ,
48
- prePath : location . protocol + "//" + location . host , // TODO This is incomplete, needs username/password and port
49
- scheme : location . protocol . substr ( 0 , location . protocol . indexOf ( ":" ) ) ,
50
- pathBase : location . protocol + "//" + location . host + location . pathname . substr ( 0 , location . pathname . lastIndexOf ( "/" ) + 1 )
51
- } ;
52
- try {
53
- var result = new Readability ( uri , document ) . parse ( ) ;
54
- if ( result ) {
55
- result . userAgent = userAgent ;
56
- } else {
57
- result = {
58
- error : {
59
- message : "Empty result from Readability.js." ,
60
- sourceHTML : pageContent || "Empty page content."
61
- }
62
- } ;
63
- }
64
- return result ;
65
- } catch ( err ) {
66
- return {
67
- error : {
68
- message : err . message ,
69
- line : err . line ,
70
- stack : err . stack ,
71
- sourceHTML : pageContent || "Empty page content."
72
- }
73
- } ;
74
- }
75
- } , url , page . settings . userAgent , page . content ) ) ;
85
+ var result = page . evaluate ( runReadability , url , page . settings . userAgent , page . content ) ;
86
+ if ( result && result . error ) {
87
+ result . error . consoleLogs = consoleLogs ;
88
+ } else if ( result && result . content ) {
89
+ result . consoleLogs = consoleLogs ;
90
+ }
91
+ outputJSON ( result ) ;
76
92
phantom . exit ( ) ;
77
93
} ) ;
0 commit comments