simple

package
v1.4.4-alpha1202-diff-... Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2025 License: AGPL-3.0 Imports: 14 Imported by: 1

Documentation

Overview

Package simple @Author bcy2007 2024/11/11 15:16

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BrowserConfig

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

type BrowserConfigOpt

type BrowserConfigOpt func(*BrowserConfig)

func WithExePath

func WithExePath(exePath string) BrowserConfigOpt

simulator.simple.exePath 是一个请求选项 用于输入浏览器的websocket地址

Example:
```
exePath = simulator.simple.exePath("/Applications/Google Chrome.app/Contents/MacOS/Google Chrome")
browser = simulator.simple.createBrowser(exePath)
```

func WithFromPlugin

func WithFromPlugin(fromPlugin string) BrowserConfigOpt

func WithHeadless

func WithHeadless(headless bool) BrowserConfigOpt

simulator.simple.headless 是一个请求选项 用于开启关闭headless模式

Example:
```
headless = simulator.simple.headless(true)
browser = simulator.simple.createBrowser(headless)
```

func WithHijack

func WithHijack(hijack bool) BrowserConfigOpt

simulator.simple.hijack 是一个请求选项 用于开启流量劫持模式

Example:
```
hijack = simulator.simple.hijack(true)
browser = simulator.simple.createBrowser(hijack)
```

func WithLeakless

func WithLeakless(leakless bool) BrowserConfigOpt

simulator.simple.leakless 是一个请求选项 用于设置在程序运行结束时强行杀死浏览器 注意在windows上可能会报毒,windows建议关闭

Example:
```
leakless = simulator.simple.leakless(true)
browser = simulator.simple.createBrowser(leakless)
```

func WithNoSandBox

func WithNoSandBox(noSandBox bool) BrowserConfigOpt

simulator.simple.noSandBox 是一个请求选项 用于开启/关闭sandbox

Example:
```
sandBox = simulator.simple.noSandBox(true)
browser = simulator.simple.createBrowser(sandBox)
```

func WithProxy

func WithProxy(proxyAddress string, proxyUserInfo ...string) BrowserConfigOpt

simulator.simple.proxy 是一个请求选项 用于输入代理服务器地址

Example:
```
proxy = simulator.simple.proxy("http://127.0.0.1:7890")
browser = simulator.simple.createBrowser(proxy)
```

func WithRequestModification

func WithRequestModification(modifyUrl string, modifyTarget ModifyTarget, modifyResult interface{}) BrowserConfigOpt

func WithResponseModification

func WithResponseModification(modifyUrl string, modifyTarget ModifyTarget, modifyResult interface{}) BrowserConfigOpt

func WithRuntimeID

func WithRuntimeID(runtimeID string) BrowserConfigOpt

func WithSaveToDB

func WithSaveToDB(saveToDB bool) BrowserConfigOpt

func WithSourceType

func WithSourceType(sourceType string) BrowserConfigOpt

func WithTimeout

func WithTimeout(timeout int) BrowserConfigOpt

simulator.simple.timeout 是一个请求选项 用于设置页面最大加载时间 单位秒

Example:
```
timeout = simulator.simple.timeout(30)
browser = simulator.simple.createBrowser(timeout)
```

func WithWsAddress

func WithWsAddress(wsAddress string) BrowserConfigOpt

simulator.simple.wsAddress 是一个请求选项 用于输入浏览器的websocket地址

Example:
```
proxy = simulator.simple.proxy("http://127.0.0.1:7890")
browser = simulator.simple.createBrowser(proxy)
```

type ModifyTarget

type ModifyTarget string
var BodyModifyTarget ModifyTarget = "body"
var BodyReplaceTarget ModifyTarget = "bodyReplace"
var HeadersModifyTarget ModifyTarget = "headers"
var HostModifyTarget ModifyTarget = "host"

type RequestModification

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

func (*RequestModification) Generate

func (modification *RequestModification) Generate() error

func (*RequestModification) GetReg

func (modification *RequestModification) GetReg() *regexp.Regexp

func (*RequestModification) Modify

func (modification *RequestModification) Modify(request *rod.HijackRequest) error

type ResponseModification

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

func (*ResponseModification) Generate

func (modification *ResponseModification) Generate() error

func (*ResponseModification) GetReg

func (modification *ResponseModification) GetReg() *regexp.Regexp

func (*ResponseModification) Modify

func (modification *ResponseModification) Modify(response *rod.HijackResponse) error

type VBrowser

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

func CreateHeadlessBrowser

func CreateHeadlessBrowser(opts ...BrowserConfigOpt) (*VBrowser, error)

simulator.simple.createBrowser / simulator.simpleCreateBrowser 浏览器手动操作模式 进行目标页面操作

 可以添加零个或多个请求选项,用于对此次请求进行配置

 返回值为浏览器 可以创建页面

