Documentation
¶
Overview ¶
Package simple @Author bcy2007 2024/11/11 15:16
Index ¶
- type BrowserConfig
- type BrowserConfigOpt
- func WithExePath(exePath string) BrowserConfigOpt
- func WithFromPlugin(fromPlugin string) BrowserConfigOpt
- func WithHeadless(headless bool) BrowserConfigOpt
- func WithHijack(hijack bool) BrowserConfigOpt
- func WithLeakless(leakless bool) BrowserConfigOpt
- func WithNoSandBox(noSandBox bool) BrowserConfigOpt
- func WithProxy(proxyAddress string, proxyUserInfo ...string) BrowserConfigOpt
- func WithRequestModification(modifyUrl string, modifyTarget ModifyTarget, modifyResult interface{}) BrowserConfigOpt
- func WithResponseModification(modifyUrl string, modifyTarget ModifyTarget, modifyResult interface{}) BrowserConfigOpt
- func WithRuntimeID(runtimeID string) BrowserConfigOpt
- func WithSaveToDB(saveToDB bool) BrowserConfigOpt
- func WithSourceType(sourceType string) BrowserConfigOpt
- func WithTimeout(timeout int) BrowserConfigOpt
- func WithWsAddress(wsAddress string) BrowserConfigOpt
- type ModifyTarget
- type RequestModification
- type ResponseModification
- type VBrowser
- type VElement
- func (element *VElement) Attribute(name string) (string, error)
- func (element *VElement) Click() error
- func (element *VElement) Elements(selector string) (VElements, error)
- func (element *VElement) HTML() (string, error)
- func (element *VElement) Input(info string) error
- func (element *VElement) Text() (string, error)
- type VElements
- type VPage
- func (page *VPage) Click(selector string) error
- func (page *VPage) Close() error
- func (page *VPage) Element(selector string) (*VElement, error)
- func (page *VPage) Elements(selector string) (VElements, error)
- func (page *VPage) HTML() (string, error)
- func (page *VPage) Input(selector, inputStr string) error
- func (page *VPage) Navigate(urlStr string, waitFor string) error
- func (page *VPage) ScreenShot() (string, error)
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) Modify ¶
func (modification *RequestModification) Modify(request *rod.HijackRequest) error
type ResponseModification ¶
type ResponseModification struct {
// contains filtered or unexported fields
}
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) Navigate ¶
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 ¶
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 ¶
Click 点击该网页元素
Example:
```
page, _ = browser.Navigate("https://example.com", "")
element, _ = page.Element("#pageName")
element.Click() // 点击id为pageName的元素
```
func (*VElement) Elements ¶
Elements 返回该网页元素下的所有匹配css selector的子元素
Example:
```
page, _ = browser.Navigate("https://example.com", "")
element, _ = page.Element("#test")
elements, _ = element.Elements("#id")
```
func (*VElement) HTML ¶
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 ¶
Input 激活网页元素并输入内容
注意如果该元素不可输入或隐藏可能会卡死
```
Example:
page, _ = browser.Navigate("https://example.com", "")
element, _ = page.Element("#pageName")
element.Input("hello") // 在id为pageName的元素中输入hello
```
type VPage ¶
type VPage struct {
// contains filtered or unexported fields
}
func (*VPage) Element ¶
Element 返回页面中匹配css selector的一个元素
Example:
```
page, _ = browser.Navigate("https://example.com", "")
element, _ = page.Element("#pageName") // 匹配id为pageName的元素
```
func (*VPage) Elements ¶
Elements 返回页面中匹配css selector的所有元素
Example:
```
page, _ = browser.Navigate("https://example.com", "")
elements, _ = page.Element("p") // 匹配所有p标签元素
```
func (*VPage) HTML ¶
HTML 返回整个页面的html内容
Example:
```
page, _ = browser.Navigate("https://example.com", "")
html, _ = page.HTML()
```