firefox

package module
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 13, 2026 License: BSD-3-Clause Imports: 13 Imported by: 1

README

go-firefox

Build status GoDoc

go-firefox is a set of tools to drive Firefox through the marionette protocol.

Documentation

Overview

Package firefox provides tools to script a marionetted Firefox.

Index

Examples

Constants

View Source
const (
	ByID    = marionette.ID
	ByQuery = marionette.CSS_SELECTOR
)

Variables

This section is empty.

Functions

func Attribute

func Attribute[T any](e *WebElement, name string) (T, error)

func Click

func Click(f Finder, by By, sel string) error

func ElementIsNotPresent added in v0.2.0

func ElementIsNotPresent(by By, v string) func(f Finder) (bool, *WebElement, error)

func ElementIsPresent added in v0.2.0

func ElementIsPresent(by By, v string) func(f Finder) (bool, *WebElement, error)

func GetPref added in v0.2.0

func GetPref[T any](cli *Client, name string) (T, error)

GetPref retrieves the named preference from 'about:config'

func InnerHTML

func InnerHTML(f Finder, by By, sel string) (string, error)

func Property

func Property[T any](e *WebElement, name string) (T, error)

func SendKeys

func SendKeys(f Finder, by By, sel, keys string) error

func SetPref added in v0.2.0

func SetPref[T any](cli *Client, name string, v T) error

SetPref modifies the named preference from 'about:config' with the provided value v.

func TextContent

func TextContent(f Finder, by By, sel string) (string, error)

func WaitNotPresent

func WaitNotPresent(f Finder, by By, sel string) error

func WaitVisible

func WaitVisible(f Finder, by By, sel string) error

Types

type By

type By = marionette.By

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is a Firefox client.

Client provides tools to script a marionetted Firefox instance.

Example (GetSetPrefs)
package main

import (
	"encoding/json"
	"fmt"
	"log"
	"os"

	"git.sr.ht/~sbinet/firefox"
)

func main() {
	dir, err := os.MkdirTemp("", "go-firefox-")
	if err != nil {
		log.Fatalf("could not create tmp dir: %+v", err)
	}
	defer os.RemoveAll(dir)

	cli, err := firefox.New(firefox.WithFirefoxProfile(dir), firefox.WithMarionettePort(-1))
	if err != nil {
		log.Fatalf("could not create firefox marionette: %+v", err)
	}
	defer cli.Close()

	{
		k := "browser.bookmarks.defaultLocation"
		v, err := firefox.GetPref[string](cli, k)
		if err != nil {
			log.Fatalf("could not get %q: %+v", k, err)
		}
		fmt.Printf("pref[%s]: %q\n", k, v)

		err = firefox.SetPref(cli, k, v+"XXX")
		if err != nil {
			log.Fatalf("could not modify %q: %+v", k, err)
		}

		v, err = firefox.GetPref[string](cli, k)
		if err != nil {
			log.Fatalf("could not get %q: %+v", k, err)
		}
		fmt.Printf("pref[%s]: %q\n", k, v)
	}

	{
		k := "browser.chrome.site_icons"
		v, err := firefox.GetPref[bool](cli, k)
		if err != nil {
			log.Fatalf("could not get %q: %+v", k, err)
		}
		fmt.Printf("pref[%s]: %+v\n", k, v)

		err = firefox.SetPref(cli, k, !v)
		if err != nil {
			log.Fatalf("pref: %+v", err)
		}

		v, err = firefox.GetPref[bool](cli, k)
		if err != nil {
			log.Fatalf("could not get %q: %+v", k, err)
		}
		fmt.Printf("pref[%s]: %+v\n", k, v)
	}

	{
		k := "apz.android.chrome_fling_physics.friction"
		err = firefox.SetPref(cli, k, "1.1")
		if err != nil {
			log.Fatalf("pref: %+v", err)
		}

		v, err := firefox.GetPref[json.Number](cli, k)
		if err != nil {
			log.Fatalf("could not get %q: %+v", k, err)
		}
		fmt.Printf("pref[%s]: %+v\n", k, v)
	}

	{
		k := "browser.contentblocking.cfr-milestone.milestones"
		v, err := firefox.GetPref[string](cli, k)
		if err != nil {
			log.Fatalf("could not get %q: %+v", k, err)
		}
		fmt.Printf("pref[%s]: %q\n", k, v)

		err = firefox.SetPref(cli, k, v+"XXX")
		if err != nil {
			log.Fatalf("could not modify %q: %+v", k, err)
		}

		v, err = firefox.GetPref[string](cli, k)
		if err != nil {
			log.Fatalf("could not get %q: %+v", k, err)
		}
		fmt.Printf("pref[%s]: %q\n", k, v)
	}

}
Output:
pref[browser.bookmarks.defaultLocation]: "toolbar"
pref[browser.bookmarks.defaultLocation]: "toolbarXXX"
pref[browser.chrome.site_icons]: true
pref[browser.chrome.site_icons]: false
pref[apz.android.chrome_fling_physics.friction]: 1.1
pref[browser.contentblocking.cfr-milestone.milestones]: "[1000, 5000, 10000, 25000, 50000, 100000, 250000, 314159, 500000, 750000, 1000000, 1250000, 1500000, 1750000, 2000000, 2250000, 2500000, 8675309]"
pref[browser.contentblocking.cfr-milestone.milestones]: "[1000, 5000, 10000, 25000, 50000, 100000, 250000, 314159, 500000, 750000, 1000000, 1250000, 1500000, 1750000, 2000000, 2250000, 2500000, 8675309]XXX"