Example:
```
proxy = simulator.simple.proxy("http://127.0.0.1:7890") // 代理地址修改
exePath = simulator.simple.exePath("/Applications/Google Chrome.app/Contents/MacOS/Google Chrome") // 浏览器路径修改
timeout = simulator.simple.timeout(20)
browser, err = simulator.simple.CreateBrowser(exePath, timeout, proxy)
if err != nil {
	return
}

```

func (*VBrowser) Close

func (browser *VBrowser) Close() error

Close 关闭浏览器

func (*VBrowser) Navigate

func (browser *VBrowser) Navigate(urlStr string, waitFor string) (*VPage, error)

Navigate 开启浏览器的一个页面 并跳转到对应url

其中第二个参数为 页面存在对应元素selector时即认为完成加载

 Example:
 ```
proxy = simulator.simple.proxy("http://127.0.0.1:7890") // 代理地址修改
exePath = simulator.simple.exePath("/Applications/Google Chrome.app/Contents/MacOS/Google Chrome") // 浏览器路径修改
timeout = simulator.simple.timeout(20)
browser, _ = simulator.simple.CreateBrowser(exePath, timeout, proxy)
selector = "#code"
// Navigate方法第二个参数不为空时 表示页面处于加载状态直到页面中出现css selector匹配到的元素后完成加载
page, err = browser.Navigate("https://example.com", selector)
// Navigate方法第二个参数为空时 页面正常通过document.readyState参数获取页面加载状态 完成加载等待
page, err = browser.Navigate("https://example.com", "")
if err != nil {
	return
}

```

type VElement

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

func (*VElement) Attribute

func (element *VElement) Attribute(name string) (string, error)

Attribute 返回一个网页元素的标签属性

Example:
```
page, _ = browser.Navigate("https://example.com", "")
element, _ = page.Element("#pageName") // 假设该元素为 <h2 id="pageName">welcome to our page</h2>
attribute, _ = element.Attribute("id") // 返回 pageName
```

func (*VElement) Click

func (element *VElement) Click() error

Click 点击该网页元素

Example:
```
page, _ = browser.Navigate("https://example.com", "")
element, _ = page.Element("#pageName")
element.Click() // 点击id为pageName的元素
```

func (*VElement) Elements

func (element *VElement) Elements(selector string) (VElements, error)

Elements 返回该网页元素下的所有匹配css selector的子元素

Example:
```
page, _ = browser.Navigate("https://example.com", "")
element, _ = page.Element("#test")
elements, _ = element.Elements("#id")
```

func (*VElement) HTML

func (element *VElement) HTML() (string, error)

HTML 返回一个网页元素的HTML内容

Example:
```
page, _ = browser.Navigate("https://example.com", "")
element, _ = page.Element("#pageName") // 假设该元素为 <h2 id="pageName">welcome to our page</h2>
html, _ = element.HTML() // 返回 <h2 id="pageName">welcome to our page</h2>
```

func (*VElement) Input

func (element *VElement) Input(info string) error

Input 激活网页元素并输入内容

注意如果该元素不可输入或隐藏可能会卡死

```
Example:
page, _ = browser.Navigate("https://example.com", "")
element, _ = page.Element("#pageName")
element.Input("hello") // 在id为pageName的元素中输入hello
```

func (*VElement) Text

func (element *VElement) Text() (string, error)

Text 返回一个网页元素所展示的文本内容

Example:
```
page, _ = browser.Navigate("https://example.com", "")
element, _ = page.Element("#pageName") // 假设该元素为 <h2 id="pageName">welcome to our page</h2>
text, _ = element.Text() // 返回 welcome to our page
```

type VElements

type VElements []*VElement

type VPage

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

func (*VPage) Click

func (page *VPage) Click(selector string) error

Click 点击页面中css selector匹配到的元素

同 element.Click()

func (*VPage) Close

func (page *VPage) Close() error

func (*VPage) Element

func (page *VPage) Element(selector string) (*VElement, error)

Element 返回页面中匹配css selector的一个元素

Example:
```
page, _ = browser.Navigate("https://example.com", "")
element, _ = page.Element("#pageName") // 匹配id为pageName的元素
```

func (*VPage) Elements

func (page *VPage) Elements(selector string) (VElements, error)

Elements 返回页面中匹配css selector的所有元素

Example:
```
page, _ = browser.Navigate("https://example.com", "")
elements, _ = page.Element("p") // 匹配所有p标签元素
```

func (*VPage) HTML

func (page *VPage) HTML() (string, error)

HTML 返回整个页面的html内容

Example:
```
page, _ = browser.Navigate("https://example.com", "")
html, _ = page.HTML()
```

func (*VPage) Input

func (page *VPage) Input(selector, inputStr string) error

Input 在页面中css selector匹配到的元素中输入内容

同 element.Input(string)

func (*VPage) Navigate

func (page *VPage) Navigate(urlStr string, waitFor string) error

func (*VPage) ScreenShot

func (page *VPage) ScreenShot() (string, error)

Jump to

Keyboard shortcuts

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