wee

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2024 License: GPL-3.0 Imports: 31 Imported by: 3

README

wee

refer

Faq

panic: context canceled

context canceled issue usually happend when elem is bound with Timeout on bot.Elem(xxx), you can use

elem.CancelTimeout().Timeout(xxx) to rewrite previous value.

Documentation

Index

Constants

View Source
const (
	SEP       = "@@@"
	IFrameSep = "$$$"
)
View Source
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
)
View Source
const (
	LongScrollStep     = 32
	MediumScrollStep   = 16
	ShortScrollStep    = 8
	QuickScrollStep    = 4
	DirectlyScrollStep = 1
)
View Source
const (
	// PT10MilliSec a very short timeout in millisecond
	PT10MilliSec = 0.01
)
View Source
const PaintRects = "--show-paint-rects"

Variables

View Source
var (
	ErrCannotActivateOpenedPage = errors.New("cannot activate latest opened page")
	ErrMissingCookieFile        = errors.New("missing cookie file")
)
View Source
var (
	ErrEmptySelector  = errors.New("empty selector")
	ErrGetElemsByText = errors.New("get elems not support by text")
	CannotFindElem    = errors.New("cannot find elem of selector")
)
View Source
var ErrElemShapeBox = errors.New("cannot get element box by shape.Box()")
View Source
var ErrSelectorEmpty = errors.New("selector is empty")

Functions

func BindBotLanucher

func BindBotLanucher(bot *Bot, options ...BotOption)

BindBotLanucher launches browser and page for bot.

func Blocked

func Blocked()

func Confirm

func Confirm(msg ...string) bool

func ErrCannotFindSelector

func ErrCannotFindSelector(sel string) error

func Filenamify

func Filenamify(name string) string

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 IsJSON

func IsJSON(str string) bool

func IsZeroVal

func IsZeroVal(x interface{}) bool

IsZeroVal check if any type is its zero value

func MustStrToFloat

func MustStrToFloat(raw string, keptChars string) float64

func MustStringify

func MustStringify(data interface{}) string

func MustWriteFile

func MustWriteFile(filepath string, content []byte)

func NewBrowser

func NewBrowser(opts ...BrowserOptionFunc) (*launcher.Launcher, *rod.Browser)

func NewChromeExtension

func NewChromeExtension(line, savePath string) (string, string, string)

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

func NewStringSlice(raw string, fixStep int, randomStep ...bool) []string

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 NewUserMode

func NewUserMode() (*launcher.Launcher, *rod.Browser)

func QuitIfY

func QuitIfY()

func QuitOnTimeout

func QuitOnTimeout(args ...int)

func RandFloatX1k

func RandFloatX1k(min, max float64) int

RandFloatX1k returns a value between (min, max) * 1000

func RandSleep

func RandSleep(min, max float64, msg ...string) int

RandSleep random sleep some seconds

@returns milliseconds

func SleepWithSpin

func SleepWithSpin(timeInSeconds int, args ...string)

func StrAorB

func StrAorB(a, b string) string

StrAorB returns the first non empty string.

func StrToNumChars

func StrToNumChars(raw string, keptChars string) string

func Stringify

func Stringify(data interface{}) (string, error)

Stringify returns a string representation

func StripIllegalChars

func StripIllegalChars(name string) string

Types

type Bot

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

func NewBot

func NewBot(options ...BotOption) *Bot

func NewBotDefault

func NewBotDefault(options ...BotOption) *Bot

func NewBotForDebug

func NewBotForDebug(options ...BotOption) *Bot

func NewBotUserMode

func NewBotUserMode(options ...BotOption) *Bot

func NewBotWithOptionsOnly

func NewBotWithOptionsOnly(options ...BotOption) *Bot

func (*Bot) ActivateLatestOpenedPage

func (b *Bot) ActivateLatestOpenedPage(pagesBefore rod.Pages, retry int) error

func (*Bot) ActivatePageByURLRegex

func (b *Bot) ActivatePageByURLRegex(jsRegex string, retry int) error

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 (b *Bot) AllElementsAttrMap(elems []*rod.Element, opts ...ElemOptionFunc) []map[string]string

