UNPKG

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