UNPKG

9.23 kBTypeScriptView Raw
1// Type definitions for PhantomJS API 1.9
2// Project: https://github.com/ariya/phantomjs/wiki/API-Reference
3// Definitions by: Mike Keesey <https://github.com/keesey>
4// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
5
6declare function require(module: string): any;
7
8declare var phantom: Phantom;
9
10interface Phantom {
11
12 // Properties
13 args: string[]; // DEPRECATED
14 cookies: Cookie[];
15 cookiesEnabled: boolean;
16 libraryPath: string;
17 scriptName: string; // DEPRECATED
18 version: {
19 major: number;
20 minor: number;
21 patch: number;
22 };
23
24 // Functions
25 addCookie(cookie: Cookie): boolean;
26 clearCookies(): void;
27 deleteCookie(cookieName: string): boolean;
28 exit(returnValue?: any): boolean;
29 injectJs(filename: string): boolean;
30
31 // Callbacks
32 onError: (msg: string, trace: string[]) => any;
33}
34
35interface System {
36 pid: number;
37 platform: string;
38 os: {
39 architecture: string;
40 name: string;
41 version: string;
42 };
43 env: { [name: string]: string; };
44 args: string[];
45}
46
47interface WebPage {
48
49 // Properties
50 canGoBack: boolean;
51 canGoForward: boolean;
52 clipRect: ClipRect;
53 content: string;
54 cookies: Cookie[];
55 customHeaders: { [name: string]: string; };
56 event: any; // :TODO: elaborate this when documentation improves
57 focusedFrameName: string;
58 frameContent: string;
59 frameName: string;
60 framePlainText: string;
61 frameTitle: string;
62 frameUrl: string;
63 framesCount: number;
64 framesName: any; // :TODO: elaborate this when documentation improves
65 libraryPath: string;
66 navigationLocked: boolean;
67 offlineStoragePath: string;
68 offlineStorageQuota: number;
69 ownsPages: boolean;
70 pages: WebPage[];
71 pagesWindowName: string;
72 paperSize: PaperSize;
73 plainText: string;
74 scrollPosition: TopLeft;
75 settings: WebPageSettings;
76 title: string;
77 url: string;
78 viewportSize: Size;
79 windowName: string;
80 zoomFactor: number;
81
82 // Functions
83 addCookie(cookie: Cookie): boolean;
84 childFramesCount(): number; // DEPRECATED
85 childFramesName(): string; // DEPRECATED
86 clearCookies(): void;
87 close(): void;
88 currentFrameName(): string; // DEPRECATED
89 deleteCookie(cookieName: string): boolean;
90 evaluate(fn: Function, ...args: any[]): any;
91 evaluateAsync(fn: Function): void;
92 evaluateJavaScript(str: string): any; // :TODO: elaborate this when documentation improves
93 getPage(windowName: string): WebPage;
94 go(index: number): void;
95 goBack(): void;
96 goForward(): void;
97 includeJs(url: string, callback: Function): void;
98 injectJs(filename: string): boolean;
99 open(url: string, callback: (status: string) => any): void;
100 open(url: string, method: string, callback: (status: string) => any): void;
101 open(url: string, method: string, data: any, callback: (status: string) => any): void;
102 openUrl(url: string, httpConf: any, settings: any): void; // :TODO: elaborate this when documentation improves
103 release(): void; // DEPRECATED
104 reload(): void;
105 render(filename: string): void;
106 renderBase64(format: string): string;
107 sendEvent(mouseEventType: string, mouseX?: number, mouseY?: number, button?: string): void;
108 sendEvent(keyboardEventType: string, keyOrKeys: any, aNull?: any, bNull?: any, modifier?: number): void;
109 setContent(content: string, url: string): void;
110 stop(): void;
111 switchToFocusedFrame(): void;
112 switchToFrame(frameName: string): void;
113 switchToFrame(framePosition: number): void;
114 switchToChildFrame(frameName: string): void;
115 switchToChildFrame(framePosition: number): void;
116 switchToMainFrame(): void; // DEPRECATED
117 switchToParentFrame(): void; // DEPRECATED
118 uploadFile(selector: string, filename: string): void;
119
120 // Callbacks
121 onAlert: (msg: string) => any;
122 onCallback: Function; // EXPERIMENTAL
123 onClosing: (closingPage: WebPage) => any;
124 onConfirm: (msg: string) => boolean;
125 onConsoleMessage: (msg: string, lineNum?: number, sourceId?: string) => any;
126 onError: (msg: string, trace: string[]) => any;
127 onFilePicker: (oldFile: string) => string;
128 onInitialized: () => any;
129 onLoadFinished: (status: string) => any;
130 onLoadStarted: () => any;
131 onNavigationRequested: (url: string, type: string, willNavigate: boolean, main: boolean) => any;
132 onPageCreated: (newPage: WebPage) => any;
133 onPrompt: (msg: string, defaultVal: string) => string;
134 onResourceError: (resourceError: ResourceError) => any;
135 onResourceReceived: (response: ResourceResponse) => any;
136 onResourceRequested: (requestData: ResourceRequest, networkRequest: NetworkRequest) => any;
137 onUrlChanged: (targetUrl: string) => any;
138
139 // Callback triggers
140 closing(closingPage: WebPage): void;
141 initialized(): void;
142 javaScriptAlertSent(msg: string): void;
143 javaScriptConsoleMessageSent(msg: string, lineNum?: number, sourceId?: string): void;
144 loadFinished(status: string): void;
145 loadStarted(): void;
146 navigationRequested(url: string, type: string, willNavigate: boolean, main: boolean): void;
147 rawPageCreated(newPage: WebPage): void;
148 resourceReceived(response: ResourceResponse): void;
149 resourceRequested(requestData: ResourceRequest, networkRequest: NetworkRequest): void;
150 urlChanged(targetUrl: string): void;
151}
152
153interface ResourceError {
154 id: number;
155 url: string;
156 errorCode: string;
157 errorString: string;
158}
159
160interface ResourceResponse {
161 id: number;
162 url: string;
163 time: Date;
164 headers: { [name: string]: string; };
165 bodySize: number;
166 contentType?: string | undefined;
167 redirectURL?: string | undefined;
168 stage: string;
169 status: number;
170 statusText: string;
171}
172
173interface ResourceRequest {
174 id: number;
175 method: string;
176 url: string;
177 time: Date;
178 headers: { [name: string]: string; };
179}
180
181interface NetworkRequest {
182 abort(): void;
183 changeUrl(url: string): void;
184 setHeader(name: string, value: string): void;
185}
186
187interface PaperSize {
188 width?: string | undefined;
189 height?: string | undefined;
190 border: string;
191 format?: string | undefined;
192 orientation?: string | undefined;
193}
194
195interface WebPageSettings {
196 javascriptEnabled: boolean;
197 loadImages: boolean;
198 localToRemoteUrlAccessEnabled: boolean;
199 userAgent: string;
200 userName: string;
201 password: string;
202 XSSAuditingEnabled: boolean;
203 webSecurityEnabled: boolean;
204 resourceTimeout: number;
205}
206
207interface FileSystem {
208
209 // Properties
210 separator: string;
211 workingDirectory: string;
212
213 // Functions
214
215 // Query Functions
216 list(path: string): string[];
217 absolute(path: string): string;
218 exists(path: string): boolean;
219 isDirectory(path: string): boolean;
220 isFile(path: string): boolean;
221 isAbsolute(path: string): boolean;
222 isExecutable(path: string): boolean;
223 isReadable(path: string): boolean;
224 isWritable(path: string): boolean;
225 isLink(path: string): boolean;
226 readLink(path: string): string;
227
228 // Directory Functions
229 changeWorkingDirectory(path: string): void;
230 makeDirectory(path: string): void;
231 makeTree(path: string): void;
232 removeDirectory(path: string): void;
233 removeTree(path: string): void;
234 copyTree(source: string, destination: string): void;
235
236 // File Functions
237 open(path: string, mode: string): Stream;
238 open(path: string, options: { mode: string; charset?: string | undefined; }): Stream;
239 read(path: string): string;
240 write(path: string, content: string, mode: string): void;
241 size(path: string): number;
242 remove(path: string): void;
243 copy(source: string, destination: string): void;
244 move(source: string, destination: string): void;
245 touch(path: string): void;
246}
247
248interface Stream {
249 atEnd(): boolean;
250 close(): void;
251 flush(): void;
252 read(): string;
253 readLine(): string;
254 seek(position: number): void;
255 write(data: string): void;
256 writeLine(data: string): void;
257}
258
259interface WebServer {
260 port: number;
261 listen(port: number, cb?: (request: WebServerRequest, response: WebServerResponse) => void): boolean;
262 listen(ipAddressPort: string, cb?: (request: WebServerRequest, response: WebServerResponse) => void): boolean;
263 close(): void;
264}
265
266interface WebServerRequest {
267 method: string;
268 url: string;
269 httpVersion: number;
270 headers: { [name: string]: string; };
271 post: string;
272 postRaw: string;
273}
274
275interface WebServerResponse {
276 headers: { [name: string]: string; };
277 setHeader(name: string, value: string): void;
278 header(name: string): string;
279 statusCode: number;
280 setEncoding(encoding: string): void;
281 write(data: string): void;
282 writeHead(statusCode: number, headers?: { [name: string]: string; }): void;
283 close(): void;
284 closeGracefully(): void;
285}
286
287interface TopLeft {
288 top: number;
289 left: number;
290}
291
292interface Size {
293 width: number;
294 height: number;
295}
296
297interface ClipRect extends TopLeft, Size {
298}
299
300interface Cookie {
301 name: string;
302 value: string;
303 domain?: string | undefined;
304}
305
306declare module "webpage" {
307 export function create(): WebPage;
308}