func (*Bot) AnyElem

func (b *Bot) AnyElem(selectors ...string) (string, error)

func (*Bot) AnyElemWithTimeout

func (b *Bot) AnyElemWithTimeout(selectors []string, opts ...ElemOptionFunc) (string, error)

func (*Bot) Browser

func (b *Bot) Browser() *rod.Browser

func (*Bot) Cleanup

func (b *Bot) Cleanup()

func (*Bot) Click

func (b *Bot) Click(selector string, opts ...ElemOptionFunc) error

func (*Bot) ClickElem

func (b *Bot) ClickElem(elem *rod.Element, opts ...ElemOptionFunc) 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 (b *Bot) CloseIfHasPopovers(popovers ...string) int

func (*Bot) ClosePopover

func (b *Bot) ClosePopover(sel string) (int, error)

func (*Bot) CurrentUrl

func (b *Bot) CurrentUrl() string

func (*Bot) CustomizePage

func (b *Bot) CustomizePage()

func (*Bot) DisableImages

func (b *Bot) DisableImages()

func (*Bot) DumpCookies

func (b *Bot) DumpCookies() (string, error)

func (*Bot) DumpXHR

func (b *Bot) DumpXHR(ptn []string, handler func(h *rod.Hijack))

func (*Bot) Elem

func (b *Bot) Elem(selector string, opts ...ElemOptionFunc) (*rod.Element, error)

Elem get element by selector.

func (*Bot) ElemAttr

func (b *Bot) ElemAttr(selector string, opts ...ElemOptionFunc) (string, error)

func (*Bot) ElemByIndex

func (b *Bot) ElemByIndex(selector string, index int) (*rod.Element, error)

ElemByIndex get elem without wait.

func (*Bot) ElemByText

func (b *Bot) ElemByText(selector string, opts ...ElemOptionFunc) (*rod.Element, error)

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 (b *Bot) ElementAttr(elem *rod.Element, opts ...ElemOptionFunc) (string, error)

func (*Bot) ElementAttrMap

func (b *Bot) ElementAttrMap(elem *rod.Element, opts ...ElemOptionFunc) map[string]string

func (*Bot) Elems

func (b *Bot) Elems(selector string, opts ...ElemOptionFunc) ([]*rod.Element, error)

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 (b *Bot) ElemsByText(selector string, opts ...ElemOptionFunc) ([]*rod.Element, error)

func (*Bot) EnsureInteractable

func (b *Bot) EnsureInteractable(elem *rod.Element, byEsc bool) error

func (*Bot) FocusAndHighlight

func (b *Bot) FocusAndHighlight(elem *rod.Element)

func (*Bot) FromCookieStr

func (b *Bot) FromCookieStr()

func (*Bot) GetElemBox

func (b *Bot) GetElemBox(elem *rod.Element) (*proto.DOMRect, error)

func (*Bot) GetScrollHeight

func (b *Bot) GetScrollHeight() (float64, error)

func (*Bot) GetWindowInnerHeight

func (b *Bot) GetWindowInnerHeight() float64

func (*Bot) Highlight

func (b *Bot) Highlight(elem *rod.Element, show, hide float64, style string, count int) float64

func (*Bot) HighlightElem

func (b *Bot) HighlightElem(elem *rod.Element)

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 (b *Bot) IframeElem(iframe, selector string, opts ...ElemOptionFunc) (*rod.Element, error)

func (*Bot) Init

func (b *Bot) Init()

Init init all attributes not exposed with options

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) Interactable

func (b *Bot) Interactable(elem *rod.Element) error

func (*Bot) LoadCookies

func (b *Bot) LoadCookies(filepath string) error

func (*Bot) MarkElem

func (b *Bot) MarkElem(elem *rod.Element, timeout time.Duration, style string)

func (*Bot) MarkElems

func (b *Bot) MarkElems(timeout time.Duration, elems ...*rod.Element)

func (*Bot) MiscellanyBackup

func (b *Bot) MiscellanyBackup() error

func (*Bot) MustAcceptCookies

func (b *Bot) MustAcceptCookies(cookies ...string)

