Documentation
¶
Overview ¶
Package browser drives a Chrome the operator runs beside the server, over the DevTools protocol: a fresh, isolated context per run, a page in it, and the few things a model needs — navigate, read the page as a tree it can point into, click, type, scroll, wait, screenshot. Every navigation and every request the page makes go through the same address guard as fetching a page: nothing private, nothing link-local, unless the operator listed it. Downloads are refused.
Nothing here launches Chrome. It connects to an endpoint, and tests speak to an in-process fake.
Index ¶
- Constants
- func IsWebAddress(address string) bool
- func OriginOf(address string) string
- type Browser
- type Commit
- type Context
- func (self *Context) AddPasskey(ctx context.Context, passkey Passkey) error
- func (self *Context) Back(ctx context.Context) (*Location, error)
- func (self *Context) CheckCurrentDocument(ctx context.Context) error
- func (self *Context) CheckNotScripted(ctx context.Context) error
- func (self *Context) Click(ctx context.Context, where Target) (string, *Location, error)
- func (self *Context) Close() error
- func (self *Context) Evaluate(ctx context.Context, expression string, maximum int) (string, error)
- func (self *Context) FillSecret(ctx context.Context, request FillRequest) error
- func (self *Context) Filled() *FilledValues
- func (self *Context) HasPasskey() bool
- func (self *Context) Hover(ctx context.Context, where Target) (string, error)
- func (self *Context) LastUsed() time.Time
- func (self *Context) Navigate(ctx context.Context, address string) (*Location, error)
- func (self *Context) PageURL(ctx context.Context) (string, error)
- func (self *Context) PrepareToFill(ctx context.Context)
- func (self *Context) Press(ctx context.Context, key string) error
- func (self *Context) RemovePasskey()
- func (self *Context) Reset(ctx context.Context) error
- func (self *Context) Screenshot(ctx context.Context, fullPage bool) ([]byte, error)
- func (self *Context) Scroll(ctx context.Context, direction string, amount int, where Target) error
- func (self *Context) Select(ctx context.Context, where Target, value string, index int) (string, error)
- func (self *Context) Snapshot(ctx context.Context, mode string, maximum int) (*Snapshot, error)
- func (self *Context) Type(ctx context.Context, where Target, text string, clearFirst, submit bool) error
- func (self *Context) Wait(ctx context.Context, condition, selector string, timeout time.Duration) (string, error)
- type Document
- type FieldKind
- type FillRequest
- type FilledValues
- func (self *FilledValues) Committed(tab string, commit Commit)
- func (self *FilledValues) ForgetPages()
- func (self *FilledValues) ForgetScriptedOrigins()
- func (self *FilledValues) HasBeenScripted(tab string) bool
- func (self *FilledValues) HasFilledDocument(tab string) bool
- func (self *FilledValues) HasScriptOnEveryPage(tab string) bool
- func (self *FilledValues) HasSecretHost(tab, host string) bool
- func (self *FilledValues) IsLocked(tab string) bool
- func (self *FilledValues) IsOriginScripted(address string) bool
- func (self *FilledValues) IsScripted(tab, documentID string) bool
- func (self *FilledValues) Lock(tab string, document Document)
- func (self *FilledValues) NoteCurrentDocument(tab, documentID string)
- func (self *FilledValues) NoteScriptOnEveryPage(tab string)
- func (self *FilledValues) NoteScripted(tab string, document Document)
- func (self *FilledValues) NoteScriptedAddress(tab, address string)
- func (self *FilledValues) NoteScriptedOrigin(origin string)
- func (self *FilledValues) NoteScriptedOrigins(origins []string)
- func (self *FilledValues) Remember(values ...string)
- func (self *FilledValues) RememberOneTimeCode(period time.Duration, code string)
- func (self *FilledValues) RememberSecretHost(tab, host string)
- func (self *FilledValues) ResetCommitCounts()
- func (self *FilledValues) ScriptedOrigins() []string
- func (self *FilledValues) Scrub(text string) string
- type Location
- type Passkey
- type Settings
- type Snapshot
- type Target
Constants ¶
const ScriptOnEveryPageWords = "" /* 224-byte string literal not displayed */
ScriptOnEveryPageWords is what the model is told when a tab was given a script that runs in every page it loads.
const ScriptedPageWords = "This page has run the agent's script, so it is not filled. Sign in before you run any script on a site."
ScriptedPageWords is what the model is told when a page is not filled because the agent ran script in it and the site it is on could not be told, here and by the tab target.
const ScriptedSiteInTabWords = "" /* 362-byte string literal not displayed */
ScriptedSiteInTabWords is what the model is told when the person's browser does not fill a page because the agent ran a script on its site.
const ScriptedSiteWords = "" /* 283-byte string literal not displayed */
ScriptedSiteWords is what the model is told when the headless browser does not fill a page because the agent ran a script on its site.
Variables ¶
This section is empty.
Functions ¶
func IsWebAddress ¶ added in v0.193.0
IsWebAddress says the address is an http or https one, whether or not its site can be read (OriginOf): a page that is not one (about:blank, a data page, an error page) belongs to no site.
func OriginOf ¶ added in v0.193.0
OriginOf is the site an address is on, written the one way the scripted marks are compared: scheme, lower-case host without a leading "www." (the fill treats it as the same site) and the port when it is not the scheme's own. It is empty for an address that is not an http or https one, and for one whose site cannot be read (see IsWebAddress).
Only the scheme and the part up to the first "/", "?" or "#" are parsed. A path, query or fragment that is not valid on its own (a stray "%") is still a page of the site, and parsing the whole address would fail on it and name no site at all.
Types ¶
type Browser ¶
type Browser struct {
// contains filtered or unexported fields
}
Browser is a connection to a Chrome.
type Commit ¶ added in v0.193.0
type Commit struct {
DocumentID string
Address string
// IsBackForward says the commit is a move through the history, which does
// not make a page new: the filled document may be what comes back.
IsBackForward bool
// Count numbers the tab's commits in the order they happened.
Count int64
}
Commit is a tab committing a document: the browser says a main frame now shows another document.
type Context ¶
type Context struct {
// contains filtered or unexported fields
}
Context is a fresh, isolated browser context with one page in it: no cookies, no storage, no history from anyone else.
func (*Context) AddPasskey ¶ added in v0.193.0
AddPasskey gives the page a virtual authenticator that holds only this passkey, answers for the person without asking, and is removed after the page's next navigation or after a minute, whichever is first, and when the context closes. A second call replaces the first.
func (*Context) Back ¶
Back goes to the previous page. It returns once the browser has moved to the previous entry of its history (the move itself is not instant: the old page stays the current document until the new one commits), so that whatever the caller does next acts on the page that was gone back to, not on the one that was left a moment before.
func (*Context) CheckCurrentDocument ¶ added in v0.193.0
CheckCurrentDocument asks the browser which document the page shows right now and locks the page if a value was put into that document. Going back can restore the filled document from the history's cache a moment before the event that reports it reaches the lock (and before the server's own history read finishes), so a command that could read a field back asks first instead of trusting the events alone. It does nothing for a page nothing was filled into. The error says the browser did not answer; the caller then refuses the command rather than guess.
func (*Context) CheckNotScripted ¶ added in v0.193.0
CheckNotScripted says the page can be filled: the agent has run no script on the site it shows now, and none in the document. It also closes every window but the page, which a script may have opened (a window outlives the document that opened it and can reach back into the site's pages). The error is the words for the model. The browser is asked which document the page shows only if a script was ever run, and when it does not answer the page is refused.
func (*Context) Evaluate ¶
Evaluate runs an expression and returns its JSON value, bounded.
A script is the agent's, and can leave a listener or a changed form behind that a later sign-in would feed, and a window, a worker or a handler that outlives its page. So the site it ran on is marked and is not filled until the browser is reset (see CheckNotScripted). The mark is made before the script and again after it returns, and for every document the page committed meanwhile: a page that is about to be replaced can run the script in the document that replaced it, which the first mark did not name.
func (*Context) FillSecret ¶ added in v0.193.0
func (self *Context) FillSecret(ctx context.Context, request FillRequest) error
FillSecret puts text into the field named by the request, replacing what is in it, in one step in the page (see fillScript). Only a ref from the last snapshot names a field, and only a field of the kind asked for takes the text. The error says what was wrong and never repeats the text.
func (*Context) Filled ¶ added in v0.193.0
func (self *Context) Filled() *FilledValues
Filled is what a sign-in put into this page: the values to hide from the model and whether the page is locked.
func (*Context) HasPasskey ¶ added in v0.193.0
HasPasskey says whether a virtual authenticator is in the page.
func (*Context) Navigate ¶
Navigate opens an address, after the guard, and waits for the page to load.
func (*Context) PageURL ¶ added in v0.193.0
PageURL is the address of the page as the browser itself reports it, from its navigation history, not from a script the page could answer for itself.
func (*Context) PrepareToFill ¶ added in v0.193.0
PrepareToFill is called before a value goes into the page. It stops the browser from keeping what the page asks of the network (a request's body is where a form's password goes), locks the page against what could read a field back (see FilledValues), and watches the page's main frame so that the lock ends only when it commits a new document. The values themselves are given to Filled().Remember, before this.
func (*Context) RemovePasskey ¶ added in v0.193.0
func (self *Context) RemovePasskey()
RemovePasskey takes the virtual authenticator away, if there is one.
func (*Context) Reset ¶ added in v0.193.0
Reset throws the page away and opens a new one in a new browser context, as when it was first opened: no cookie, storage, history, window or worker from before is there. A script the agent ran leaves things behind that outlive its page, so a site it ran a script on is not filled again until this is done (see CheckNotScripted); the values to hide from the model stay hidden.
func (*Context) Screenshot ¶
Screenshot is the viewport, or the whole page, as PNG.
func (*Context) Select ¶
func (self *Context) Select(ctx context.Context, where Target, value string, index int) (string, error)
Select chooses an option of a select by value, text or index.
func (*Context) Type ¶
func (self *Context) Type(ctx context.Context, where Target, text string, clearFirst, submit bool) error
Type types into an element, whatever kind it is. It is the person's agent doing what they asked; what stands between it and a field is the card they answer, not a list of fields kept here.
type Document ¶ added in v0.193.0
type Document struct {
// ID identifies the document (the browser's document id for a tab of the
// person's browser, the loader id for the headless page). It may be empty
// when the browser did not say.
ID string
// Address is the document's address.
Address string
// CommitCount is the number of commits the browser had counted for the tab
// when the value went in; a report of an earlier commit is not news.
CommitCount int64
}
Document is where a value was put: a tab's document.
type FieldKind ¶ added in v0.193.0
type FieldKind string
FieldKind is what a field is to hold, which the field must be able to hold.
const ( // FieldKindUserName is a text, email or telephone input, or one whose // autocomplete says username or email. FieldKindUserName FieldKind = "userName" // FieldKindPassword is a password input, or one whose autocomplete says // current-password or new-password. FieldKindPassword FieldKind = "password" // FieldKindOneTimeCode is a text, number or telephone input, or one whose // autocomplete says one-time-code. FieldKindOneTimeCode FieldKind = "oneTimeCode" )
type FillRequest ¶ added in v0.193.0
type FillRequest struct {
// Ref is the field, by the ref of the last snapshot.
Ref int
// Host is the host the page must be on (lower case, without a leading
// "www."), and Port the port, which is checked when it is not zero.
Host string
Port int
// Kind is what the field must be able to hold.
Kind FieldKind
// Text is the value.
Text string
}
FillRequest is one value to put into one field.
type FilledValues ¶ added in v0.193.0
type FilledValues struct {
// contains filtered or unexported fields
}
FilledValues is what the agent's sign-in put into pages and must not read back: passwords and one-time codes, kept in memory only and never written anywhere, together with which pages (tabs) are locked because of it.
It does two things for the browser tool.
The lock. A tab that was filled is locked: the tool does not run a script on it, take a picture of it, read what it asks of the network or send it a raw DevTools command that could read a field, because each of those is a way to ask a page for the value of a field. The lock is kept for the tab and for its document, not for the tool as a whole:
- another tab is not locked by it, and switching to or from a tab does not change it;
- a change that keeps the document (history.pushState, a fragment) does not end it;
- it ends when the tab commits a new document that is not a move through the history (back or forward);
- a move through the history that returns to the filled document, or to an address that was filled, locks the tab again.
The scrubbing. For as long as the values are remembered, every result and error of the tool has them replaced, in the ways a page or a network log writes a value (see writtenForms). Values stay remembered after the page moved on, so what an old page left in the results is still hidden. A one-time code is forgotten a little after its period ends: it opens nothing by then.
The zero value holds nothing and is ready to use; a nil one is safe to ask.
func (*FilledValues) Committed ¶ added in v0.193.0
func (self *FilledValues) Committed(tab string, commit Commit)
Committed is the browser saying the tab committed a document. Reports are applied in the order of their counts; a report older than one already seen is ignored. A tab that was never filled holds no lock to end.
func (*FilledValues) ForgetPages ¶ added in v0.193.0
func (self *FilledValues) ForgetPages()
ForgetPages forgets what is known of the pages: which are locked, which a script ran in, which sites it ran on and where a secret was put. It keeps the values to hide (they were typed once and stay hidden from the model), for a browser that was reset: it has no page, cookie or storage from before.
func (*FilledValues) ForgetScriptedOrigins ¶ added in v0.193.0
func (self *FilledValues) ForgetScriptedOrigins()
ForgetScriptedOrigins lets the agent fill on every site again.
func (*FilledValues) HasBeenScripted ¶ added in v0.193.0
func (self *FilledValues) HasBeenScripted(tab string) bool
HasBeenScripted says the agent ran script in some document of the tab, so that asking the browser which document it shows now is worth a round trip.
func (*FilledValues) HasFilledDocument ¶ added in v0.193.0
func (self *FilledValues) HasFilledDocument(tab string) bool
HasFilledDocument says a value was put into some document of the tab, so that asking the browser which document it shows now is worth a round trip.
func (*FilledValues) HasScriptOnEveryPage ¶ added in v0.193.0
func (self *FilledValues) HasScriptOnEveryPage(tab string) bool
HasScriptOnEveryPage says the tab, or a tab the browser did not name, was given something that runs in every page it loads.
func (*FilledValues) HasSecretHost ¶ added in v0.193.0
func (self *FilledValues) HasSecretHost(tab, host string) bool
HasSecretHost says a secret was put into a page of the host in the tab. An empty host asks whether one was put into any host.
func (*FilledValues) IsLocked ¶ added in v0.193.0
func (self *FilledValues) IsLocked(tab string) bool
IsLocked says whether a script, a picture or a raw protocol command could read a value on the tab: it, or a tab that the browser did not name, holds a filled document it has not left.
func (*FilledValues) IsOriginScripted ¶ added in v0.193.0
func (self *FilledValues) IsOriginScripted(address string) bool
IsOriginScripted says the agent ran a script on the site the address is on.
func (*FilledValues) IsScripted ¶ added in v0.193.0
func (self *FilledValues) IsScripted(tab, documentID string) bool
IsScripted says the document the tab shows now is one the agent ran script in. With the document's id (the headless browser asks the browser for it) the answer is exact; without it the answer is the tab's mark, which the commits the extension reports keep.
func (*FilledValues) Lock ¶ added in v0.193.0
func (self *FilledValues) Lock(tab string, document Document)
Lock says a value was put into a document of the tab, and locks the tab. The tab is whatever the browser calls it: an empty name is the one page of the headless browser, or a tab of the person's browser whose number the extension did not say.
func (*FilledValues) NoteCurrentDocument ¶ added in v0.193.0
func (self *FilledValues) NoteCurrentDocument(tab, documentID string)
NoteCurrentDocument is the answer to asking the browser, right now, which document the tab shows. Events say the same thing but can arrive late: a move back can restore the filled document from the history's cache before the event that reports it, so a command that is about to run asks instead of waiting for the event. If the document is one a value was put into, the tab is locked (and stays so until a new document is committed).
func (*FilledValues) NoteScriptOnEveryPage ¶ added in v0.193.0
func (self *FilledValues) NoteScriptOnEveryPage(tab string)
NoteScriptOnEveryPage says the tab was given something that runs in every page it loads (see filledTab.hasScriptOnEveryPage).
func (*FilledValues) NoteScripted ¶ added in v0.193.0
func (self *FilledValues) NoteScripted(tab string, document Document)
NoteScripted says the agent ran script in a document of the tab, or changed it with a DevTools command, before anything was filled into it. A page that has run the agent's script can hold a listener that sends a field's value out or a form that sends it to another site, neither of which the lock or the check of the form (which look at the page as it is) can rule out, so it is not filled. A document with no id is taken to be the newest one the tab committed.
func (*FilledValues) NoteScriptedAddress ¶ added in v0.193.0
func (self *FilledValues) NoteScriptedAddress(tab, address string)
NoteScriptedAddress is NoteScriptedOrigin for the address of a document the agent ran a script in. When the address is a web one whose site cannot be read, nothing can say which pages the script was on, so it is taken to be on every page of the tab (NoteScriptOnEveryPage), as a script that cannot be placed at all is.
func (*FilledValues) NoteScriptedOrigin ¶ added in v0.193.0
func (self *FilledValues) NoteScriptedOrigin(origin string)
NoteScriptedOrigin says the agent ran a script on a site, or in a window it opened on that site, and so on every page of the site from now on. The origin is whatever OriginOf gives; anything else is ignored. A fill, a one-time code or a passkey is refused on a page of a scripted origin for as long as this value lives (the headless page: until it is reset; the person's browser: until the person allows it again).
func (*FilledValues) NoteScriptedOrigins ¶ added in v0.193.0
func (self *FilledValues) NoteScriptedOrigins(origins []string)
NoteScriptedOrigins is NoteScriptedOrigin for each address given.
func (*FilledValues) Remember ¶ added in v0.193.0
func (self *FilledValues) Remember(values ...string)
Remember keeps values, to hide them from the model, for as long as this value lives. It locks nothing: Lock does.
func (*FilledValues) RememberOneTimeCode ¶ added in v0.193.0
func (self *FilledValues) RememberOneTimeCode(period time.Duration, code string)
RememberOneTimeCode keeps a code, to hide it from the model, until its period of the given length has ended and a little longer.
func (*FilledValues) RememberSecretHost ¶ added in v0.193.0
func (self *FilledValues) RememberSecretHost(tab, host string)
RememberSecretHost says a password or a one-time code was put into a page of this host in the tab. It lasts as long as the tab: the cookies and the storage that sign-in made are then the site's session, which the agent does not read back.
func (*FilledValues) ResetCommitCounts ¶ added in v0.193.0
func (self *FilledValues) ResetCommitCounts()
ResetCommitCounts forgets the numbering of commits, for a browser that connected again: it numbers from the start.
func (*FilledValues) ScriptedOrigins ¶ added in v0.193.0
func (self *FilledValues) ScriptedOrigins() []string
ScriptedOrigins are the sites the agent ran a script on, sorted.
func (*FilledValues) Scrub ¶ added in v0.193.0
func (self *FilledValues) Scrub(text string) string
Scrub replaces every remembered value in the text with [hidden], in every form writtenForms lists.
type Location ¶
type Location struct {
URL string `json:"url"`
Title string `json:"title"`
Status int `json:"status,omitempty"`
}
Location is where a navigation ended.
type Passkey ¶ added in v0.193.0
type Passkey struct {
CredentialID []byte
RelyingPartyID string
UserHandle []byte
PrivateKey []byte
}
Passkey is one passkey as a virtual authenticator holds it. The byte values are sent to the browser as base64, which is how the protocol takes them; PrivateKey is an ES256 key as PKCS#8 DER.
type Settings ¶
type Settings struct {
// Endpoint is http://host:9222 or a ws:// debugger address.
Endpoint string
// AllowPrivate lists hosts or CIDRs the operator lets the browser
// reach although they are private.
AllowPrivate []string
// ProxyListen is where the guarded proxy binds. Empty means every
// address on an unused port, and Chrome is told the address its own
// connection to this server came from -- which is reachable from
// wherever it is running.
ProxyListen string
}
Settings are what a browser needs.