func New

func New(opts ...Option) (*Client, error)

New creates a new Firefox client with the provided options.

func (*Client) AddonInstall added in v0.2.0

func (cli *Client) AddonInstall(xpi io.Reader) (string, error)

AddonInstall installs the provided xpi addon and returns its ID.

func (*Client) AddonUninstall added in v0.2.0

func (cli *Client) AddonUninstall(xpi string) error

AddonUninstall uninstalls the xpi addon, referenced by name.

func (*Client) Browser

func (cli *Client) Browser() *marionette.Client

Browser returns the marionette client connected to the underlying Firefox browser.

func (*Client) Close

func (cli *Client) Close() (err error)

Close closes the underlying Firefox browser and cleans everything up.

func (*Client) Context added in v0.2.0

func (cli *Client) Context() (ctx Context, err error)

Context returns the marionette server context.

func (*Client) ExecuteScript

func (cli *Client) ExecuteScript(src Script, opts ...ScriptOption) (*Response, error)

func (*Client) FindElement

func (cli *Client) FindElement(by By, v string) (*WebElement, error)

FindElement finds an element using the indicated search strategy.

func (*Client) FindElements

func (cli *Client) FindElements(by By, v string) ([]*WebElement, error)

FindElements finds elements using the indicated search strategy.

func (*Client) Navigate

func (cli *Client) Navigate(v string) (*Response, error)

Navigate opens the url v

func (*Client) SetContext added in v0.2.0

func (cli *Client) SetContext(ctx Context) (*Response, error)

SetContext sets the context of the marionette server.

type Context added in v0.2.0

type Context = marionette.Context
var (
	Chrome  Context = marionette.CHROME
	Content Context = marionette.CONTENT
)

type Finder

type Finder = marionette.Finder

type Option

type Option func(c *Client) error

Option configures how a Firefox client should be customized.

func WithCapabilities added in v0.2.0

func WithCapabilities(caps marionette.Capabilities) Option

WithCapabilities modifies the default capabilities the marionette client will use.

func WithFirefoxProfile

func WithFirefoxProfile(dir string) Option

WithFirefoxProfile provides the path to the Firefox profile to use.

func WithHeadless

func WithHeadless(v bool) Option

WithHeadless instructs a Firefox client whether it should start with a GUI.

func WithKiosk

func WithKiosk(v bool) Option

WithKiosk instructs a Firefox client whether it should start in kiosk mode.

func WithMarionettePort added in v0.2.0

func WithMarionettePort(port int) Option

WithMarionettePort modifies the port to use for marionette. If the provided port is 0, the default marionette port is used. If the provided port is negative, a random one is used.

func WithTor added in v0.2.0

func WithTor(v bool) Option

WithTor modifies the Firefox client to use Tor (through SOCK5 proxy).

func WithUserAgent added in v0.2.0

func WithUserAgent(name string) Option

WithUserAgent modifies the default user agent.

type Response

type Response = marionette.Response

type Script

type Script struct {
	// contains filtered or unexported fields
}

func NewScript

func NewScript(src string, args ...any) Script

type ScriptOption

type ScriptOption func(scr *Script) error

func WithScriptNewSandbox

func WithScriptNewSandbox(v bool) ScriptOption

func WithScriptTimeout

func WithScriptTimeout(d time.Duration) ScriptOption

type WebElement

type WebElement = marionette.WebElement

func Nodes

func Nodes(f Finder, by By, sel string) ([]*WebElement, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL