type definitions for phantomjs-node

This commit is contained in:
Eugene Gusarov
2016-03-11 23:16:57 +03:00
parent e86bd1858a
commit e1c7bffb54
3 changed files with 224 additions and 1 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
/// <reference path="./phantom.d.ts" />
/// <reference path="./phantom-1.0.0.d.ts" />
import phantom = require("phantom");
+166
View File
@@ -0,0 +1,166 @@
/// <reference path="./phantom.d.ts" />
import phantom = require("phantom");
phantom.create().then((ph: phantom.PhantomJS): void => {
ph.createPage().then((page): void => {
page.open("http://www.google.com").then((status: string) => {
console.log("opened google? ", status);
return page.evaluate((): string => {
return document.title;
}).then((result: string): void => {
console.log('Page title is ' + result);
ph.exit();
});
});
});
});
var _ph: phantom.PhantomJS;
phantom.create(["--web-security=no", "--ignore-ssl-errors=yes"]).then((ph) => {
console.log("Phantom Bridge Initiated");
_ph = ph;
});
var _page: phantom.WebPage;
_ph.createPage().then((page) => {
console.log("Page created!");
_page = page;
});
_page.open("http://www.google.com").then(function (status) {
if (status == "success") {
console.log("Page is open!");
_page.close();
}
});
_page.evaluate(function() {
var title = (<HTMLElement>document.querySelector("title")).innerText
console.log("The page title is " + title)
})
_page.evaluate(function(selector) {
var text = (<HTMLElement>document.querySelector(selector)).innerText
console.log(selector + " contains the following text: " + text)
}, "title").then(function(result) {})
_page.evaluate(function(selector) {
var text = (<HTMLElement>document.querySelector(selector)).innerText
return text
}, "title").then(function(result) {
console.log("The element contains the following text: " + result)
});
_page.set('viewportSize', { width: 1920, height: 1080 }).then(function (result) {
console.log("Viewport set to: " + result.width + "x" + result.height);
});
_page.set('onConsoleMessage', function (msg: string) {
console.log("Phantom Console: " + msg);
});
_page.set('onUrlChanged', function(url: string) {
console.log("New URL: "+url);
});
_page.set('onResourceRequested', function () {
console.log("Resource requested..");
});
_page.set('onResourceReceived', function (res: any) {
if (res.stage == 'end') {
console.log("Resource received!")
}
});
_page.set('onLoadStarted', function () {
console.log("Loading started");
});
_page.set('onLoadFinished', function (status: string) {
console.log("Loading finished, the page is " + ((status == "success") ? "open." : "not open!"));
});
_page.set('settings.loadImages', false);
_page.set('settings.resourceTimeout', 1000);
_page.set('settings.viewportSize', {
width : 1920,
height : 1080
});
_page.open("http://google.co.jp").then((status) => {
_page.render("/tmp/google-top.jpg", {
format : 'jpeg',
quality : '80'
});
_ph.exit();
});
phantom.create().then((ph) => {
ph.addCookie('cookie_name', 'cookie_value', 'localhost', () => {});
ph.getCookies((cookies) => {});
ph.createPage().then((page) => {
page.set('Referer', 'http://google.com');
page.set('settings.userAgent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1');
page.open("http://localhost:9901/cookie", (status) => {
var someFunc = (aaa: string, my_obj: Object) => {
var attribute_to_want = aaa;
var h2Arr: string[] = [];
var results = document.querySelectorAll(attribute_to_want);
for (var i = 0, len = results.length; i < len; ++i) {
var result = <HTMLElement>results[i];
h2Arr.push(result.innerText);
}
return {
h2: h2Arr,
aaa: this.arguments,
obj: my_obj
};
};
var finishedFunc = (result: any) => {
ph.exit();
};
page.evaluate<string, {wahtt: number;}, void>(someFunc, 'div', {wahtt: 111}).then(finishedFunc);
});
});
ph.createPage((page) => {
page.open('http://www.phantomjs.org', (status) => {
if (status === 'success') {
page.includeJs('http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js', () => {
page.injectJs('do.js', (res) => {
page.evaluate(() => {
return document.title;
}, (title: string) => {
console.log(title);
ph.exit();
});
});
});
page.sendEvent('click', 350, 320);
page.sendEvent('click', 350, 320, 'right');
page.sendEvent('keypress', 'A', null, null, 0x02000000 | 0x08000000);
page.sendEvent('keypress', 'A');
page.setViewportSize(800, 640);
page.setPaperSize({
width: '200px',
height: '300px',
margin: '0px'
});
page.setPaperSize({
format: 'A4',
orientation: 'portrait',
margin: '1cm'
});
page.setPaperSize({
width: '5in',
height: '7in',
margin: {
top: '50px',
left: '20px'
}
});
page.setZoomFactor(0.25);
}
});
});
});
+57
View File
@@ -0,0 +1,57 @@
// Type definitions for PhantomJS bridge for NodeJS
// Project: https://github.com/sgentle/phantomjs-node
// Definitions by: Random <https://github.com/llRandom/>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
declare module "phantom" {
function create(args?: string[]): Promise<PhantomJS>;
interface PhantomJS {
createPage(): Promise<WebPage>;
exit(returnValue?: number): void;
}
interface WebPage {
open(url: string): Promise<string>;
close(): void;
get(key: string): Promise<any>;
set(key: string, value: any): Promise<any>;
setHeaders(headers: Object): Promise<void>;
setViewportSize(width: number, height: number): Promise<void>;
setPaperSize(options: IPaperSizeOptions): Promise<void>;
setZoomFactor(factor: number): Promise<void>;
evaluate<R>(callback: () => R): Promise<R>;
evaluate<T>(callback: (arg: T) => void, arg: T): Promise<void>;
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>;
includeJs(url: string): Promise<void>;
injectJs(filename: string): Promise<boolean>;
sendEvent(mouseEventType: string, mouseX: number, mouseY: number, button?: string): void;
sendEvent(keyboardEventType: string, key: string, null1?: void, null2?: void, modifier?: number): void;
uploadFile(selector: string, filename: string): void;
render(filename: string): Promise<void>;
render(filename: string, options?: { format?: string; quality?: string; }): Promise<void>;
renderBase64(type: string): Promise<string>;
goBack(): void;
goForward(): void;
reload(): void;
getContent(): Promise<string>;
setContent(html: string, url: string): Promise<string>;
getCookies(): Promise<{ name: string; value: string; domain?: string; }[]>;
}
interface IPaperSizeOptions {
width?: string;
height?: string;
format?: string;
orientation?: string;
margin?: any; // string | { top?: string; left?: string; bottom?: string; right?: string; }
}
}