Initial Version based on PhantomJS

This commit is contained in:
nedunk
2017-03-26 12:57:00 +02:00
parent bb7d6b2d08
commit 311fc0d3f8
3 changed files with 445 additions and 0 deletions
+359
View File
@@ -0,0 +1,359 @@
// Type definitions for SlimerJS 0.10.3
// Project: https://docs.slimerjs.org/current/index.html#api-reference
// Definitions by: Alex Wall <https://github.com/alexwall>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
declare var phantom: IPhantom;
declare var slimer: ISlimer;
interface ISlimer {
version: number;
geckoVersion: number;
clearHttpAuth(): void;
isExiting(): boolean;
hasFeature(featureName: string): boolean;
exit(returnValue?: number): void;
wait(milliseconds: number): void;
}
interface IPhantom {
// Properties
args: string[]; // DEPRECATED
cookies: ICookie[];
cookiesEnabled: boolean;
libraryPath: string;
scriptName: string; // DEPRECATED
version: {
major: number;
minor: number;
patch: number;
};
// Functions
addCookie(cookie: ICookie): boolean;
clearCookies(): void;
deleteCookie(cookieName: string): boolean;
exit(returnValue?: any): boolean;
injectJs(filename: string): boolean;
// Callbacks
onError: (msg: string, trace: string[]) => any;
}
interface ISystem {
pid: number;
platform: string;
os: {
architecture: string;
name: string;
version: string;
};
env: { [name: string]: string; };
args: string[];
}
interface IHttpConf {
operation: string;
data: any;
headers: any
}
interface IWebPage {
// Properties
canGoBack: boolean;
canGoForward: boolean;
clipRect: IClipRect;
content: string;
cookies: ICookie[];
customHeaders: { [name: string]: string; };
event: any; // :TODO: elaborate this when documentation improves
focusedFrameName: string;
frameContent: string;
frameName: string;
framePlainText: string;
frameTitle: string;
frameUrl: string;
framesCount: number;
framesName: any; // :TODO: elaborate this when documentation improves
libraryPath: string;
navigationLocked: boolean;
offlineStoragePath: string;
offlineStorageQuota: number;
ownsPages: boolean;
pages: IWebPage[];
pagesWindowName: string;
paperSize: IPaperSize;
plainText: string;
scrollPosition: ITopLeft;
settings: IWebPageSettings;
title: string;
url: string;
viewportSize: ISize;
windowName: string;
zoomFactor: number;
captureContent: Array<RegExp>; // slimerjs only
// Functions
addCookie(cookie: ICookie): boolean;
childFramesCount(): number; // DEPRECATED
childFramesName(): string; // DEPRECATED
clearCookies(): void;
close(): Promise<void>;
close(): void;
currentFrameName(): string; // DEPRECATED
deleteCookie(cookieName: string): boolean;
// evaluate<R>(callback: () => R): Promise<R>;
// evaluate<T, R>(callback: (arg: T) => R, arg: T): Promise<R>;
// evaluate<T1, T2, R>(callback: (arg1: T1, arg2: T2) => R, arg1: T1, arg2: T2): Promise<R>;
// evaluate<T1, T2, T3, R>(callback: (arg1: T1, arg2: T2, arg3: T3) => R, arg1: T1, arg2: T2, arg3: T3): Promise<R>;
// evaluate<R>(callback: (...args: any[]) => R, ...args: any[]): Promise<R>;
evaluate<R>(callback: () => R, ...args: any[]): R;
evaluateAsync(fn: Function): void;
evaluateJavaScript(str: string): any; // :TODO: elaborate this when documentation improves
getPage(windowName: string): IWebPage;
go(index: number): void;
goBack(): void;
goForward(): void;
includeJs(url: string): Promise<void>;
includeJs(url: string, callback: Function): void;
injectJs(filename: string): Promise<boolean>;
injectJs(filename: string): boolean;
open(url: string): Promise<string>;
open(url: string, callback: (status: string) => any): Promise<string>;
open(url: string, method: string, callback: (status: string) => any): Promise<string>; // maybe data is missing
open(url: string, method: string, data: any, callback?: (status: string) => any): Promise<string>;
open(url: string, method: string, data: any, headers: any, callback: (status: string) => any): Promise<string>;
openUrl(url: string, httpConf: IHttpConf, settings: any): Promise<string>; // :TODO: elaborate this when documentation improves
release(): void; // DEPRECATED
reload(): void;
render(filename: string): Promise<void>;
render(filename: string, options?: { format?: string; quality?: string; ratio?: number; onlyViewport?: boolean }): Promise<void>;
renderBase64(type: string): Promise<string>;
render(filename: string): void;
renderBase64(format: string): string;
sendEvent(mouseEventType: string, mouseX?: number, mouseY?: number, button?: string): Promise<void>;
sendEvent(keyboardEventType: string, key: string, null1?: void, null2?: void, modifier?: number): Promise<void>;
sendEvent(mouseEventType: string, mouseX?: number, mouseY?: number, button?: string): void;
sendEvent(keyboardEventType: string, keyOrKeys: any, aNull?: any, bNull?: any, modifier?: number): void;
setContent(html: string, url: string): Promise<string>;
setContent(content: string, url: string): void;
stop(): void;
switchToFocusedFrame(): void;
switchToFrame(frameName: string): void;
switchToFrame(framePosition: number): void;
switchToChildFrame(frameName: string): void;
switchToChildFrame(framePosition: number): void;
switchToMainFrame(): void; // DEPRECATED
switchToParentFrame(): void; // DEPRECATED
uploadFile(selector: string, filename: string): void;
// Callbacks
onAlert: (msg: string) => any;
onCallback: Function; // EXPERIMENTAL
onClosing: (closingPage: IWebPage) => any;
onConfirm: (msg: string) => boolean;
onConsoleMessage: (msg: string, lineNum?: number, sourceId?: string) => any;
onError: (msg: string, trace: string[]) => any;
onFilePicker: (oldFile: string) => string;
onInitialized: () => any;
onLoadFinished: (status: string) => any;
onLoadStarted: () => any;
onNavigationRequested: (url: string, type: string, willNavigate: boolean, main: boolean) => any;
onPageCreated: (newPage: IWebPage) => any;
onPrompt: (msg: string, defaultVal: string) => string;
onResourceError: (resourceError: IResourceError) => any;
onResourceReceived: (response: IResourceResponse) => any;
onResourceRequested: (requestData: IResourceRequest, networkRequest: INetworkRequest) => any;
onUrlChanged: (targetUrl: string) => any;
// Callback triggers
closing(closingPage: IWebPage): void;
initialized(): void;
javaScriptAlertSent(msg: string): void;
javaScriptConsoleMessageSent(msg: string, lineNum?: number, sourceId?: string): void;
loadFinished(status: string): void;
loadStarted(): void;
navigationRequested(url: string, type: string, willNavigate: boolean, main: boolean): void;
rawPageCreated(newPage: IWebPage): void;
resourceReceived(response: IResourceResponse): void;
resourceRequested(requestData: IResourceRequest, networkRequest: INetworkRequest): void;
urlChanged(targetUrl: string): void;
}
interface IResourceError {
id: number;
url: string;
errorCode: string;
errorString: string;
}
interface IHttpVersion {
major: number,
minor: number
}
interface IResourceResponse {
id: number;
url: string;
time: Date;
headers: { [name: string]: string; };
bodySize: number;
contentType?: string;
redirectURL?: string;
stage: string;
status: number;
statusText: string;
contentCharset: string;
referrer: string;
isFileDownloading: boolean;
body: any;
httpVersion: IHttpVersion;
}
interface IResourceRequest {
id: number;
method: string;
url: string;
time: Date;
headers: { [name: string]: string; };
}
interface INetworkRequest {
abort(): void;
changeUrl(url: string): void;
setHeader(name: string, value: string): void;
}
interface IPaperSize {
width?: string;
height?: string;
border: string;
format?: string;
orientation?: string;
margin?: any; // string | { top?: string; left?: string; bottom?: string; right?: string; }
}
interface IWebPageSettings {
javascriptEnabled: boolean;
loadImages: boolean;
localToRemoteUrlAccessEnabled: boolean;
userAgent: string;
userName: string;
password: string;
XSSAuditingEnabled: boolean;
webSecurityEnabled: boolean;
resourceTimeout: number;
}
interface IFileSystem {
// Properties
separator: string;
workingDirectory: string;
// Functions
// Query Functions
list(path: string): string[];
absolute(path: string): string;
exists(path: string): boolean;
isDirectory(path: string): boolean;
isFile(path: string): boolean;
isAbsolute(path: string): boolean;
isExecutable(path: string): boolean;
isReadable(path: string): boolean;
isWritable(path: string): boolean;
isLink(path: string): boolean;
readLink(path: string): string;
// Directory Functions
changeWorkingDirectory(path: string): void;
makeDirectory(path: string): void;
makeTree(path: string): void;
removeDirectory(path: string): void;
removeTree(path: string): void;
copyTree(source: string, destination: string): void;
// File Functions
open(path: string, mode: string): IStream;
open(path: string, options: { mode: string; charset?: string; }): IStream;
read(path: string): string;
write(path: string, content: string, mode: string): void;
size(path: string): number;
remove(path: string): void;
copy(source: string, destination: string): void;
move(source: string, destination: string): void;
touch(path: string): void;
}
interface IStream {
atEnd(): boolean;
close(): void;
flush(): void;
read(): string;
readLine(): string;
seek(position: number): void;
write(data: string): void;
writeLine(data: string): void;
}
interface IWebServer {
port: number;
listen(port: number, cb?: (request: IWebServerRequest, response: IWebServerResponse) => void): boolean;
listen(ipAddressPort: string, cb?: (request: IWebServerRequest, response: IWebServerResponse) => void): boolean;
close(): void;
}
interface IWebServerRequest {
method: string;
url: string;
httpVersion: number;
headers: { [name: string]: string; };
post: string;
postRaw: string;
}
interface IWebServerResponse {
headers: { [name: string]: string; };
setHeader(name: string, value: string): void;
header(name: string): string;
statusCode: number;
setEncoding(encoding: string): void;
write(data: string): void;
writeHead(statusCode: number, headers?: { [name: string]: string; }): void;
close(): void;
closeGracefully(): void;
}
interface ITopLeft {
top: number;
left: number;
}
interface ISize {
width: number;
height: number;
}
interface IClipRect extends ITopLeft, ISize {
}
interface ICookie {
name: string,
value: string,
domain?: string,
path: string,
httponly?: boolean,
secure?: boolean,
expires?: string,
expiry: number
}
declare module "webpage" {
export function create(): IWebPage;
export function exit(returnValue?: number): void;
}
+64
View File
@@ -0,0 +1,64 @@
import webpagemod = require('webpage')
let page = webpagemod.create();
let vUrl = 'https://www.w3c.org';
let vUserAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/602.4.8 (KHTML, like Gecko) Version/10.0.3 Safari/602.4.8'
page.settings.userAgent = vUserAgent;
page.onConsoleMessage = function(vMsg) {
};
page.onAlert = function(vMsg) {
};
page.onLoadStarted = function() {
};
page.captureContent = [ /json/ ]
page.onResourceReceived = function(oResponse) {
oResponse.id;
oResponse.bodySize;
oResponse.body;
oResponse.contentType;
oResponse.headers;
};
page.onResourceRequested = function(oRequestData, oNetworkRequest) {
oRequestData.headers;
oRequestData.id;
oRequestData.method;
oRequestData.time;
oRequestData.url;
};
page.onLoadFinished = function(vStatus) {
phantom.exit();
};
page.onInitialized = function() {
};
page.onPageCreated = function(oPage) {
let oCookie: ICookie
oPage.addCookie(oCookie);
};
page.open(vUrl) // loads a page
.then(function(){ // executed after loading
page.viewportSize = { width:414, height:736 };
let vFilename = `../data/page01.png`;
page.render(vFilename, {onlyViewport:true})
// then open a second page
return page.open('http://');
})
.then(function(){
// click somewhere on the second page
page.sendEvent("click", 5, 5, 'left', 0);
slimer.exit()
});
+22
View File
@@ -0,0 +1,22 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": false,
"baseUrl": "../",
"typeRoots": [
"./"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"slimerjs-tests.ts"
]
}