1
+ import { ICaptureOptions } from "./index" ;
2
+
1
3
const Nightmare = require ( "nightmare" ) ;
2
4
3
5
declare const document : any ;
@@ -52,17 +54,53 @@ export interface IWebPageRenderer {
52
54
export class WebPageRenderer implements IWebPageRenderer {
53
55
54
56
/**
55
- * Specifies the path to load Electron from or null to use default path .
57
+ * Options for capturing .
56
58
*/
57
- electronPath ?: string ;
59
+ private options ?: ICaptureOptions ;
58
60
59
61
/**
60
62
* Nightmare headless browser instance.
61
63
*/
62
- nightmare : any | null = null ;
64
+ private nightmare : any | null = null ;
65
+
66
+ constructor ( options ?: ICaptureOptions ) {
67
+ this . options = options ;
68
+ }
69
+
70
+ /**
71
+ * Log an info message.
72
+ */
73
+ info ( msg : string ) : void {
74
+ if ( this . options && this . options . log ) {
75
+ this . options . log . info ( msg ) ;
76
+ }
77
+ else {
78
+ console . info ( msg ) ;
79
+ }
80
+ }
63
81
64
- constructor ( electronPath ?: string ) {
65
- this . electronPath = electronPath ;
82
+ /**
83
+ * Log a warning message.
84
+ */
85
+ warn ( msg : string ) : void {
86
+ if ( this . options && this . options . log ) {
87
+ this . options . log . warn ( msg ) ;
88
+ }
89
+ else {
90
+ console . warn ( msg ) ;
91
+ }
92
+ }
93
+
94
+ /**
95
+ * Log an error message.
96
+ */
97
+ error ( msg : string ) : void {
98
+ if ( this . options && this . options . log ) {
99
+ this . options . log . error ( msg ) ;
100
+ }
101
+ else {
102
+ console . error ( msg ) ;
103
+ }
66
104
}
67
105
68
106
/**
@@ -71,15 +109,18 @@ export class WebPageRenderer implements IWebPageRenderer {
71
109
*/
72
110
async start ( ) : Promise < void > {
73
111
const nightmareOptions : any = {
74
- show : false ,
112
+ show : this . options && this . options . showBrowser ,
75
113
frame : false ,
76
114
maxHeight : 1000000 ,
77
115
maxWidth : 1000000 ,
116
+ waitTimeout : this . options && this . options . waitTimeout ,
117
+ gotoTimeout : this . options && this . options . gotoTimeout ,
118
+ openDevTools : this . options && this . options . openDevTools ,
78
119
} ;
79
120
80
- if ( this . electronPath ) {
121
+ if ( this . options && this . options . electronPath ) {
81
122
// Include Electron path if specified.
82
- nightmareOptions . electronPath = this . electronPath ;
123
+ nightmareOptions . electronPath = this . options . electronPath ;
83
124
}
84
125
85
126
this . nightmare = new Nightmare ( nightmareOptions ) ;
@@ -90,34 +131,34 @@ export class WebPageRenderer implements IWebPageRenderer {
90
131
91
132
this . nightmare . on ( 'page' , ( type : string , message : string , stack : any ) => {
92
133
if ( type === "error" ) {
93
- console . error ( "Browser page error: " + message ) ;
94
- console . error ( stack ) ;
134
+ this . error ( "Browser page error: " + message ) ;
135
+ this . error ( stack ) ;
95
136
}
96
137
} ) ;
97
138
98
139
this . nightmare . on ( "did-fail-load" , ( event : any , errorCode : number , errorDescription : string , validatedURL : string , isMainFrame : boolean ) => {
99
- console . error ( "Browser page failed to load." ) ;
100
- console . error ( "Error code: " + errorCode ) ;
101
- console . error ( "Error description: " + errorDescription ) ;
102
- console . error ( "Validated URL: " + validatedURL ) ;
103
- console . error ( "Is main frame: " + isMainFrame ) ;
140
+ this . error ( "Browser page failed to load." ) ;
141
+ this . error ( "Error code: " + errorCode ) ;
142
+ this . error ( "Error description: " + errorDescription ) ;
143
+ this . error ( "Validated URL: " + validatedURL ) ;
144
+ this . error ( "Is main frame: " + isMainFrame ) ;
104
145
} ) ;
105
146
106
147
this . nightmare . on ( 'console' , ( type : string , message : string ) => {
107
148
108
149
if ( type === 'log' ) {
109
- console . log ( 'LOG: ' + message ) ;
150
+ this . info ( 'LOG: ' + message ) ;
110
151
return ;
111
152
}
112
153
113
154
if ( type === 'warn' ) {
114
- console . warn ( 'LOG: ' + message ) ;
155
+ this . warn ( 'LOG: ' + message ) ;
115
156
return ;
116
157
}
117
158
118
159
if ( type === 'error' ) {
119
- console . error ( "Browser JavaScript error:" ) ;
120
- console . error ( message ) ;
160
+ this . error ( "Browser JavaScript error:" ) ;
161
+ this . error ( message ) ;
121
162
}
122
163
} ) ;
123
164
}
0 commit comments