func (*Bot) MustAllElemsAttrs

func (b *Bot) MustAllElemsAttrs(selector string, opts ...ElemOptionFunc) []string

func (*Bot) MustAnyElem

func (b *Bot) MustAnyElem(selectors ...string) string

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 (b *Bot) MustClickOneByOne(selectors ...string)

func (*Bot) MustElem

func (b *Bot) MustElem(selector string, opts ...ElemOptionFunc) *rod.Element

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

func (b *Bot) MustEval(script string) string

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) MustInput

func (b *Bot) MustInput(sel, text string, opts ...ElemOptionFunc) string

func (*Bot) MustOpen

func (b *Bot) MustOpen(uri string)

func (*Bot) MustPressEscape

func (b *Bot) MustPressEscape(elem *rod.Element)

func (*Bot) MustScrollToBottom

func (b *Bot) MustScrollToBottom(opts ...ElemOptionFunc)

func (*Bot) MustScrollToTop

func (b *Bot) MustScrollToTop()

func (*Bot) MustScrollToXY

func (b *Bot) MustScrollToXY(x, y float64)

func (*Bot) MustStable

func (b *Bot) MustStable()

func (*Bot) MustUpdatePage

func (b *Bot) MustUpdatePage(page *rod.Page)

func (*Bot) MustWaitLoad

func (b *Bot) MustWaitLoad()

func (*Bot) NotNilElem

func (b *Bot) NotNilElem(sel string, opts []ElemOptionFunc) (*rod.Element, error)

func (*Bot) Open

func (b *Bot) Open(url string, timeouts ...time.Duration) error

func (*Bot) OpenWithCookies

func (b *Bot) OpenWithCookies(uri string) error

OpenWithCookies open uri with cookies.

typically with following steps:

func (*Bot) Page

func (b *Bot) Page() *rod.Page

func (*Bot) PageSource

func (b *Bot) PageSource() string

func (*Bot) Press

func (b *Bot) Press(elem *rod.Element, keys ...input.Key) error

func (*Bot) PressEscape

func (b *Bot) PressEscape(elem *rod.Element) error

func (*Bot) ResetToOriginalPage

func (b *Bot) ResetToOriginalPage() error

func (*Bot) Scroll

func (b *Bot) Scroll(x, y float64, step int) error

Scroll Scroll with mouse.

func (*Bot) ScrollLikeHuman

func (b *Bot) ScrollLikeHuman(offsetX, offsetY float64, opts ...ElemOptionFunc) error

func (*Bot) ScrollToElemDirectly

func (b *Bot) ScrollToElemDirectly(elem *rod.Element) 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) TypeCharsOneByOne

func (b *Bot) TypeCharsOneByOne(elem *rod.Element, value string)

func (*Bot) URLContains

func (b *Bot) URLContains(str string, timeouts ...float64) error

URLContains uses `decodeURIComponent(window.location.href).includes` to check if url has str or not

func (*Bot) UpdatePage

func (b *Bot) UpdatePage(page *rod.Page) error

type BotOption

type BotOption func(*Bot)

func Headless

func Headless(b bool) BotOption

func WithAcceptLanguage

func WithAcceptLanguage(s string) BotOption

func WithBrowser

func WithBrowser(brw *rod.Browser) BotOption

func WithCookieFile

func WithCookieFile(s string) BotOption

func WithCookies

func WithCookies(b bool) BotOption

func WithHighlightTimes

func WithHighlightTimes(i int) BotOption

func WithLauncher

func WithLauncher(l *launcher.Launcher) BotOption

func WithPage

func WithPage(b bool) BotOption

func WithPanicBy

func WithPanicBy(i PanicByType) BotOption

func WithPopovers

func WithPopovers(popovers ...string) BotOption

WithPopovers is useful when popovers appear randomly.

func WithUserAgent

func WithUserAgent(s string) BotOption

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

type HijackHandler func(*rod.Hijack)

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
}

Directories

Path Synopsis
examples
basic command
blocket command
books command
express command
hijack command
jandan command
modal command
nfcloud command
qiyueba command
usermode command

Jump to

Keyboard shortcuts

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