Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/zhang2333/chrome-ditto
Common API for Nightmare, Puppeteer
https://github.com/zhang2333/chrome-ditto
nightmare puppeteer
Last synced: 29 days ago
JSON representation
Common API for Nightmare, Puppeteer
- Host: GitHub
- URL: https://github.com/zhang2333/chrome-ditto
- Owner: zhang2333
- License: mit
- Created: 2017-09-01T07:49:46.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-01-17T10:47:23.000Z (about 7 years ago)
- Last Synced: 2024-12-09T17:53:45.382Z (about 2 months ago)
- Topics: nightmare, puppeteer
- Language: TypeScript
- Size: 22.5 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Chrome Ditto
Common API for `Nightmare`, `Puppeteer`
## Install
``` shell
yarn add chrome-ditto# or npm
npm i chrome-ditto
```## Usage
``` js
const Ditto = require('chrome-ditto')
const Nightmre = require('nightmare')
// const puppeteer = require('puppeteer')(async () => {
const browser = await Ditto(Nightmare)
// const browser = Ditto(puppeteer)
const page = await browser.newPage()
await page.goto('http://www.example.com/')
await page.wait('h1')
let content = await page.evaluate(() => {
return document.querySelector('p').textContent.trim()
})
console.log('content:', content)
await page.screenshot('./example.png')
await browser.close()
})()
```## API
### Ditto
- `Ditto(model, options: DittoOptions): Browser`
`model`: Nightmare | Puppeteer, that you want to transform
- `DittoOptions`
- `show`: `boolean`
show browser or not
- `showImages`: `boolean`
- `ignoreHTTPSErrors`: `boolean`
- `waitTimeout`: `number`
- `sessionId`: `string`persist states between pages
### Browser
#### Init
- `init(options: DittoOptions): Promise`
#### Properties
- `model`
- `name: string`name of `Browser`, `'nightmare', 'puppeteer'` etc.
### Page
#### Properties
- `ins`
instance of model
- `options: DittoOptions`
#### Setters of Page
- `setUserAgent(ua: string): Promise`
- `setViewport(width: number, height: number): Promise`#### Actions
- `click(selector: string): Promise`
- `close(): Promise`
- `goto(url: string): Promise`
- `type(selector: string, text: string): Promise`#### Extract
- `evaluate(func: Function, arg1, arg2...): Promise`
- `html(): Promise`
- `screenshot(relativePath: string): Promise`#### Waiting
- `wait(time: number): Promise`
- `wait(selector: string, timeout: number): Promise`
- `wait(func: Function, timeout: number, arg1, arg2...): Promise`- `func(arg1, arg2...): boolean`
- `waitOne(waitings: Array, timeout: number): Promise`
```js
const isFooPage = () => !!window.location.href.match(/foo/)
const flags = ['#succeessBox', '#failedBox', isfooPage]
let indexOfFlag = await page.waitOne(flags)
switch(indexOfFlag) {
case 0:
console.log('succeess')
break
case 1:
console.log('failed')
break
case 2:
throw 'foo?'
break
}
```