/foxr

🦊 Node.js API to control Firefox

Primary LanguageTypeScriptMIT LicenseMIT

foxr

npm tests coverage

Node.js API to control Firefox.

*At this point Foxr is more a working proof of concept. Although the goal is to have a fully compatible to Puppeteer API, or at least a subset of it, the work is pretty much in progress.

Example

Run a locally installed Firefox:

/path/to/firefox -headless -marionette -safe-mode

Or a dockerized version:

docker run -it --rm --shm-size 2g -p 2828:2828 deepsweet/firefox-headless-remote:61
import foxr from 'foxr'

(async () => {
  try {
    const browser = await foxr.connect()
    const page = await browser.newPage()

    await page.goto('https://example.com')
    await page.screenshot({ path: 'example.png' })
    await browser.close()
  } catch (error) {
    console.error(error)
  }
})()

Requirements

Install

yarn add --dev foxr
# or
npm install --dev foxr

API

Foxr

connect

Connect to the Marionette endpoint.

type TOptions = {
  host?: string,
  port?: number
}

foxr.connect(options?: TOptions): Promise<TBrowser>
  • host – 'localhost' by default
  • port – 2828 by default

Browser

close

browser.close(): Promise<void>

disconnect

browser.disconnect(): Promise<void>

newPage

browser.newPage(): Promise<TPage>

pages

browser.pages(): Promise<TPage[]>

Page

$

page.$(selector: string): Promise<TElement>

$$

page.$$(selector: string): Promise<TElement[]>

close

page.close(): Promise<void>

evaluate

type TAnyJson = boolean | number | string | null | TJsonArray | TJsonMap
interface TJsonMap { [key: string]: TAnyJson }
interface TJsonArray extends Array<TAnyJson> {}

type TSerializableFunction = (...args: TAnyJson[]) => TAnyJson

page.evaluate(target: TSerializableFunction | string): Promise<TAnyJson>

goto

page.goto(url: string): Promise<void>

screenshot

type TOptions = {
  path?: string
}

page.screenshot(options?: TOptions): Promise<Buffer>

setContent

page.setContent(html: string): Promise<void>

title

page.title(): Promise<string>

Element

$

element.$(selector: string): Promise<TElement>

$$

element.$$(selector: string): Promise<TElement[]>

screenshot

type TOptions = {
  path?: string
}

element.screenshot(options?: TOptions): Promise<Buffer>

Development

See my Start task runner preset for details.

References