Skip to content
This repository has been archived by the owner on Nov 24, 2018. It is now read-only.

Prepare for v1.1 release #143

Merged
merged 9 commits into from
Aug 2, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ const chromeless = new Chromeless({
- [`exists(selector: string)`](docs/api.md#api-exists)
- [`screenshot()`](docs/api.md#api-screenshot)
- [`pdf(options?: PdfOptions)`](docs/api.md#api-pdf)
- [`getHtml()`](docs/api.md#api-gethtml)
- [`html()`](docs/api.md#api-html)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ha, I was actually going back and forth about the naming of getHtml() vs html() when I implemented this.

I originally called it getHtml() to avoid confusion with setHtml(), but I actually prefer html(), as it's more in line with the naming of screenshot() and pdf().

I like this change 👍

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@seangransee glad that you like it! The maintainers had a conversation about API design goals yesterday where we decided on this naming convention. I'll share more about the design goals soon in a docs/api-design.md linked to via CONTRIBUTING.md.

- [`cookiesGet()`](docs/api.md#api-cookiesget)
- [`cookiesGet(name: string)`](docs/api.md#api-cookiesget-name)
- [`cookiesGet(query: CookieQuery)`](docs/api.md#api-cookiesget-query) - Not implemented yet
Expand Down
6 changes: 3 additions & 3 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Chromeless provides TypeScript typings.
- [`exists(selector: string)`](#api-exists)
- [`screenshot()`](#api-screenshot)
- [`pdf(options?: PdfOptions)`](#api-pdf)
- [`getHtml()`](#api-gethtml)
- [`html()`](#api-html)
- [`cookiesGet()`](#api-cookiesget)
- [`cookiesGet(name: string)`](#api-cookiesget-name)
- [`cookiesGet(query: CookieQuery)`](#api-cookiesget-query) - Not implemented yet
Expand Down Expand Up @@ -431,7 +431,7 @@ console.log(pdf) // prints local file path or S3 URL

<a name="api-gethtml" />

### getHtml(): Chromeless<string>
### html(): Chromeless<string>

Get full HTML of the loaded page.

Expand All @@ -440,7 +440,7 @@ __Example__
```js
const html = await chromeless
.setHtml('<h1>Hello world!</h1>')
.getHtml()
.html()

console.log(html) // <html><head></head><body><h1>Hello world!</h1></body></html>
```
Expand Down
4 changes: 4 additions & 0 deletions serverless/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export function createPresignedURL(
accessKeyId = process.env.AWS_ACCESS_KEY_ID,
secretAccessKey = process.env.AWS_SECRET_ACCESS_KEY,
sessionToken = process.env.AWS_SESSION_TOKEN,
// expires = 0, // @TODO: 300, check if this is working http://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-query-string-auth.html
} = {}
): string {
const signed = aws4.sign(
Expand All @@ -23,6 +24,9 @@ export function createPresignedURL(
service,
region,
signQuery: true,
// headers: {
// 'X-Amz-Expires': expires,
// },
},
{
accessKeyId,
Expand Down
89 changes: 58 additions & 31 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { ChromelessOptions, Cookie, CookieQuery, PdfOptions } from './types'
import { getDebugOption } from './util'

export default class Chromeless<T extends any> implements Promise<T> {

private queue: Queue
private lastReturnPromise: Promise<any>

Expand Down Expand Up @@ -53,7 +52,10 @@ export default class Chromeless<T extends any> implements Promise<T> {
*/
readonly [Symbol.toStringTag]: 'Promise'

then<U>(onFulfill: (value: T) => U | PromiseLike<U>, onReject?: (error: any) => U | PromiseLike<U>): Promise<U> {
then<U>(
onFulfill: (value: T) => U | PromiseLike<U>,
onReject?: (error: any) => U | PromiseLike<U>
): Promise<U> {
return this.lastReturnPromise.then(onFulfill, onReject) as Promise<U>
}

Expand All @@ -62,19 +64,19 @@ export default class Chromeless<T extends any> implements Promise<T> {
}

goto(url: string): Chromeless<T> {
this.queue.enqueue({type: 'goto', url})
this.queue.enqueue({ type: 'goto', url })

return this
}

setUserAgent(useragent: string): Chromeless<T> {
this.queue.enqueue({type: 'setUserAgent', useragent})
this.queue.enqueue({ type: 'setUserAgent', useragent })

return this
}

click(selector: string): Chromeless<T> {
this.queue.enqueue({type: 'click', selector})
this.queue.enqueue({ type: 'click', selector })

return this
}
Expand All @@ -85,15 +87,15 @@ export default class Chromeless<T extends any> implements Promise<T> {
wait(firstArg, ...args: any[]): Chromeless<T> {
switch (typeof firstArg) {
case 'number': {
this.queue.enqueue({type: 'wait', timeout: firstArg})
this.queue.enqueue({ type: 'wait', timeout: firstArg })
break
}
case 'string': {
this.queue.enqueue({type: 'wait', selector: firstArg})
this.queue.enqueue({ type: 'wait', selector: firstArg })
break
}
case 'function': {
this.queue.enqueue({type: 'wait', fn: firstArg, args})
this.queue.enqueue({ type: 'wait', fn: firstArg, args })
break
}
default:
Expand All @@ -104,18 +106,18 @@ export default class Chromeless<T extends any> implements Promise<T> {
}

focus(selector: string): Chromeless<T> {
this.queue.enqueue({type: 'focus', selector})
return this
this.queue.enqueue({ type: 'focus', selector })
return this
}

press(keyCode: number, count?: number, modifiers?: any): Chromeless<T> {
this.queue.enqueue({type: 'press', keyCode, count, modifiers})
this.queue.enqueue({ type: 'press', keyCode, count, modifiers })

return this
}

type(input: string, selector?: string): Chromeless<T> {
this.queue.enqueue({type: 'type', input, selector})
this.queue.enqueue({ type: 'type', input, selector })

return this
}
Expand All @@ -133,27 +135,27 @@ export default class Chromeless<T extends any> implements Promise<T> {
}

mousedown(selector: string): Chromeless<T> {
this.queue.enqueue({type: 'mousedown', selector})
return this
this.queue.enqueue({ type: 'mousedown', selector })
return this
}

mouseup(selector: string): Chromeless<T> {
this.queue.enqueue({type: 'mouseup', selector})
return this
this.queue.enqueue({ type: 'mouseup', selector })
return this
}

mouseover(): Chromeless<T> {
throw new Error('Not implemented yet')
}

scrollTo(x: number, y: number): Chromeless<T> {
this.queue.enqueue({type: 'scrollTo', x, y})
this.queue.enqueue({ type: 'scrollTo', x, y })

return this
}

setHtml(html: string): Chromeless<T> {
this.queue.enqueue({type: 'setHtml', html})
this.queue.enqueue({ type: 'setHtml', html })

return this
}
Expand All @@ -162,38 +164,56 @@ export default class Chromeless<T extends any> implements Promise<T> {
throw new Error('Not implemented yet')
}

evaluate<U extends any>(fn: (...args: any[]) => void, ...args: any[]): Chromeless<U> {
this.lastReturnPromise = this.queue.process<U>({type: 'returnCode', fn: fn.toString(), args})
evaluate<U extends any>(
fn: (...args: any[]) => void,
...args: any[]
): Chromeless<U> {
this.lastReturnPromise = this.queue.process<U>({
type: 'returnCode',
fn: fn.toString(),
args,
})

return new Chromeless<U>({}, this)
}

inputValue(selector: string): Chromeless<string> {
this.lastReturnPromise = this.queue.process<string>({type: 'returnInputValue', selector})
this.lastReturnPromise = this.queue.process<string>({
type: 'returnInputValue',
selector,
})

return new Chromeless<string>({}, this)
}

exists(selector: string): Chromeless<boolean> {
this.lastReturnPromise = this.queue.process<boolean>({type: 'returnExists', selector})
this.lastReturnPromise = this.queue.process<boolean>({
type: 'returnExists',
selector,
})

return new Chromeless<boolean>({}, this)
}

screenshot(): Chromeless<string> {
this.lastReturnPromise = this.queue.process<string>({type: 'returnScreenshot'})
this.lastReturnPromise = this.queue.process<string>({
type: 'returnScreenshot',
})

return new Chromeless<string>({}, this)
}

getHtml(): Chromeless<string> {
this.lastReturnPromise = this.queue.process<string>({type: 'returnHtml'})
html(): Chromeless<string> {
this.lastReturnPromise = this.queue.process<string>({ type: 'returnHtml' })

return new Chromeless<string>({}, this)
}

pdf(options?: PdfOptions): Chromeless<string> {
this.lastReturnPromise = this.queue.process<string>({type: 'returnPDF', options})
this.lastReturnPromise = this.queue.process<string>({
type: 'returnPdf',
options,
})

return new Chromeless<string>({}, this)
}
Expand All @@ -212,18 +232,25 @@ export default class Chromeless<T extends any> implements Promise<T> {
* @param query
*/
cookiesGet(query: CookieQuery): Chromeless<Cookie[] | null>
cookiesGet(nameOrQuery?: string | CookieQuery): Chromeless<Cookie | Cookie[] | null> {
cookiesGet(
nameOrQuery?: string | CookieQuery
): Chromeless<Cookie | Cookie[] | null> {
if (typeof nameOrQuery !== 'undefined') {
throw new Error('Querying cookies is not implemented yet')
}

this.lastReturnPromise = this.queue.process<Cookie[] | Cookie | null>({type: 'cookiesGet', nameOrQuery})
this.lastReturnPromise = this.queue.process<Cookie[] | Cookie | null>({
type: 'cookiesGet',
nameOrQuery,
})

return new Chromeless<Cookie | Cookie[] | null>({}, this)
}

cookiesGetAll(): Chromeless<Cookie[]> {
this.lastReturnPromise = this.queue.process<Cookie[]>({type: 'cookiesGetAll'})
this.lastReturnPromise = this.queue.process<Cookie[]>({
type: 'cookiesGetAll',
})

return new Chromeless<Cookie[]>({}, this)
}
Expand All @@ -232,7 +259,7 @@ export default class Chromeless<T extends any> implements Promise<T> {
cookiesSet(cookie: Cookie): Chromeless<T>
cookiesSet(cookies: Cookie[]): Chromeless<T>
cookiesSet(nameOrCookies, value?: string): Chromeless<T> {
this.queue.enqueue({type: 'cookiesSet', nameOrCookies, value})
this.queue.enqueue({ type: 'cookiesSet', nameOrCookies, value })

return this
}
Expand All @@ -242,7 +269,7 @@ export default class Chromeless<T extends any> implements Promise<T> {
}

cookiesClearAll(): Chromeless<T> {
this.queue.enqueue({type: 'cookiesClearAll'})
this.queue.enqueue({ type: 'cookiesClearAll' })

return this
}
Expand Down
Loading