browser

package module
v0.0.0-...-44cfe45 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2024 License: MIT Imports: 9 Imported by: 0

README

Browser

GitHub go.mod Go version Go Report Card GitHub
Browser is a ski JS plugin for web automation via Chrome DevTools protocol

License

Distributed under the MIT license.

Documentation

Overview

Package browser the JS browser implementations

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewElement

func NewElement(ele *rod.Element, vm *sobek.Runtime) sobek.Value

NewElement creates a new Element mapping

func NewPage

func NewPage(p *rod.Page, rt *sobek.Runtime) sobek.Value

NewPage creates a new Page mapping

Types

type Browser

type Browser struct{}

func (Browser) Instantiate

func (Browser) Instantiate(rt *sobek.Runtime) (sobek.Value, error)

type Element

type Element struct {
	*rod.Element
}

Element represents the DOM element

func (*Element) BackgroundImage

func (el *Element) BackgroundImage(_ sobek.FunctionCall, vm *sobek.Runtime) (ret sobek.Value)

BackgroundImage returns the css background-image of the element

func (*Element) ContainsElement

func (el *Element) ContainsElement(element sobek.Value) (bool, error)

ContainsElement check if the target is equal or inside the element.

func (*Element) Describe

func (el *Element) Describe(call sobek.FunctionCall, vm *sobek.Runtime) (ret sobek.Value)

Describe the current element. The depth is the maximum depth at which children should be retrieved, defaults to 1, use -1 for the entire subtree or provide an integer larger than 0. The pierce decides whether or not iframes and shadow roots should be traversed when returning the subtree. The returned proto.DOMNode.NodeID will always be empty, because NodeID is not stable (when proto.DOMDocumentUpdated is fired all NodeID on the page will be reassigned to another value) we don't recommend using the NodeID, instead, use the BackendNodeID to identify the element.

func (*Element) ElementByJS

func (el *Element) ElementByJS(call sobek.FunctionCall, vm *sobek.Runtime) (ret sobek.Value)

ElementByJS returns the element from the return value of the js

func (*Element) ElementR

func (el *Element) ElementR(selector, jsRegex string) (any, error)

ElementR returns the first child element that matches the css selector and its text matches the jsRegex.

func (*Element) Elements

func (el *Element) Elements(selector string) (any, error)

Elements returns all elements that match the css selector

func (*Element) ElementsByJS

func (el *Element) ElementsByJS(call sobek.FunctionCall, vm *sobek.Runtime) (ret sobek.Value)

ElementsByJS returns the elements from the return value of the js

func (*Element) ElementsX

func (el *Element) ElementsX(xpath string) (any, error)

ElementsX returns all elements that match the XPath selector

func (*Element) Equal

func (el *Element) Equal(elm sobek.Value) (bool, error)

Equal checks if the two elements are equal.

func (*Element) Eval

func (el *Element) Eval(call sobek.FunctionCall, vm *sobek.Runtime) (ret sobek.Value)

Eval is a shortcut for Element.Evaluate with AwaitPromise, ByValue and AutoExp set to true.

func (*Element) Evaluate

func (el *Element) Evaluate(call sobek.FunctionCall, vm *sobek.Runtime) (ret sobek.Value)

Evaluate is just a shortcut of Page.Evaluate with This set to current element.

func (*Element) Frame

func (el *Element) Frame(_ sobek.FunctionCall, vm *sobek.Runtime) (ret sobek.Value)

Frame creates a page instance that represents the iframe

func (*Element) Has

func (el *Element) Has(selector string) (bool, any, error)

Has an element that matches the css selector

func (*Element) HasR

func (el *Element) HasR(selector, jsRegex string) (bool, any, error)

HasR an element that matches the css selector and its display text matches the jsRegex.

func (*Element) HasX

func (el *Element) HasX(selector string) (bool, any, error)

HasX an element that matches the XPath selector

func (*Element) Interactable

func (el *Element) Interactable(_ sobek.FunctionCall, vm *sobek.Runtime) (ret sobek.Value)

Interactable checks if the element is interactable with cursor. The cursor can be mouse, finger, stylus, etc. If not interactable err will be ErrNotInteractable, such as when covered by a modal,

func (*Element) NElement

func (el *Element) NElement(selector string) (any, error)

NElement returns the first child that matches the css selector

func (*Element) Next

func (el *Element) Next() any

Next returns the next sibling element in the DOM tree

func (*Element) Page

func (el *Element) Page() any

Page of the element

func (*Element) Parent

func (el *Element) Parent() any

Parent returns the parent element in the DOM tree

func (*Element) Parents

func (el *Element) Parents(selector string) any

Parents that match the selector

func (*Element) Previous

func (el *Element) Previous() any

Previous returns the previous sibling element in the DOM tree

