diff --git a/phantom/phantom-1.0.0-tests.ts b/phantom/phantom-1.0.0-tests.ts new file mode 100644 index 000000000..c0563fd15 --- /dev/null +++ b/phantom/phantom-1.0.0-tests.ts @@ -0,0 +1,166 @@ +/// + +import phantom = require("phantom"); + +phantom.create((ph: phantom.PhantomJS): void => { + ph.createPage((page): void => { + page.open("http://www.google.com", (status: string): void => { + console.log("opened google? ", status); + page.evaluate((): string => { + return document.title; + }, (result: string): void => { + console.log('Page title is ' + result); + ph.exit(); + }); + }); + }); +}); + + +var _ph: phantom.PhantomJS; +phantom.create("--web-security=no", "--ignore-ssl-errors=yes", { port: 12345 }, (ph) => { + console.log("Phantom Bridge Initiated"); + _ph = ph; +}); +var _page: phantom.WebPage; +_ph.createPage((page) => { + console.log("Page created!"); + _page = page; +}); + +_page.open("http://www.google.com", function (status) { + if (status == "success") { + console.log("Page is open!"); + _page.close(); + } +}); + +_page.evaluate(function() { + var title = (document.querySelector("title")).innerText + console.log("The page title is " + title) +}) +_page.evaluate(function(selector) { + var text = (document.querySelector(selector)).innerText + console.log(selector + " contains the following text: " + text) +}, function(result) {}, "title") +_page.evaluate(function(selector) { + var text = (document.querySelector(selector)).innerText + return text +}, function(result) { + console.log("The element contains the following text: " + result) +}, "title"); + + +_page.set('viewportSize', { width: 1920, height: 1080 }, 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", (status) => { + _page.render("/tmp/google-top.jpg", { + format : 'jpeg', + quality : '80' + }); + _ph.exit(); +}); + + +phantom.create((ph) => { + ph.addCookie('cookie_name', 'cookie_value', 'localhost', () => {}); + ph.getCookies((cookies) => {}); + + ph.createPage((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 = results[i]; + h2Arr.push(result.innerText); + } + return { + h2: h2Arr, + aaa: this.arguments, + obj: my_obj + }; + }; + var finishedFunc = (result: any) => { + ph.exit(); + }; + page.evaluate(someFunc, finishedFunc, 'div', {wahtt: 111}); + }); + }); + + 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); + } + }); + }); +}); diff --git a/phantom/phantom-1.0.0.d.ts b/phantom/phantom-1.0.0.d.ts new file mode 100644 index 000000000..849817354 --- /dev/null +++ b/phantom/phantom-1.0.0.d.ts @@ -0,0 +1,78 @@ +// Type definitions for PhantomJS bridge for NodeJS 0.7.0 +// Project: https://github.com/sgentle/phantomjs-node +// Definitions by: horiuchi +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +declare module "phantom" { + + function create(callback: (ph: PhantomJS) => void): void; + function create(options: ICreateOptions, callback: (ph: PhantomJS) => void): void; + function create(arg: string, callback: (ph: PhantomJS) => void): void; + function create(arg: string, options: ICreateOptions, callback: (ph: PhantomJS) => void): void; + function create(arg1: string, arg2: string, callback: (ph: PhantomJS) => void): void; + function create(arg1: string, arg2: string, options: ICreateOptions, callback: (ph: PhantomJS) => void): void; + function create(arg1: string, arg2: string, arg3: string, callback: (ph: PhantomJS) => void): void; + function create(arg1: string, arg2: string, arg3: string, options: ICreateOptions, callback: (ph: PhantomJS) => void): void; + + interface PhantomJS { + createPage(callback: (page: WebPage) => void): void; + exit(returnValue?: number): void; + injectJs(filename: string, callback?: (result: boolean) => void): void; + + addCookie(name: string, value: string, domain: string, callback?: (res: boolean) => void): void; + clearCookies(callback?: () => void): void; + getCookies(callback: (cookies: { name: string; value: string; domain?: string; }[]) => void): void; + } + + interface WebPage { + open(url: string, callback: (status: string) => void): void; + close(): void; + + get(key: string, callback: (result: any) => void): void; + set(key: string, value: any, callback?: (result: any) => void): void; + setHeaders(headers: Object, callback?: () => void): void; + setViewportSize(width: number, height: number, callback?: () => void): void; + setPaperSize(options: IPaperSizeOptions, callback?: () => void): void; + setZoomFactor(factor: number, callback?: () => void): void; + + evaluate(callback: () => void): void; + evaluate(callback: () => R, returnCallback: (result: R) => void): void; + evaluate(callback: (arg: T) => void, returnCallback: () => void, arg: T): void; + evaluate(callback: (arg: T) => R, returnCallback: (result: R) => void, arg: T): void; + evaluate(callback: (arg1: T1, arg2: T2) => R, returnCallback: (result: R) => void, arg1: T1, arg2: T2): void; + evaluate(callback: (arg1: T1, arg2: T2, arg3: T3) => R, returnCallback: (result: R) => void, arg1: T1, arg2: T2, arg3: T3): void; + includeJs(url: string, callback?: () => void): void; + injectJs(filename: string, callback?: (res: boolean) => void): void; + 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, callback?: () => void): void; + render(filename: string, options?: { format?: string; quality?: string; }, callback?: () => void): void; + renderBase64(type: string, callback: (data: string) => void): void; + + goBack(): void; + goForward(): void; + reload(): void; + + getContent(callback: (content: string) => void): void; + setContent(html: string, url: string, callback?: (status: string) => void): void; + getCookies(callback: (cookies: { name: string; value: string; domain?: string; }[]) => void): void; + } + + interface ICreateOptions { + binary?: string; + hostname?: string; + path?: string; + port?: number; + } + interface IPaperSizeOptions { + width?: string; + height?: string; + format?: string; + orientation?: string; + margin?: any; // string | { top?: string; left?: string; bottom?: string; right?: string; } + } + +} + diff --git a/phantom/phantom-tests.ts b/phantom/phantom-tests.ts index d18168827..e53b7a1ff 100644 --- a/phantom/phantom-tests.ts +++ b/phantom/phantom-tests.ts @@ -2,165 +2,139 @@ import phantom = require("phantom"); -phantom.create((ph: phantom.PhantomJS): void => { - ph.createPage((page): void => { - page.open("http://www.google.com", (status: string): void => { - console.log("opened google? ", status); - page.evaluate((): string => { - return document.title; - }, (result: string): void => { - console.log('Page title is ' + result); - ph.exit(); - }); - }); - }); -}); - - -var _ph: phantom.PhantomJS; -phantom.create("--web-security=no", "--ignore-ssl-errors=yes", { port: 12345 }, (ph) => { - console.log("Phantom Bridge Initiated"); - _ph = ph; -}); -var _page: phantom.WebPage; -_ph.createPage((page) => { - console.log("Page created!"); - _page = page; -}); - -_page.open("http://www.google.com", function (status) { - if (status == "success") { - console.log("Page is open!"); - _page.close(); - } -}); - -_page.evaluate(function() { - var title = (document.querySelector("title")).innerText - console.log("The page title is " + title) -}) -_page.evaluate(function(selector) { - var text = (document.querySelector(selector)).innerText - console.log(selector + " contains the following text: " + text) -}, function(result) {}, "title") -_page.evaluate(function(selector) { - var text = (document.querySelector(selector)).innerText - return text -}, function(result) { - console.log("The element contains the following text: " + result) -}, "title"); - - -_page.set('viewportSize', { width: 1920, height: 1080 }, 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", (status) => { - _page.render("/tmp/google-top.jpg", { - format : 'jpeg', - quality : '80' - }); - _ph.exit(); -}); - - -phantom.create((ph) => { - ph.addCookie('cookie_name', 'cookie_value', 'localhost', () => {}); - ph.getCookies((cookies) => {}); - - ph.createPage((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 = results[i]; - h2Arr.push(result.innerText); - } - return { - h2: h2Arr, - aaa: this.arguments, - obj: my_obj - }; - }; - var finishedFunc = (result: any) => { - ph.exit(); - }; - page.evaluate(someFunc, finishedFunc, 'div', {wahtt: 111}); - }); - }); - - 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(); +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(); }); - - 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); - } }); - }); }); + +phantom.create(["--web-security=no", "--ignore-ssl-errors=yes"]).then((ph) => { + console.log("Phantom Bridge Initiated"); + ph.createPage().then((page) => { + console.log("Page created!"); + + return page.open("http://www.google.com").then(function(status) { + if (status == "success") { + console.log("Page is open!"); + } + + return page.evaluate(function() { + var title = (document.querySelector("title")).innerText; + console.log("The page title is " + title); + }); + }).then(() => { + return page.evaluate(function(selector) { + var text = (document.querySelector(selector)).innerText; + console.log(selector + " contains the following text: " + text); + }, "title"); + }).then(() => { + return page.evaluate(function(selector) { + var text = (document.querySelector(selector)).innerText + return text + }) + }).then(function(result) { + console.log("The element contains the following text: " + result) + + return page.property('onConsoleMessage', function(msg: string) { + console.log("Phantom Console: " + msg); + }); + }).then(() => { + return page.property('onUrlChanged', function(url: string) { + console.log("New URL: " + url); + }); + }).then(() => { + return page.property('onResourceRequested', function() { + console.log("Resource requested.."); + }); + }).then(() => { + return page.property('onResourceReceived', function(res: any) { + if (res.stage == 'end') { + console.log("Resource received!") + } + }); + }).then(() => { + return page.property('onLoadStarted', function() { + console.log("Loading started"); + }); + }).then(() => { + return page.property('onLoadFinished', function(status: string) { + console.log("Loading finished, the page is " + ((status == "success") ? "open." : "not open!")); + }); + }).then(() => { + return page.property('settings.loadImages', false); + }).then(() => { + return page.property('settings.resourceTimeout', 1000); + }).then(() => { + return page.property('settings.viewportSize', { + width: 1920, + height: 1080 + }); + }).then(() => { + return page.open("http://google.co.jp"); + }).then((status) => { + return page.render("/tmp/google-top.jpg", { + format: 'jpeg', + quality: '80' + }); + }).then(() => { + return page.close(); + }); + }).then(() => { + ph.exit(); + }); +}); + +phantom.create().then((ph) => { + return ph.createPage().then((page) => { + page.open("http://localhost:9901/cookie").then((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 = results[i]; + h2Arr.push(result.innerText); + } + return { + h2: h2Arr, + aaa: this.arguments, + obj: my_obj + }; + }; + var finishedFunc = (result: any) => { + ph.exit(); + }; + return page.evaluate(someFunc, 'div', { wahtt: 111 }).then(finishedFunc); + }); + }).then(() => { + return ph.createPage().then((page) => { + page.open('http://www.phantomjs.org').then((status) => { + if (status === 'success') { + page.includeJs('http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js').then(() => { + page.injectJs('do.js').then((res) => { + page.evaluate(() => { + return document.title; + }).then((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'); + } + }); + }); + }); +}); \ No newline at end of file diff --git a/phantom/phantom.d.ts b/phantom/phantom.d.ts index 849817354..f87f25782 100644 --- a/phantom/phantom.d.ts +++ b/phantom/phantom.d.ts @@ -1,71 +1,58 @@ -// Type definitions for PhantomJS bridge for NodeJS 0.7.0 +// Type definitions for PhantomJS bridge for NodeJS // Project: https://github.com/sgentle/phantomjs-node -// Definitions by: horiuchi -// Definitions: https://github.com/borisyankov/DefinitelyTyped +// Definitions by: horiuchi , Random +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped declare module "phantom" { - function create(callback: (ph: PhantomJS) => void): void; - function create(options: ICreateOptions, callback: (ph: PhantomJS) => void): void; - function create(arg: string, callback: (ph: PhantomJS) => void): void; - function create(arg: string, options: ICreateOptions, callback: (ph: PhantomJS) => void): void; - function create(arg1: string, arg2: string, callback: (ph: PhantomJS) => void): void; - function create(arg1: string, arg2: string, options: ICreateOptions, callback: (ph: PhantomJS) => void): void; - function create(arg1: string, arg2: string, arg3: string, callback: (ph: PhantomJS) => void): void; - function create(arg1: string, arg2: string, arg3: string, options: ICreateOptions, callback: (ph: PhantomJS) => void): void; + function create(args?: string[]): Promise; interface PhantomJS { - createPage(callback: (page: WebPage) => void): void; + createPage(): Promise; exit(returnValue?: number): void; - injectJs(filename: string, callback?: (result: boolean) => void): void; - - addCookie(name: string, value: string, domain: string, callback?: (res: boolean) => void): void; - clearCookies(callback?: () => void): void; - getCookies(callback: (cookies: { name: string; value: string; domain?: string; }[]) => void): void; } interface WebPage { - open(url: string, callback: (status: string) => void): void; - close(): void; + open(url: string): Promise; + close(): Promise; - get(key: string, callback: (result: any) => void): void; - set(key: string, value: any, callback?: (result: any) => void): void; - setHeaders(headers: Object, callback?: () => void): void; - setViewportSize(width: number, height: number, callback?: () => void): void; - setPaperSize(options: IPaperSizeOptions, callback?: () => void): void; - setZoomFactor(factor: number, callback?: () => void): void; + evaluate(callback: () => R): Promise; + evaluate(callback: (arg: T) => void, arg: T): Promise; + evaluate(callback: (arg: T) => R, arg: T): Promise; + evaluate(callback: (arg1: T1, arg2: T2) => R, arg1: T1, arg2: T2): Promise; + evaluate(callback: (arg1: T1, arg2: T2, arg3: T3) => R, arg1: T1, arg2: T2, arg3: T3): Promise; + evaluate(callback: (...args: any[]) => R, ...args: any[]): Promise; + includeJs(url: string): Promise; + injectJs(filename: string): Promise; + sendEvent(mouseEventType: string, mouseX?: number, mouseY?: number, button?: string): Promise; + sendEvent(keyboardEventType: string, key: string, null1?: void, null2?: void, modifier?: number): Promise; - evaluate(callback: () => void): void; - evaluate(callback: () => R, returnCallback: (result: R) => void): void; - evaluate(callback: (arg: T) => void, returnCallback: () => void, arg: T): void; - evaluate(callback: (arg: T) => R, returnCallback: (result: R) => void, arg: T): void; - evaluate(callback: (arg1: T1, arg2: T2) => R, returnCallback: (result: R) => void, arg1: T1, arg2: T2): void; - evaluate(callback: (arg1: T1, arg2: T2, arg3: T3) => R, returnCallback: (result: R) => void, arg1: T1, arg2: T2, arg3: T3): void; - includeJs(url: string, callback?: () => void): void; - injectJs(filename: string, callback?: (res: boolean) => void): void; - 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; + render(filename: string, options?: { format?: string; quality?: string; }): Promise; + renderBase64(type: string): Promise; - render(filename: string, callback?: () => void): void; - render(filename: string, options?: { format?: string; quality?: string; }, callback?: () => void): void; - renderBase64(type: string, callback: (data: string) => void): void; - - goBack(): void; - goForward(): void; - reload(): void; - - getContent(callback: (content: string) => void): void; - setContent(html: string, url: string, callback?: (status: string) => void): void; - getCookies(callback: (cookies: { name: string; value: string; domain?: string; }[]) => void): void; + setContent(html: string, url: string): Promise; + + property(key: string): Promise; + property(key: string, value: T): Promise; + + setting(key: string): Promise; + + addCookie(cookie: ICookie): Promise; + deleteCookie(cookieName: string): Promise; + clearCookies(): Promise; } - interface ICreateOptions { - binary?: string; - hostname?: string; - path?: string; - port?: number; + interface ICookie { + name: string, + value: string, + domain?: string, + path: string, + httponly?: boolean, + secure?: boolean, + expires?: Date } + interface IPaperSizeOptions { width?: string; height?: string; @@ -73,6 +60,4 @@ declare module "phantom" { orientation?: string; margin?: any; // string | { top?: string; left?: string; bottom?: string; right?: string; } } - -} - +} \ No newline at end of file