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

provide ability to listen to network events being made and received #177

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
48 changes: 48 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,54 @@ export default class Chromeless<T extends any> implements Promise<T> {
return this
}

requestWillBeSentEvent(func: void): Chromeless<T> {
this.queue.enqueue({type: 'requestWillBeSentEvent', func})

return this
}

requestSentEvent(func: void): Chromeless<T> {
this.queue.enqueue({type: 'requestSentEvent', func})

return this
}

responseReceivedEvent(func: void): Chromeless<T> {
this.queue.enqueue({type: 'responseReceivedEvent', func})

return this
}

requestServedFromCacheEvent(func: void): Chromeless<T> {
this.queue.enqueue({type: 'requestServedFromCacheEvent', func})

return this
}

dataReceivedEvent(func: void): Chromeless<T> {
this.queue.enqueue({type: 'dataReceivedEvent', func})

return this
}

loadingFinishedEvent(func: void): Chromeless<T> {
this.queue.enqueue({type: 'loadingFinishedEvent', func})

return this
}

loadingFailedEvent(func: void): Chromeless<T> {
this.queue.enqueue({type: 'loadingFailedEvent', func})

return this
}

requestInterceptedEvent(func: void): Chromeless<T> {
this.queue.enqueue({type: 'requestInterceptedEvent', func})

return this
}

async end(): Promise<T> {
const result = await this.lastReturnPromise
await this.queue.end()
Expand Down
66 changes: 66 additions & 0 deletions src/chrome/local-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,77 @@ export default class LocalRuntime {
return this.focus(command.selector)
case 'clearInput':
return this.clearInput(command.selector)
case 'requestWillBeSentEvent':
return this.requestWillBeSentEvent(command.func)
case 'requestSentEvent':
return this.requestSentEvent(command.func)
case 'dataReceivedEvent':
return this.dataReceivedEvent(command.func)
case 'requestServedFromCacheEvent':
return this.requestServedFromCacheEvent(command.func)
case 'dataReceivedEvent':
return this.dataReceivedEvent(command.func)
case 'loadingFinishedEvent':
return this.loadingFinishedEvent(command.func)
case 'loadingFailedEvent':
return this.loadingFailedEvent(command.func)
case 'requestInterceptedEvent':
return this.requestInterceptedEvent(command.func)
case 'responseReceivedEvent':
return this.responseReceivedEvent(command.func)
default:
throw new Error(`No such command: ${JSON.stringify(command)}`)
}
}

private async requestWillBeSentEvent(func: void): Promise<void> {
const { Network } = this.client
Network.requestWillBeSent(func)
console.log('requestWillBeSent eventlistener added')
}

private async requestSentEvent(func: void): Promise<void> {
const { Network } = this.client
Network.requestSent(func)
console.log('requestSent eventlistener added')
}

private async dataReceivedEvent(func: void): Promise<void> {
const { Network } = this.client
Network.dataReceived(func)
console.log('dateReceived eventlistener added')
}

private async requestServedFromCacheEvent(func: void): Promise<void> {
const { Network } = this.client
Network.requestServedFromCache(func)
console.log('requestServedFromCache eventlistener added')
}

private async loadingFinishedEvent(func: void): Promise<void> {
const { Network } = this.client
Network.loadingFinished(func)
console.log('loadingFinished eventlistener added')
}

private async loadingFailedEvent(func: void): Promise<void> {
const { Network } = this.client
Network.loadingFailed(func)
console.log('loadingFailed eventlistener added')
}

private async requestInterceptedEvent(func: void): Promise<void> {
const { Network } = this.client
Network.requestIntercepted(func)
console.log('requestIntercepted eventlistener added')
}

private async responseReceivedEvent(func: void): Promise<void> {
const { Network } = this.client
Network.responseReceived(func)
console.log('responseReceived eventlistener added')
}

private async goto(url: string): Promise<void> {
const { Network, Page } = this.client
await Promise.all([Network.enable(), Page.enable()])
Expand Down
32 changes: 32 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,38 @@ export type Command =
type: 'clearInput'
selector: string
}
| {
type: 'requestWillBeSentEvent'
func: void
}
| {
type: 'requestSentEvent'
func: void
}
| {
type: 'dataReceivedEvent'
func: void
}
| {
type: 'requestServedFromCacheEvent'
func: void
}
| {
type: 'loadingFinishedEvent'
func: void
}
| {
type: 'loadingFailedEvent'
func: void
}
| {
type: 'requestInterceptedEvent'
func: void
}
| {
type: 'responseReceivedEvent'
func: void
}

export interface Cookie {
url?: string
Expand Down