func (*Element) Resource

func (el *Element) Resource(_ sobek.FunctionCall, vm *sobek.Runtime) (ret sobek.Value)

Resource returns the "src" content of current element. Such as the jpg of <img src="a.jpg">

func (*Element) Screenshot

func (el *Element) Screenshot(call sobek.FunctionCall, vm *sobek.Runtime) (ret sobek.Value)

Screenshot of the area of the element

func (*Element) ShadowRoot

func (el *Element) ShadowRoot() any

ShadowRoot returns the shadow root of this element

func (*Element) Shape

func (el *Element) Shape(_ sobek.FunctionCall, vm *sobek.Runtime) (ret sobek.Value)

Shape of the DOM element content. The shape is a group of 4-sides polygons. A 4-sides polygon is not necessary a rectangle. 4-sides polygons can be apart from each other. For example, we use 2 4-sides polygons to describe the shape below:

  ____________          ____________
 /        ___/    =    /___________/    +     _________
/________/                                   /________/

func (*Element) Wait

func (el *Element) Wait(call sobek.FunctionCall, vm *sobek.Runtime) (ret sobek.Value)

Wait until the js returns true

func (*Element) WaitInteractable

func (el *Element) WaitInteractable(_ sobek.FunctionCall, vm *sobek.Runtime) (ret sobek.Value)

WaitInteractable waits for the element to be interactable. It will try to scroll to the element on each try.

type Elements

type Elements []map[string]any

Elements provides some helpers to deal with element list

func NewElements

func NewElements(elements rod.Elements) Elements

NewElements creates a new Elements mapping

func (Elements) Empty

func (els Elements) Empty() bool

Empty returns true if the list is empty

func (Elements) First

func (els Elements) First() any

First returns the first element, if the list is empty returns nil

func (Elements) Last

func (els Elements) Last() any

Last returns the last element, if the list is empty returns nil

type EvalOptions

type EvalOptions struct {
	ByValue      bool                       `json:"byValue"`
	AwaitPromise bool                       `json:"awaitPromise"`
	ThisObj      *proto.RuntimeRemoteObject `json:"thisObj"`
	JS           string                     `json:"js"`
	JSArgs       []any                      `json:"jsArgs"`
	UserGesture  bool                       `json:"userGesture"`
}

EvalOptions for Page.Evaluate

type Page

type Page struct {
	*rod.Page
}

Page the rod.Page mapping

func (*Page) Activate

func (p *Page) Activate() (any, error)

Activate (focuses) the page

func (*Page) Cookies

func (p *Page) Cookies(call sobek.FunctionCall, vm *sobek.Runtime) (ret sobek.Value)

Cookies returns the page cookies. By default it will return the cookies for current page. The urls is the list of URLs for which applicable cookies will be fetched.

func (*Page) Element

func (p *Page) Element(selector string) (any, error)

Element retries until an element in the page that matches the CSS selector, then returns the matched element.

func (*Page) ElementByJS

func (p *Page) ElementByJS(call sobek.FunctionCall, vm *sobek.Runtime) (ret sobek.Value)

ElementByJS returns the element from the return value of the js function. If sleeper is nil, no retry will be performed. By default, it will retry until the js function doesn't return null. To customize the retry logic, check the examples of Page.Sleeper.

func (*Page) ElementFromNode

func (p *Page) ElementFromNode(call sobek.FunctionCall, vm *sobek.Runtime) (ret sobek.Value)

ElementFromNode creates an Element from the node, NodeID or BackendNodeID must be specified.

func (*Page) ElementFromObject

func (p *Page) ElementFromObject(call sobek.FunctionCall, vm *sobek.Runtime) (ret sobek.Value)

ElementFromObject creates an Element from the remote object id.

func (*Page) ElementFromPoint

func (p *Page) ElementFromPoint(x, y int) (any, error)

ElementFromPoint creates an Element from the absolute point on the page. The point should include the window scroll offset.

func (*Page) ElementR

func (p *Page) ElementR(selector, jsRegex string) (any, error)

ElementR retries until an element in the page that matches the css selector and it's text matches the jsRegex, then returns the matched element.

func (*Page) ElementX

func (p *Page) ElementX(xPath string) (any, error)

ElementX retries until an element in the page that matches one of the XPath selectors, then returns the matched element.

func (*Page) Elements

func (p *Page) Elements(selector string) (Elements, error)

Elements returns all elements that match the css selector

func (*Page) ElementsByJS

func (p *Page) ElementsByJS(call sobek.FunctionCall, vm *sobek.Runtime) (ret sobek.Value)

ElementsByJS returns the elements from the return value of the js

func (*Page) ElementsX

func (p *Page) ElementsX(xpath string) (Elements, error)

ElementsX returns all elements that match the XPath selector

func (*Page) Emulate

