Documentation
¶
Index ¶
- Constants
- Variables
- func BindBotLanucher(bot *Bot, options ...BotOption)
- func Blocked()
- func Confirm(msg ...string) bool
- func ErrCannotFindSelector(sel string) error
- func Filenamify(name string) string
- func FirstOrDefault[T any](dft T, args ...T) T
- func IsJSON(str string) bool
- func IsZeroVal(x interface{}) bool
- func MustStrToFloat(raw string, keptChars string) float64
- func MustStringify(data interface{}) string
- func MustWriteFile(filepath string, content []byte)
- func NewBrowser(opts ...BrowserOptionFunc) (*launcher.Launcher, *rod.Browser)
- func NewChromeExtension(line, savePath string) (string, string, string)
- func NewLauncher(opts ...BrowserOptionFunc) *launcher.Launcher
- func NewStringSlice(raw string, fixStep int, randomStep ...bool) []string
- func NewUserMode() (*launcher.Launcher, *rod.Browser)
- func QuitIfY()
- func QuitOnTimeout(args ...int)
- func RandFloatX1k(min, max float64) int
- func RandSleep(min, max float64, msg ...string) int
- func SleepWithSpin(timeInSeconds int, args ...string)
- func StrAorB(a, b string) string
- func StrToNumChars(raw string, keptChars string) string
- func Stringify(data interface{}) (string, error)
- func StripIllegalChars(name string) string
- type Bot
- func (b *Bot) ActivateLatestOpenedPage(pagesBefore rod.Pages, retry int) error
- func (b *Bot) ActivatePageByURLRegex(jsRegex string, retry int) error
- func (b *Bot) AllAttrs(selector string, opts ...ElemOptionFunc) ([]string, error)
- func (b *Bot) AllElementsAttrMap(elems []*rod.Element, opts ...ElemOptionFunc) []map[string]string
- func (b *Bot) AnyElem(selectors ...string) (string, error)
- func (b *Bot) AnyElemWithTimeout(selectors []string, opts ...ElemOptionFunc) (string, error)
- func (b *Bot) Browser() *rod.Browser
- func (b *Bot) Cleanup()
- func (b *Bot) Click(selector string, opts ...ElemOptionFunc) error
- func (b *Bot) ClickElem(elem *rod.Element, opts ...ElemOptionFunc) error
- func (b *Bot) ClickElemWithScript(elem *rod.Element, opts ...ElemOptionFunc) error
- func (b *Bot) ClickWithScript(selector string, opts ...ElemOptionFunc) error
- func (b *Bot) CloseIfHasPopovers(popovers ...string) int
- func (b *Bot) ClosePopover(sel string) (int, error)
- func (b *Bot) CurrentUrl() string
- func (b *Bot) CustomizePage()
- func (b *Bot) DisableImages()
- func (b *Bot) DumpCookies() (string, error)
- func (b *Bot) DumpXHR(ptn []string, handler func(h *rod.Hijack))
- func (b *Bot) Elem(selector string, opts ...ElemOptionFunc) (*rod.Element, error)
- func (b *Bot) ElemAttr(selector string, opts ...ElemOptionFunc) (string, error)
- func (b *Bot) ElemByIndex(selector string, index int) (*rod.Element, error)
- func (b *Bot) ElemByText(selector string, opts ...ElemOptionFunc) (*rod.Element, error)
- func (b *Bot) ElementAttr(elem *rod.Element, opts ...ElemOptionFunc) (string, error)
- func (b *Bot) ElementAttrMap(elem *rod.Element, opts ...ElemOptionFunc) map[string]string
- func (b *Bot) Elems(selector string, opts ...ElemOptionFunc) ([]*rod.Element, error)
- func (b *Bot) ElemsByText(selector string, opts ...ElemOptionFunc) ([]*rod.Element, error)
- func (b *Bot) EnsureInteractable(elem *rod.Element, byEsc bool) error
- func (b *Bot) FocusAndHighlight(elem *rod.Element)
- func (b *Bot) FromCookieStr()
- func (b *Bot) GetElemBox(elem *rod.Element) (*proto.DOMRect, error)
- func (b *Bot) GetScrollHeight() (float64, error)
- func (b *Bot) GetWindowInnerHeight() float64
- func (b *Bot) Highlight(elem *rod.Element, show, hide float64, style string, count int) float64
- func (b *Bot) HighlightElem(elem *rod.Element)
- func (b *Bot) Hijack(patterns []string, networkResourceType proto.NetworkResourceType, ...)
- func (b *Bot) HijackAny(resources []string, terminate bool, handler HijackHandler)
- func (b *Bot) IframeElem(iframe, selector string, opts ...ElemOptionFunc) (*rod.Element, error)
- func (b *Bot) Init()
- func (b *Bot) Input(sel, text string, opts ...ElemOptionFunc) (string, error)
- func (b *Bot) InputSelect(sel, text string, opts ...ElemOptionFunc) error
- func (b *Bot) Interactable(elem *rod.Element) error
- func (b *Bot) LoadCookies(filepath string) error
- func (b *Bot) MarkElem(elem *rod.Element, timeout time.Duration, style string)
- func (b *Bot) MarkElems(timeout time.Duration, elems ...*rod.Element)
- func (b *Bot) MiscellanyBackup() error
- func (b *Bot) MustAcceptCookies(cookies ...string)
- func (b *Bot) MustAllElemsAttrs(selector string, opts ...ElemOptionFunc) []string
- func (b *Bot) MustAnyElem(selectors ...string) string
- func (b *Bot) MustClick(selector string, opts ...ElemOptionFunc)
- func (b *Bot) MustClickAll(selectors []string, opts ...ElemOptionFunc)
- func (b *Bot) MustClickAndWait(selector string, opts ...ElemOptionFunc)
- func (b *Bot) MustClickElem(elem *rod.Element, opts ...ElemOptionFunc)
- func (b *Bot) MustClickElemAndWait(elem *rod.Element, opts ...ElemOptionFunc)
- func (b *Bot) MustClickOneByOne(selectors ...string)
- func (b *Bot) MustElem(selector string, opts ...ElemOptionFunc) *rod.Element
- func (b *Bot) MustElemAttr(selector string, opts ...ElemOptionFunc) string
- func (b *Bot) MustElems(selector string, opts ...ElemOptionFunc) []*rod.Element
- func (b *Bot) MustElemsForAllSelectors(selectors []string, opts ...ElemOptionFunc) []*rod.Element
- func (b *Bot) MustEval(script string) string
- func (b *Bot) MustIframeElem(iframe, selector string, opts ...ElemOptionFunc) *rod.Element
- func (b *Bot) MustInput(sel, text string, opts ...ElemOptionFunc) string
- func (b *Bot) MustOpen(uri string)
- func (b *Bot) MustPressEscape(elem *rod.Element)
- func (b *Bot) MustScrollToBottom(opts ...ElemOptionFunc)
- func (b *Bot) MustScrollToTop()
- func (b *Bot) MustScrollToXY(x, y float64)
- func (b *Bot) MustStable()
- func (b *Bot) MustUpdatePage(page *rod.Page)
- func (b *Bot) MustWaitLoad()
- func (b *Bot) NotNilElem(sel string, opts []ElemOptionFunc) (*rod.Element, error)
- func (b *Bot) Open(url string, timeouts ...time.Duration) error
- func (b *Bot) OpenWithCookies(uri string) error
- func (b *Bot) Page() *rod.Page
- func (b *Bot) PageSource() string
- func (b *Bot) Press(elem *rod.Element, keys ...input.Key) error
- func (b *Bot) PressEscape(elem *rod.Element) error
- func (b *Bot) ResetToOriginalPage() error
- func (b *Bot) Scroll(x, y float64, step int) error
- func (b *Bot) ScrollLikeHuman(offsetX, offsetY float64, opts ...ElemOptionFunc) error
- func (b *Bot) ScrollToElemDirectly(elem *rod.Element) error
- func (b *Bot) ScrollToElement(elem *rod.Element, opts ...ElemOptionFunc)
- func (b *Bot) SetPanicWith(panicWith PanicByType)
- func (b *Bot) SetTimeout()
- func (b *Bot) TypeCharsOneByOne(elem *rod.Element, value string)
- func (b *Bot) URLContains(str string, timeouts ...float64) error
- func (b *Bot) UpdatePage(page *rod.Page) error
- type BotOption
- func Headless(b bool) BotOption
- func WithAcceptLanguage(s string) BotOption
- func WithBrowser(brw *rod.Browser) BotOption
- func WithCookieFile(s string) BotOption
- func WithCookies(b bool) BotOption
- func WithHighlightTimes(i int) BotOption
- func WithLauncher(l *launcher.Launcher) BotOption
- func WithPage(b bool) BotOption
- func WithPanicBy(i PanicByType) BotOption
- func WithPopovers(popovers ...string) BotOption
- func WithUserAgent(s string) BotOption
- type BrowserOptionFunc
- type BrowserOptions
- type ElemOptionFunc
- func DisableHandleCoverByEsc() ElemOptionFunc
- func WithAttr(s string) ElemOptionFunc
- func WithAttrMap(m map[string]string) ElemOptionFunc
- func WithCaseInsensitive(b bool) ElemOptionFunc
- func WithClickByScript(b bool) ElemOptionFunc
- func WithHighlight(b bool) ElemOptionFunc
- func WithHumanized(b bool) ElemOptionFunc
- func WithIframe(page *rod.Page) ElemOptionFunc
- func WithIndex(i int) ElemOptionFunc
- func WithRoot(root *rod.Element) ElemOptionFunc
- func WithScrollAsHuman(ah *ScrollAsHuman) ElemOptionFunc
- func WithSelectorType(t rod.SelectorType) ElemOptionFunc
- func WithSteps(i int) ElemOptionFunc
- func WithSubmit(b bool) ElemOptionFunc
- func WithTimeout(t float64) ElemOptionFunc
- func WithWaitStable(b bool) ElemOptionFunc
- type ElemOptions
- type HijackHandler
- type PanicByType
- type ScrollAsHuman
Constants ¶
const ( SEP = "@@@" IFrameSep = "$$$" )
const ( // ZeroToSec no timeout in second ZeroToSec = 0 // NapToSec a nap timeout in second NapToSec = 2 // ShortToSec short timeout in second ShortToSec = 5 // MediumToSec medium timeout in second MediumToSec = 20 // LongToSec long timeout in second LongToSec = 60 )
const ( LongScrollStep = 32 MediumScrollStep = 16 ShortScrollStep = 8 QuickScrollStep = 4 DirectlyScrollStep = 1 )
const (
// PT10MilliSec a very short timeout in millisecond
PT10MilliSec = 0.01
)
const PaintRects = "--show-paint-rects"
Variables ¶
var ( ErrCannotActivateOpenedPage = errors.New("cannot activate latest opened page") ErrMissingCookieFile = errors.New("missing cookie file") )
var ( ErrEmptySelector = errors.New("empty selector") ErrGetElemsByText = errors.New("get elems not support by text") CannotFindElem = errors.New("cannot find elem of selector") )
var ErrElemShapeBox = errors.New("cannot get element box by shape.Box()")
var ErrSelectorEmpty = errors.New("selector is empty")
Functions ¶
func BindBotLanucher ¶
BindBotLanucher launches browser and page for bot.
func ErrCannotFindSelector ¶
func Filenamify ¶
func FirstOrDefault ¶
func FirstOrDefault[T any](dft T, args ...T) T
FirstOrDefault
return the first args value, if args not empty else return default value
func MustStrToFloat ¶
func MustStringify ¶
func MustStringify(data interface{}) string
func MustWriteFile ¶
func NewBrowser ¶
func NewBrowser(opts ...BrowserOptionFunc) (*launcher.Launcher, *rod.Browser)
func NewChromeExtension ¶
NewChromeExtension will create two files from line, and save to savePath
- background.js
- manifest.json
line format is: "host:port:username:password:<OTHER>"
func NewLauncher ¶
func NewLauncher(opts ...BrowserOptionFunc) *launcher.Launcher
func NewStringSlice ¶
NewStringSlice
raw: the raw string to be convert to slice fixStep: how many chars in each slice args: just add any one to enable random step mode
return:
str slice with each has (~)maxLen chars
func QuitOnTimeout ¶
func QuitOnTimeout(args ...int)
func RandFloatX1k ¶
RandFloatX1k returns a value between (min, max) * 1000
func SleepWithSpin ¶
func StrToNumChars ¶
func StripIllegalChars ¶
Types ¶
type Bot ¶
type Bot struct {
// contains filtered or unexported fields
}
func NewBotDefault ¶
func NewBotForDebug ¶
func NewBotUserMode ¶
func NewBotWithOptionsOnly ¶
func (*Bot) ActivateLatestOpenedPage ¶
func (*Bot) ActivatePageByURLRegex ¶
ActivatePageByURLRegex
usage:
if v := regexStr; v != "" {
return c.activatePageByURLRegex(v)
}
func (*Bot) AllAttrs ¶
func (b *Bot) AllAttrs(selector string, opts ...ElemOptionFunc) ([]string, error)
func (*Bot) AllElementsAttrMap ¶
func (*Bot) AnyElemWithTimeout ¶
func (b *Bot) AnyElemWithTimeout(selectors []string, opts ...ElemOptionFunc) (string, error)
func (*Bot) ClickElemWithScript ¶
func (b *Bot) ClickElemWithScript(elem *rod.Element, opts ...ElemOptionFunc) error
func (*Bot) ClickWithScript ¶
func (b *Bot) ClickWithScript(selector string, opts ...ElemOptionFunc) error
func (*Bot) CloseIfHasPopovers ¶
func (*Bot) CurrentUrl ¶
func (*Bot) CustomizePage ¶
func (b *Bot) CustomizePage()
func (*Bot) DisableImages ¶
func (b *Bot) DisableImages()
func (*Bot) DumpCookies ¶
func (*Bot) ElemAttr ¶
func (b *Bot) ElemAttr(selector string, opts ...ElemOptionFunc) (string, error)
func (*Bot) ElemByIndex ¶
ElemByIndex get elem without wait.
func (*Bot) ElemByText ¶
ElemByText by text content.
Available layouts:
- when selector is like `div.abc@@@txt`, will use contains
- when selector is like `div.abc@@@---@@@txt`, will use exact match
func (*Bot) ElementAttr ¶
func (*Bot) ElementAttrMap ¶
func (*Bot) Elems ¶
Elems get all elements immediately.
WARN: when use Elems(MustElems), please ensure following conditions
- !!! selector cannot be empty
- ??? selector cannot contain SEP: `@@@`, which means shouldn't by Text
func (*Bot) ElemsByText ¶
func (*Bot) EnsureInteractable ¶
func (*Bot) FocusAndHighlight ¶
func (*Bot) FromCookieStr ¶
func (b *Bot) FromCookieStr()
func (*Bot) GetScrollHeight ¶
func (*Bot) GetWindowInnerHeight ¶
func (*Bot) HighlightElem ¶
func (*Bot) Hijack ¶
func (b *Bot) Hijack(patterns []string, networkResourceType proto.NetworkResourceType, handler HijackHandler, continueRequest bool)
func (*Bot) HijackAny ¶
func (b *Bot) HijackAny(resources []string, terminate bool, handler HijackHandler)
HijackAny
func (*Bot) IframeElem ¶
func (*Bot) Input ¶
func (b *Bot) Input(sel, text string, opts ...ElemOptionFunc) (string, error)
Input first clear all content, and then input text content.
func (*Bot) InputSelect ¶
func (b *Bot) InputSelect(sel, text string, opts ...ElemOptionFunc) error
func (*Bot) LoadCookies ¶
func (*Bot) MiscellanyBackup ¶
func (*Bot) MustAcceptCookies ¶
func (*Bot) MustAllElemsAttrs ¶
func (b *Bot) MustAllElemsAttrs(selector string, opts ...ElemOptionFunc) []string
func (*Bot) MustAnyElem ¶
func (*Bot) MustClick ¶
func (b *Bot) MustClick(selector string, opts ...ElemOptionFunc)
func (*Bot) MustClickAll ¶
func (b *Bot) MustClickAll(selectors []string, opts ...ElemOptionFunc)
func (*Bot) MustClickAndWait ¶
func (b *Bot) MustClickAndWait(selector string, opts ...ElemOptionFunc)
func (*Bot) MustClickElem ¶
func (b *Bot) MustClickElem(elem *rod.Element, opts ...ElemOptionFunc)
func (*Bot) MustClickElemAndWait ¶
func (b *Bot) MustClickElemAndWait(elem *rod.Element, opts ...ElemOptionFunc)
func (*Bot) MustClickOneByOne ¶
func (*Bot) MustElemAttr ¶
func (b *Bot) MustElemAttr(selector string, opts ...ElemOptionFunc) string
func (*Bot) MustElems ¶
func (b *Bot) MustElems(selector string, opts ...ElemOptionFunc) []*rod.Element
func (*Bot) MustElemsForAllSelectors ¶
func (b *Bot) MustElemsForAllSelectors(selectors []string, opts ...ElemOptionFunc) []*rod.Element
func (*Bot) MustEval ¶
MustEval a wrapper with MediumTo to rod.Page.MustEval
refer: https://github.com/go-rod/rod/blob/main/examples_test.go#L53
@param script `() => console.log("hello world")` or "`(a, b) => a + b`, 1, 2"
@return string
func (*Bot) MustIframeElem ¶
func (b *Bot) MustIframeElem(iframe, selector string, opts ...ElemOptionFunc) *rod.Element
func (*Bot) MustPressEscape ¶
func (*Bot) MustScrollToBottom ¶
func (b *Bot) MustScrollToBottom(opts ...ElemOptionFunc)
func (*Bot) MustScrollToTop ¶
func (b *Bot) MustScrollToTop()
func (*Bot) MustScrollToXY ¶
func (*Bot) MustStable ¶
func (b *Bot) MustStable()
func (*Bot) MustUpdatePage ¶
func (*Bot) MustWaitLoad ¶
func (b *Bot) MustWaitLoad()
func (*Bot) NotNilElem ¶
func (*Bot) OpenWithCookies ¶
OpenWithCookies open uri with cookies.
typically with following steps:
- open it's domain `https://xxx.com`
- load cookies
- open uri
func (*Bot) PageSource ¶
func (*Bot) ResetToOriginalPage ¶
func (*Bot) ScrollLikeHuman ¶
func (b *Bot) ScrollLikeHuman(offsetX, offsetY float64, opts ...ElemOptionFunc) error
func (*Bot) ScrollToElement ¶
func (b *Bot) ScrollToElement(elem *rod.Element, opts ...ElemOptionFunc)
func (*Bot) SetPanicWith ¶
func (b *Bot) SetPanicWith(panicWith PanicByType)
func (*Bot) SetTimeout ¶
func (b *Bot) SetTimeout()
func (*Bot) URLContains ¶
URLContains uses `decodeURIComponent(window.location.href).includes` to check if url has str or not
type BotOption ¶
type BotOption func(*Bot)
func WithAcceptLanguage ¶
func WithBrowser ¶
func WithCookieFile ¶
func WithCookies ¶
func WithHighlightTimes ¶
func WithLauncher ¶
func WithPanicBy ¶
func WithPanicBy(i PanicByType) BotOption
func WithPopovers ¶
WithPopovers is useful when popovers appear randomly.
func WithUserAgent ¶
type BrowserOptionFunc ¶
type BrowserOptionFunc func(o *BrowserOptions)
func WithBrowserHeadless ¶
func WithBrowserHeadless(b bool) BrowserOptionFunc
func WithFlags ¶
func WithFlags(arr ...string) BrowserOptionFunc
func WithPaintRects ¶
func WithPaintRects(b bool) BrowserOptionFunc
func WithSlowDelay ¶
func WithSlowDelay(i int) BrowserOptionFunc
type BrowserOptions ¶
type BrowserOptions struct {
// contains filtered or unexported fields
}
type ElemOptionFunc ¶
type ElemOptionFunc func(o *ElemOptions)
func DisableHandleCoverByEsc ¶
func DisableHandleCoverByEsc() ElemOptionFunc
func WithAttr ¶
func WithAttr(s string) ElemOptionFunc
func WithAttrMap ¶
func WithAttrMap(m map[string]string) ElemOptionFunc
func WithCaseInsensitive ¶
func WithCaseInsensitive(b bool) ElemOptionFunc
func WithClickByScript ¶
func WithClickByScript(b bool) ElemOptionFunc
func WithHighlight ¶
func WithHighlight(b bool) ElemOptionFunc
func WithHumanized ¶
func WithHumanized(b bool) ElemOptionFunc
func WithIframe ¶
func WithIframe(page *rod.Page) ElemOptionFunc
func WithIndex ¶
func WithIndex(i int) ElemOptionFunc
func WithRoot ¶
func WithRoot(root *rod.Element) ElemOptionFunc
func WithScrollAsHuman ¶
func WithScrollAsHuman(ah *ScrollAsHuman) ElemOptionFunc
func WithSelectorType ¶
func WithSelectorType(t rod.SelectorType) ElemOptionFunc
func WithSteps ¶
func WithSteps(i int) ElemOptionFunc
func WithSubmit ¶
func WithSubmit(b bool) ElemOptionFunc
func WithTimeout ¶
func WithTimeout(t float64) ElemOptionFunc
func WithWaitStable ¶
func WithWaitStable(b bool) ElemOptionFunc
type ElemOptions ¶
type ElemOptions struct {
// contains filtered or unexported fields
}
type HijackHandler ¶
HijackHandler hijacks network resources, you can disable hijacked resources by setting the return value to true.
type PanicByType ¶
type PanicByType int
const ( PanicByDft PanicByType = iota PanicByDump PanicByLogError )
type ScrollAsHuman ¶
type ScrollAsHuman struct {
// contains filtered or unexported fields
}