browser

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2023 License: AGPL-3.0 Imports: 11 Imported by: 0

README

Browser

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

License

cloudcat is distributed under the AGPL-3.0 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 *goja.Runtime) goja.Value

NewElement creates a new Element mapping

func NewPage

func NewPage(p *rod.Page, vm *goja.Runtime) goja.Value

NewPage creates a new Page mapping

Types

type Browser

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

Browser module represents the browser. It doesn't depends on file system, it should work with remote browser seamlessly.

func (*Browser) Page

func (b *Browser) Page(call goja.FunctionCall, vm *goja.Runtime) goja.Value

Page returns a new page

type Element

type Element struct {
	*rod.Element
}

Element represents the DOM element

func (*Element) BackgroundImage

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

BackgroundImage returns the css background-image of the element

func (*Element) ContainsElement

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

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

func (*Element) Describe

func (el *Element) Describe(call goja.FunctionCall, vm *goja.Runtime) (ret goja.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 goja.FunctionCall, vm *goja.Runtime) (ret goja.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 goja.FunctionCall, vm *goja.Runtime) (ret goja.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 goja.Value) (bool, error)

Equal checks if the two elements are equal.

func (*Element) Eval

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

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

func (*Element) Evaluate

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

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

func (*Element) Frame

func (el *Element) Frame(_ goja.FunctionCall, vm *goja.Runtime) (ret goja.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(_ goja.FunctionCall, vm *goja.Runtime) (ret goja.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(_ goja.FunctionCall, vm *goja.Runtime) (ret goja.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 goja.FunctionCall, vm *goja.Runtime) (ret goja.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(_ goja.FunctionCall, vm *goja.Runtime) (ret goja.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 goja.FunctionCall, vm *goja.Runtime) (ret goja.Value)

Wait until the js returns true

func (*Element) WaitInteractable

func (el *Element) WaitInteractable(_ goja.FunctionCall, vm *goja.Runtime) (ret goja.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 Module

type Module struct{}

Module js module

func (*Module) Exports

func (*Module) Exports() any

Exports returns module instance

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 goja.FunctionCall, vm *goja.Runtime) (ret goja.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 goja.FunctionCall, vm *goja.Runtime) (ret goja.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 goja.FunctionCall, vm *goja.Runtime) (ret goja.Value)

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

func (*Page) ElementFromObject

func (p *Page) ElementFromObject(call goja.FunctionCall, vm *goja.Runtime) (ret goja.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 goja.FunctionCall, vm *goja.Runtime) (ret goja.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 goja.FunctionCall, vm *goja.Runtime) (ret goja.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 goja.FunctionCall, vm *goja.Runtime) (ret goja.Value)

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

func (*Page) Evaluate

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

Evaluate js on the page.

func (*Page) GetResource

func (p *Page) GetResource(call goja.FunctionCall, vm *goja.Runtime) (ret goja.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(_ goja.FunctionCall, vm *goja.Runtime) (ret goja.Value)

GetWindow position and size info

func (*Page) HandleDialog

func (p *Page) HandleDialog(_ goja.FunctionCall, vm *goja.Runtime) (ret goja.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(_ goja.FunctionCall, vm *goja.Runtime) (ret goja.Value)

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

func (*Page) ObjectToJSON

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

ObjectToJSON by object id

func (*Page) PDF

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

PDF prints page as PDF

func (*Page) Release

func (p *Page) Release(call goja.FunctionCall, vm *goja.Runtime) (ret goja.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 goja.FunctionCall, vm *goja.Runtime) (ret goja.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 goja.FunctionCall, vm *goja.Runtime) (ret goja.Value)

SetCookies is similar to Browser.SetCookies .

func (*Page) SetUserAgent

func (p *Page) SetUserAgent(call goja.FunctionCall, vm *goja.Runtime) (ret goja.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 goja.FunctionCall, vm *goja.Runtime) (ret goja.Value)

SetViewport overrides the values of device screen dimensions

func (*Page) SetWindow

func (p *Page) SetWindow(call goja.FunctionCall, vm *goja.Runtime) (ret goja.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 goja.FunctionCall, vm *goja.Runtime) (ret goja.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(_ goja.FunctionCall, vm *goja.Runtime) (ret goja.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