func (p *Page) Emulate(call sobek.FunctionCall, vm *sobek.Runtime) (ret sobek.Value)

Emulate the device, such as iPhone9. If device is devices.Clear, it will clear the override.

func (*Page) Eval

func (p *Page) Eval(call sobek.FunctionCall, vm *sobek.Runtime) (ret sobek.Value)

Eval is a shortcut for Page.Evaluate with AwaitPromise, ByValue set to true.

func (*Page) Evaluate

func (p *Page) Evaluate(call sobek.FunctionCall, vm *sobek.Runtime) (ret sobek.Value)

Evaluate js on the page.

func (*Page) GetResource

func (p *Page) GetResource(call sobek.FunctionCall, vm *sobek.Runtime) (ret sobek.Value)

GetResource content by the url. Such as image, css, html, etc. Use the proto.PageGetResourceTree to list all the resources.

func (*Page) GetWindow

func (p *Page) GetWindow(_ sobek.FunctionCall, vm *sobek.Runtime) (ret sobek.Value)

GetWindow position and size info

func (*Page) HandleDialog

func (p *Page) HandleDialog(_ sobek.FunctionCall, vm *sobek.Runtime) (ret sobek.Value)

HandleDialog accepts or dismisses next JavaScript initiated dialog (alert, confirm, prompt, or onbeforeunload). Because modal dialog will block js, usually you have to trigger the dialog in another goroutine. For example:

const { wait, handle } = page.handleDialog()
page.element("button").click()
wait()
handle(true, "")

func (*Page) Has

func (p *Page) Has(selector string) (bool, any, error)

Has an element that matches the css selector

func (*Page) HasR

func (p *Page) HasR(selector, jsRegex string) (bool, any, error)

HasR an element that matches the css selector and its display text matches the jsRegex.

func (*Page) HasX

func (p *Page) HasX(selector string) (bool, any, error)

HasX an element that matches the XPath selector

func (*Page) Info

func (p *Page) Info(_ sobek.FunctionCall, vm *sobek.Runtime) (ret sobek.Value)

Info of the page, such as the URL or title of the page

func (*Page) ObjectToJSON

func (p *Page) ObjectToJSON(call sobek.FunctionCall, vm *sobek.Runtime) (ret sobek.Value)

ObjectToJSON by object id

func (*Page) PDF

func (p *Page) PDF(call sobek.FunctionCall, vm *sobek.Runtime) (ret sobek.Value)

PDF prints page as PDF

func (*Page) Release

func (p *Page) Release(call sobek.FunctionCall, vm *sobek.Runtime) (ret sobek.Value)

Release the remote object. Usually, you don't need to call it. When a page is closed or reloaded, all remote objects will be released automatically. It's useful if the page never closes or reloads.

func (*Page) Screenshot

func (p *Page) Screenshot(call sobek.FunctionCall, vm *sobek.Runtime) (ret sobek.Value)

Screenshot captures the screenshot of current page.

func (*Page) Search

func (p *Page) Search(query string) (any, error)

Search for the given query in the DOM tree until the result count is not zero, before that it will keep retrying. The query can be plain text or css selector or xpath. It will search nested iframes and shadow doms too.

func (*Page) SetCookies

func (p *Page) SetCookies(call sobek.FunctionCall, vm *sobek.Runtime) (ret sobek.Value)

SetCookies is similar to Browser.SetCookies .

func (*Page) SetUserAgent

func (p *Page) SetUserAgent(call sobek.FunctionCall, vm *sobek.Runtime) (ret sobek.Value)

SetUserAgent (browser brand, accept-language, etc) of the page. If req is nil, a default user agent will be used, a typical mac chrome.

func (*Page) SetViewport

func (p *Page) SetViewport(call sobek.FunctionCall, vm *sobek.Runtime) (ret sobek.Value)

SetViewport overrides the values of device screen dimensions

func (*Page) SetWindow

func (p *Page) SetWindow(call sobek.FunctionCall, vm *sobek.Runtime) (ret sobek.Value)

SetWindow location and size

func (*Page) Timeout

func (p *Page) Timeout(timeout string) (page *Page, err error)

Timeout returns a clone with the specified total timeout of all chained sub-operations

func (*Page) Wait

func (p *Page) Wait(call sobek.FunctionCall, vm *sobek.Runtime) (ret sobek.Value)

Wait until the js returns true

func (*Page) WaitIdle

func (p *Page) WaitIdle(timeout string) (err error)

WaitIdle waits until the next window.requestIdleCallback is called.

func (*Page) WaitOpen

func (p *Page) WaitOpen(_ sobek.FunctionCall, vm *sobek.Runtime) (ret sobek.Value)

WaitOpen waits for the next new page opened by the current one

Jump to

Keyboard shortcuts

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