chrome

package
v0.0.0-...-9d44b34 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2024 License: GPL-3.0 Imports: 17 Imported by: 0

Documentation

Overview

本库仅支持Windows平台的所有使用Chrome内核的浏览器,线程安全的 Browser结构体用于操作浏览器

本库仅支持Windows平台的所有使用Chrome内核的浏览器,线程安全的 Tag结构体用于操作浏览器页面标签

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Browser

type Browser struct {
	Ip                   string   `json:"ip"`                       //调试IP地址,如果是本地填localhost或127.0.0.1,如果是外网则填外网IP,注意外网时不支持OpenBrowser方法打开浏览器
	Port                 int      `json:"remote-debugging-port"`    //调试端口,可空,默认为随机生成
	Path                 string   `json:"path"`                     //浏览器启动文件路径,可空,默认为Chrome浏览器
	DefaultUrl           string   `json:"new-window"`               //启动浏览器时打开的网址,可空,默认为首页
	Width                int      `json:"width"`                    //浏览器窗口宽度,单位像素,可空,默认为1000
	Height               int      `json:"height"`                   //浏览器窗口高度,单位像素,可空,默认为700
	Maximized            bool     `json:"start-maximized"`          //浏览器启动时直接最大化,为true时为最大化启动,设置的Width、Height参数将失效
	Hide                 bool     `json:"hide"`                     //隐身无痕模式,true为开启,开启后DataDir设置失效
	DataDir              string   `json:"user-data-dir""`           //浏览器数据文件存放路径,可空,默认为原始路径,建议配置此项,防止数据冲突
	Proxy                string   `json:"proxy-server"`             //代理IP,可空,默认为不使用代理,格式:127.0.0.1:8888或http://user:password@proxy.com:8080
	Headless             bool     `json:"headless"`                 //是否启动无头浏览器,启用为true,无界面隐藏运行
	RemoteDebug          bool     `json:"remote-debugging-address"` //是否允许外网调试,允许为true并且Hide强制为true
	DisSecurity          bool     `json:"allow-insecure-localhost"` //禁用localhost上的TLS/SSL错误(无插页式,不阻止请求),true为禁用
	BrowserProcessPrefix string   `json:"browser_process_prefix"`   //运行的浏览器进程前缀,可用于进程区分
	Args                 []string `json:"args"`                     //其他附加参数--开头
}

func (*Browser) ActivateTag

func (p *Browser) ActivateTag(tag Tag) bool

* 激活标签 传参:

tag:Tag标签对象

返回:

成功返回true

func (*Browser) CloseBrowser

func (p *Browser) CloseBrowser()

* 关闭浏览器,如果是隐身无痕模式则自动删除缓存文件

func (*Browser) CloseTag

func (p *Browser) CloseTag(tag Tag) bool

* 关闭标签 传参:

tag:Tag标签对象

返回:

成功返回true

func (*Browser) GetTagList

func (p *Browser) GetTagList(filter string) []Tag

* 取标签列表 传参:

filter:过滤器,用于取指定的标签类型,传入空字符串表示不过滤,
		目前已知的标签类型有:page(页面)、background_page(插件)、iframe(内嵌页)

返回:

获取到的标签列表切片

func (*Browser) NewTag

func (p *Browser) NewTag(url string) (Tag, error)

* 创建新标签 传参:

url:新标签网址,可空

返回:

Tag标签对象,失败则返回error错误信息

func (*Browser) OpenBrowser

func (p *Browser) OpenBrowser() error

* 打开一个浏览器,如果指定了端口,恰巧该端口的浏览器已经在运行中,那么本命令等价于打开一个新标签 本方法会将browser属性字段作为参数启动浏览器 所以如需自定义浏览器环境,需在调用本方法前给browser对象属性字段赋值 注:本方法不支持外网IP调试浏览器时的调用 返回:

成功error返回nil,失败error返回具体信息

type CallParm

type CallParm struct {
	Id     int                    `json:"id"`
	Method string                 `json:"method"`
	Params map[string]interface{} `json:"params"`
}

调用浏览器方法的参数

type ConsoleLog

type ConsoleLog struct {
	Typ       string      `json:"type"`  //输出数据类型,目前已知类型有:undefined=未定义,number=整数值,string=字符串,double=浮点数,boolean=布偶
	Value     interface{} `json:"value"` //日志内容,需转换为真实类型,转换代码:.(真实类型),比如Typ为number,那么Go对应的为int类型,转换代码为:Value.(int)
	ScriptUrl string      `json:"url"`   //触发该日志输出的JS代码所在的文件路径
}

控制台输出日志信息

type Cookie struct {
	Name     string  `json:"name"`     //Cookie名称
	Value    string  `json:"value"`    //Cookie值
	Domain   string  `json:"domain"`   //所指域名
	Path     string  `json:"path"`     //所指路径
	Expires  float64 `json:"expires"`  //过期时间,单位毫秒
	Size     int     `json:"size"`     //名称+值拼接的大小
	HTTPOnly bool    `json:"httpOnly"` //是否禁止通过JS获取该Cookie,true为禁止,false为允许
	Secure   bool    `json:"secure"`   //是否为https协议,true为只能用于https协议
	Session  bool    `json:"session"`  //是否为会话状态保持的Cookie,true表示该Cookie用于会话保持
	Priority string  `json:"priority"` //优先级,chrome的提案,定义了三种优先级,Low/Medium/High,当cookie数量超出时,低优先级的cookie会被优先清除
	Url      string  `json:"url"`      //Domain+Path的字符串拼接
	SameSite string  `json:"sameSite,omitempty"`
}

Cookie信息

type DialogClose

type DialogClose struct {
	Result    bool   `json:"result"`    //结果,具体含义不知
	UserInput string `json:"userInput"` //对话框输入的信息
}

对话框关闭时的信息

type DialogOpen

type DialogOpen struct {
	Url           string `json:"url"`           //发生的网页地址
	Msg           string `json:"message"`       //消息内容
	Typ           string `json:"type"`          //弹窗类型:alert,confirm,prompt,beforeunload
	DefaultPrompt string `json:"defaultPrompt"` //默认文本
}

对话框创建时的信息

type EvalRes

type EvalRes struct {
	ClassName        string `json:"className"`        //类名
	ResType          string `json:"type"`             //结果主类型,目前已知类型有:undefined=未定义,number=整数值,string=字符串,double=浮点数,boolean=布偶
	Subtype          string `json:"subtype"`          //子类型
	Value            string `json:"value"`            //执行结果值
	Description      string `json:"description"`      //结果描述,一般就是将其他类型强制转为string保存到本属性
	ExceptionDetails string `json:"exceptionDetails"` //异常信息,当执行出现错误后本属性有值
}

执行JS表达式结果

type HookHttpBody

type HookHttpBody struct {
	Id     int `json:"id"`
	Result struct {
		Body          string `json:"body"`          //POST数据主体内容
		Base64Encoded bool   `json:"base64Encoded"` //主体内容是否Base64编码
	} `json:"result"`
}

拦截的请求POST数据主体

type HookHttpRequest

type HookHttpRequest struct {
	Method string `json:"method"` //固定内容:Network.requestWillBeSent
	Params struct {
		RequestId   string `json:"requestId"`   //请求ID(和响应ID配对一致)
		LoaderId    string `json:"loaderId"`    //加载页框架ID
		DocumentURL string `json:"documentURL"` //发起请求所在的网页路径
		Request     struct {
			Url             string            `json:"url"`         //请求URL
			Method          string            `json:"method"`      //请求模式:GET POST
			Headers         map[string]string `json:"headers"`     //Head信息
			PostData        string            `json:"postData"`    //POST数据内容
			HasPostData     bool              `json:"hasPostData"` //是否有POST数据
			PostDataEntries []struct {
				Bytes string `json:"bytes"` //POST数据Base64编码后的内容
			} `json:"postDataEntries"`
			MixedContentType string `json:"mixedContentType"`
			InitialPriority  string `json:"initialPriority"`
			ReferrerPolicy   string `json:"referrerPolicy"`
		} `json:"request"`
		Timestamp float64 `json:"timestamp"`
		WallTime  float64 `json:"wallTime"`
		Initiator struct {
			Type  string `json:"type"` //源类型
			Stack struct {
				CallFrames []struct {
					FunctionName string `json:"functionName"`
					ScriptId     string `json:"scriptId"`
					Url          string `json:"url"`
					LineNumber   int    `json:"lineNumber"`
					ColumnNumber int    `json:"columnNumber"`
				} `json:"callFrames"`
				Parent struct {
					Description string `json:"description"`
					CallFrames  []struct {
						FunctionName string `json:"functionName"`
						ScriptId     string `json:"scriptId"`
						Url          string `json:"url"`
						LineNumber   int    `json:"lineNumber"`
						ColumnNumber int    `json:"columnNumber"`
					} `json:"callFrames"`
				} `json:"parent"`
			} `json:"stack"`
		} `json:"initiator"`
		Type           string `json:"type"`    //请求类型:XHR Script Document等
		FrameId        string `json:"frameId"` //页面框架ID
		HasUserGesture bool   `json:"hasUserGesture"`
	} `json:"params"`
}

拦截请求的内容

type HookHttpResponse

type HookHttpResponse struct {
	Method string `json:"method"` //固定内容:Network.responseReceived
	Params struct {
		RequestId string  `json:"requestId"` //响应ID(和请求ID配对一致)
		LoaderId  string  `json:"loaderId"`  //加载页框架ID
		Timestamp float64 `json:"timestamp"`
		Type      string  `json:"type"` //请求类型:XHR Script Document等
		Response  struct {
			Url               string            `json:"url"`    //请求URL
			Status            int               `json:"status"` //响应状态码
			StatusText        string            `json:"statusText"`
			Headers           map[string]string `json:"headers"`  //响应的头部信息
			MimeType          string            `json:"mimeType"` //文档类型:比如application/json
			ConnectionReused  bool              `json:"connectionReused"`
			ConnectionId      int               `json:"connectionId"`
			RemoteIPAddress   string            `json:"remoteIPAddress"` //远程IP地址
			RemotePort        int               `json:"remotePort"`      //远程IP地址端口
			FromDiskCache     bool              `json:"fromDiskCache"`
			FromServiceWorker bool              `json:"fromServiceWorker"`
			FromPrefetchCache bool              `json:"fromPrefetchCache"` //是否从缓存读取
			EncodedDataLength int               `json:"encodedDataLength"` //返回数据长度
			Timing            struct {
				RequestTime              float64 `json:"requestTime"`
				ProxyStart               int     `json:"proxyStart"`
				ProxyEnd                 int     `json:"proxyEnd"`
				DnsStart                 int     `json:"dnsStart"`
				DnsEnd                   int     `json:"dnsEnd"`
				ConnectStart             int     `json:"connectStart"`
				ConnectEnd               int     `json:"connectEnd"`
				SslStart                 int     `json:"sslStart"`
				SslEnd                   int     `json:"sslEnd"`
				WorkerStart              int     `json:"workerStart"`
				WorkerReady              int     `json:"workerReady"`
				WorkerFetchStart         int     `json:"workerFetchStart"`
				WorkerRespondWithSettled int     `json:"workerRespondWithSettled"`
				SendStart                float64 `json:"sendStart"`
				SendEnd                  float64 `json:"sendEnd"`
				PushStart                int     `json:"pushStart"`
				PushEnd                  int     `json:"pushEnd"`
				ReceiveHeadersEnd        float64 `json:"receiveHeadersEnd"`
			} `json:"timing"`
			ResponseTime    float64 `json:"responseTime"`
			Protocol        string  `json:"protocol"`
			SecurityState   string  `json:"securityState"`
			SecurityDetails struct {
				Protocol                       string   `json:"protocol"`
				KeyExchange                    string   `json:"keyExchange"`
				KeyExchangeGroup               string   `json:"keyExchangeGroup"`
				Cipher                         string   `json:"cipher"`
				CertificateId                  int      `json:"certificateId"`
				SubjectName                    string   `json:"subjectName"`
				SanList                        []string `json:"sanList"`
				Issuer                         string   `json:"issuer"`
				ValidFrom                      int      `json:"validFrom"`
				ValidTo                        int      `json:"validTo"`
				SignedCertificateTimestampList []struct {
					Status             string `json:"status"`
					Origin             string `json:"origin"`
					LogDescription     string `json:"logDescription"`
					LogId              string `json:"logId"`
					Timestamp          int64  `json:"timestamp"`
					HashAlgorithm      string `json:"hashAlgorithm"`
					SignatureAlgorithm string `json:"signatureAlgorithm"`
					SignatureData      string `json:"signatureData"`
				} `json:"signedCertificateTimestampList"`
				CertificateTransparencyCompliance string `json:"certificateTransparencyCompliance"`
			} `json:"securityDetails"`
		} `json:"response"`
		FrameId string `json:"frameId"` //页面框架ID
	} `json:"params"`
}

拦截响应的内容

type Tag

type Tag struct {
	Description          string `json:"description"`          //标签描述
	DevtoolsFrontendUrl  string `json:"devtoolsFrontendUrl"`  //前端开发工具链接
	FaviconUrl           string `json:"faviconUrl"`           //标签页logo
	Id                   string `json:"id"`                   //标签页ID
	ParentId             string `json:"parentId"`             //标签页父ID
	Title                string `json:"title"`                //标签页标题
	Typ                  string `json:"type"`                 //标签页类型:page(页面)、background_page(插件)、iframe(内嵌页)
	Url                  string `json:"url"`                  //标签页链接
	WebSocketDebuggerUrl string `json:"webSocketDebuggerUrl"` //WebSocket链接

	Frames []frame //框架集合,首个成员为主框架信息,其他均为子框架,本对象内有框架信息及网页音视频图资源信息
	// contains filtered or unexported fields
}

func (*Tag) Call

func (p *Tag) Call(method string, params map[string]interface{}) (string, error)

* 调用浏览器方法 传参:

method:方法名
params:参数map类型,可用map传递多个参数

返回

调用结果

func (*Tag) Close

func (p *Tag) Close(tagClose bool)

* 关闭连接,关闭连接后,将无法对标签进行操作 传参:

tagClose:是否关闭标签,如果不关闭标签,下次则还可以继续调用Connect方法连接本标签进行操作。

func (*Tag) Connect

func (p *Tag) Connect() (bool, error)

* 连接标签,连接成功后才可对标签进行操作 默认可以不调用本方法进行连接,在调用Tag结构体其余方法时会自动连接。 返回:

成功返回true,失败返回false和error错误信息

func (*Tag) CookiesClear

func (p *Tag) CookiesClear() bool

* Cookies清空,清除当前页面中Cookies数据 返回:

成功返回true,否则返回false

func (*Tag) CookiesDel

func (p *Tag) CookiesDel(url string, name string) bool

* Cookies删除单个 传参:

url:domain+Path的字符串拼接,也可以只传domain
name:名称,要删除的Cookie名称

返回:

成功返回true,否则返回false

func (*Tag) CookiesGet

func (p *Tag) CookiesGet(domain string) ([]Cookie, string, error)

* Cookies获取 传参:

domain:域名,如果要获取顶级域名的Cookies请填写:http://.baidu.com,不区分http和https,两者结果都一样,传空字符串表示获取获取所有域名的cookies

返回2参数:

参数1:为Cookie结构体的切片,表示多个Cookie
参数2:为Cookie的拼接字符串,可用于浏览器或本big包下的HttpSend的Cookies字段,如果想获取单个Cookie值,请用big.HttpGetCookie函数获取
参数3:为错误信息

func (*Tag) CookiesSet

func (p *Tag) CookiesSet(cookie Cookie) bool

* Cookies单个设置,如不存在,则创建 传参:

cookie:欲设置的Cookie,请使用chrome.Cookie赋值并传入

返回:

成功返回true,否则返回false

func (*Tag) CookiesSetStr

func (p *Tag) CookiesSetStr(url string, domain string, path string, secure bool, expires float64, httponly bool, cookies string) bool

* Cookies批量设置【文本形式】 传参:

url:domain+Path的字符串拼接,也可以只传domain,需要加http。
domain:域名,如果要设置顶级域名的Cookies请填写:.baidu.com,不需要加http。
path:路径,一般为“/”
secure:是否为https协议,true为只能用于https协议
expires:过期时间,单位毫秒,可空,传0表示365天过期
httponly:是否禁止通过JS获取该Cookie,true为禁止,false为允许
cookies:字符串格式的cookies,类似于:name=value; name=value这种格式

返回:

成功返回true,否则返回false

func (*Tag) CookiesSetStu

func (p *Tag) CookiesSetStu(cookies []Cookie) bool

* Cookies批量设置【[]chrome.Cookie结构体切片形式】 传参:

cookies:[]chrome.Cookie结构体切片

返回:

成功返回true,否则返回false

func (*Tag) CssFormReset

func (p *Tag) CssFormReset(contextId int, selector string) bool

* CSS表单(FORM)重置 传参:

contextId:指定框架上下文id,传0表示默认主框架
selector:CSS选择器路径

返回:

成功返回true,否则返回false

func (*Tag) CssFormSubmit

func (p *Tag) CssFormSubmit(contextId int, selector string) bool

* CSS表单(FORM)提交 传参:

contextId:指定框架上下文id,传0表示默认主框架
selector:CSS选择器路径

返回:

成功返回true,否则返回false

func (*Tag) CssFormUrlGet

func (p *Tag) CssFormUrlGet(contextId int, selector string) string

* CSS取表单(FORM)提交地址 传参:

contextId:指定框架上下文id,传0表示默认主框架
selector:CSS选择器路径

返回:

结果

func (*Tag) CssFormUrlSet

func (p *Tag) CssFormUrlSet(contextId int, selector string, url string) string

* CSS置表单(FORM)提交地址 传参:

contextId:指定框架上下文id,传0表示默认主框架
selector:CSS选择器路径
url:欲设置的表单提交地址

返回:

设置成功,返回设置的url表单提交地址

func (*Tag) CssSelectIndexLength

func (p *Tag) CssSelectIndexLength(contextId int, selector string) int

* CSS下拉菜单(SELECT)取表项数 传参:

contextId:指定框架上下文id,传0表示默认主框架
selector:CSS选择器路径,用于选择SELECT标签

返回:

表项总数

func (*Tag) CssSelectNowIndexGet

func (p *Tag) CssSelectNowIndexGet(contextId int, selector string) int

* CSS下拉菜单(SELECT)取现行选中项 传参:

contextId:指定框架上下文id,传0表示默认主框架
selector:CSS选择器路径,用于选择SELECT标签

返回:

现在选中的表项下标

func (*Tag) CssSelectNowIndexSet

func (p *Tag) CssSelectNowIndexSet(contextId int, selector string, index int) bool

* CSS下拉菜单(SELECT)置现行选中项 传参:

contextId:指定框架上下文id,传0表示默认主框架
selector:CSS选择器路径,用于选择SELECT标签
index:欲设置的选中项下标

返回:

成功返回true,否则返回false

func (*Tag) CssSelectTextGet

func (p *Tag) CssSelectTextGet(contextId int, selector string, index int) string

* CSS下拉菜单(SELECT)取表项文本 传参:

contextId:指定框架上下文id,传0表示默认主框架
selector:CSS选择器路径,用于选择SELECT标签
index:表项下标

返回:

结果

func (*Tag) CssTableCellHtmlGet

func (p *Tag) CssTableCellHtmlGet(contextId int, selector string, row int, cell int) string

* CSS表格(Table)取单元格源码 传参:

contextId:指定框架上下文id,传0表示默认主框架
selector:CSS选择器路径,用于选择SELECT标签
row:第几行
cell:第几列

返回:

单元格源码

func (*Tag) CssTableCellTextGet

func (p *Tag) CssTableCellTextGet(contextId int, selector string, row int, cell int) string

* CSS表格(Table)取单元格文本 传参:

contextId:指定框架上下文id,传0表示默认主框架
selector:CSS选择器路径,用于选择SELECT标签
row:第几行
cell:第几列

返回:

单元格文本

func (*Tag) CssTableCells

func (p *Tag) CssTableCells(contextId int, selector string) int

* CSS表格(Table)取单元格列数 传参:

contextId:指定框架上下文id,传0表示默认主框架
selector:CSS选择器路径,用于选择SELECT标签

返回:

单元格列数

func (*Tag) CssTableRows

func (p *Tag) CssTableRows(contextId int, selector string) int

* CSS表格(Table)取单元格行数 传参:

contextId:指定框架上下文id,传0表示默认主框架
selector:CSS选择器路径,用于选择SELECT标签

返回:

单元格行数

func (*Tag) DomAttributeGet

func (p *Tag) DomAttributeGet(contextId int, selector string, name string) string

* 元素取属性值 传参:

contextId:指定框架上下文id,传0表示默认主框架
selector:CSS选择器路径,用于选择SELECT标签
name:属性名

返回:

结果

func (*Tag) DomAttributeSet

func (p *Tag) DomAttributeSet(contextId int, selector string, name string, val string) bool

* 元素置属性值 传参:

contextId:指定框架上下文id,传0表示默认主框架
selector:CSS选择器路径,用于选择SELECT标签
name:属性名
val:属性值

返回:

成功返回true,否则返回false

func (*Tag) DomBlur

func (p *Tag) DomBlur(contextId int, selector string) bool

* 元素焦点失去 传参:

contextId:指定框架上下文id,传0表示默认主框架
selector:CSS选择器路径,用于选择SELECT标签

返回:

成功返回true,否则返回false

func (*Tag) DomCheckboxSet

func (p *Tag) DomCheckboxSet(contextId int, selector string, check string) bool

* 元素复选框置状态 传参:

contextId:指定框架上下文id,传0表示默认主框架
selector:CSS选择器路径,用于选择SELECT标签
check:是否选中,true为选中,false为未选中

返回:

成功返回true,否则返回false

func (*Tag) DomClick

func (p *Tag) DomClick(contextId int, selector string) bool

* 元素触发单击事件 传参:

contextId:指定框架上下文id,传0表示默认主框架
selector:CSS选择器路径,用于选择SELECT标签

返回:

成功返回true,否则返回false

func (*Tag) DomFocus

func (p *Tag) DomFocus(contextId int, selector string) bool

* 元素焦点激活 传参:

contextId:指定框架上下文id,传0表示默认主框架
selector:CSS选择器路径,用于选择SELECT标签

返回:

成功返回true,否则返回false

func (*Tag) DomHtmlGet

func (p *Tag) DomHtmlGet(contextId int, selector string) string

* 元素取HTML 传参:

contextId:指定框架上下文id,传0表示默认主框架
selector:CSS选择器路径,用于选择SELECT标签

返回:

HTML源码

func (*Tag) DomHtmlSet

func (p *Tag) DomHtmlSet(contextId int, selector string, html string) bool

* 元素置HTML 传参:

contextId:指定框架上下文id,传0表示默认主框架
selector:CSS选择器路径,用于选择SELECT标签
html:欲设置的html源码

返回:

成功返回true,否则返回false

func (*Tag) DomOnEvent

func (p *Tag) DomOnEvent(contextId int, selector string, name string) string

* 元素执行指定事件 传参:

contextId:指定框架上下文id,传0表示默认主框架
selector:CSS选择器路径,用于选择SELECT标签
name:欲执行的事件,需自己加括号,例如:click()

返回:

执行返回结果

func (*Tag) DomPosition

func (p *Tag) DomPosition(contextId int, selector string) (x int, y int)

* 元素定位 传参:

contextId:指定框架上下文id,传0表示默认主框架
selector:CSS选择器路径,用于选择SELECT标签

返回:

x:元素x坐标
y:元素y坐标

func (*Tag) DomTextGet

func (p *Tag) DomTextGet(contextId int, selector string) string

* 元素取文本 传参:

contextId:指定框架上下文id,传0表示默认主框架
selector:CSS选择器路径,用于选择SELECT标签

返回:

结果

func (*Tag) DomTextSet

func (p *Tag) DomTextSet(contextId int, selector string, text string) bool

* 元素置文本 传参:

contextId:指定框架上下文id,传0表示默认主框架
selector:CSS选择器路径,用于选择SELECT标签
text:欲设置的元素文本

返回:

成功返回true,否则返回false

func (*Tag) DomValGet

func (p *Tag) DomValGet(contextId int, selector string) string

* 元素取值 传参:

contextId:指定框架上下文id,传0表示默认主框架
selector:CSS选择器路径,用于选择SELECT标签

返回:

结果

func (*Tag) DomValSet

func (p *Tag) DomValSet(contextId int, selector string, val string) bool

* 元素置值 传参:

contextId:指定框架上下文id,传0表示默认主框架
selector:CSS选择器路径,用于选择SELECT标签
val:欲设置的元素值

返回:

成功返回true,否则返回false

func (*Tag) EvalJs

func (p *Tag) EvalJs(jscode string, contextId int) (EvalRes, error)

* 执行JS代码【精简版】 传参:

jscode:JS代码,可以多行。
contextId:指定框架上下文id,传0表示默认主框架

返回:

EvalRes:EvalRes结构体用于保存执行结果,具体结果查看该结构体字段属性
error:用于记录执行在Go中发生的错误

func (*Tag) Evaluate

func (p *Tag) Evaluate(expression string, objectGroup string, includeCommandLineAPI bool, silent bool,
	executionContextId int, returnByValue bool, generatePreview bool, userGesture bool, awaitPromise bool) (EvalRes, error)

* 执行表达式 传参:

expression: 表达式文本
objectGroup: 可用于释放多个对象的符号组名称。
includeCommandLineAPI: 确定在评估期间Command Line API是否可用。
silent: 在静默模式下,评估期间抛出的异常不报告,不要暂停执行。 覆盖setPauseOnException状态。
executionContextId: 指定执行上下文执行评估。 如果省略参数,将在检查页面的上下文中执行评估。
returnByValue: 结果是否应该是应该通过值发送的JSON对象。
generatePreview: 是否应为结果生成预览。
userGesture: 执行是否应该被视为用户在UI中发起的。
awaitPromise: 执行是否应该等待承诺解决。 如果评估结果不是承诺,则认为是错误。

返回:

EvalRes:EvalRes结构体用于保存执行结果,具体结果查看该结构体字段属性
error:用于记录执行在Go中发生的错误

func (*Tag) HookGetBody

func (p *Tag) HookGetBody(requestId string) HookHttpBody

* 获取POST数据内容 传参:

requestId:该参数在拦截的网络请求形参中有,传入对应相同名的参数即可

返回:

返回chrome.HookHttpBody结构体对象,具体字段属性含义查看源码定义时的注释

func (*Tag) HookHttpDis

func (p *Tag) HookHttpDis()

* 禁用拦截网络请求

func (*Tag) HookHttpEn

func (p *Tag) HookHttpEn(req func(tag *Tag, request HookHttpRequest), resp func(tag *Tag, response HookHttpResponse))

* 开启拦截网络请求,目前仅支持拦截不支持修改数据 传参:

req:传入格式为func(tag *chrome.Tag, request chrome.HookHttpRequest)的函数,当有请求时会自动触发
resp:传入格式为func(tag *chrome.Tag, response HookHttpResponse)的函数,当有响应时会自动触发

func (*Tag) InputMouseMove

func (p *Tag) InputMouseMove(x int, y int, bionic bool)

* 模拟鼠标轨迹移动到指定坐标 传参:

x:事件的X坐标相对于CSS像素中的主框架的视口
y:事件的Y坐标相对于CSS像素中的主框架视口,0表示视口的顶部,Y随着进入视口底部而增加。
bionic:轨迹是否仿生,仿生可过99%轨迹效验算法。

func (*Tag) InputSendClick

func (p *Tag) InputSendClick(x float64, y float64, duration int, tapCount int, gestureSourceType string) bool

* 发送合成点击手势 传参:

x:X坐标,相当于CSS像素,相对于浏览器视口左上角
y:Y坐标,相当于CSS像素,相对于浏览器视口左上角
duration:持续时间,达阵和触摸事件之间的持续时间(ms)(默认值:50)
tapCount:Tap次数,执行Tap的次数(例如2次双击,默认值为1)
gestureSourceType:手势类型,要生成哪种类型的输入事件(默认值:“default”,它会查询平台的首选输入类型),可选值:default, touch, mouse

返回:

成功返回true,否则返回false

func (*Tag) InputSendKey

func (p *Tag) InputSendKey(typ string, modifiers int, windowsVirtualKeyCode int, isKeypad bool, isSysttemKey bool) bool

* 发送按键事件 传参:

typ:按键类型,可选值: keyDown, keyUp, rawKeyDown, char
modifiers:功能键,可选值:Alt = 1,Ctrl = 2,Meta/Command = 4,Shift = 8,默认为 = 0
windowsVirtualKeyCode:Windows虚拟键代码(默认值:0)
isKeypad:事件是否从小键盘生成,true为是,false为否,一般为false
isSysttemKey:事件是否是系统键事件,true为是,false为否,一般为false

返回:

成功返回true,否则返回false

func (*Tag) InputSendMouse

func (p *Tag) InputSendMouse(typ string, x int, y int, modifiers int, button string, clickCount int, deltaX int, deltaY int) bool

InputSendMouse 发送鼠标事件 传参:

typ:鼠标类型,可选值: mousePressed, mouseReleased, mouseMoved, mouseWheel
x:事件的X坐标相对于CSS像素中的主框架的视口
y:事件的Y坐标相对于CSS像素中的主框架视口,0表示视口的顶部,Y随着进入视口底部而增加。
modifiers:功能键,可选值:Alt = 1,Ctrl = 2,Meta/Command = 4,Shift = 8,默认为 = 0
button:鼠标按键,可选值: none, left, middle, right
clickCount:点击次数,单击鼠标按钮的次数(默认值:0)
deltaX:鼠标滚轮事件的CSS像素中的X delta(默认值:0)
deltaY:鼠标滚轮事件的CSS像素中的Y delta(默认值:0)

返回:

成功返回true,否则返回false

func (*Tag) InputSendPinch

func (p *Tag) InputSendPinch(x float64, y float64, scaleFactor float64, relativeSpeed int, gestureSourceType string) bool

* 发送合成捏合手势【实验性功能】 传参:

x:X坐标,相当于CSS像素,相对于浏览器视口左上角
y:Y坐标,相当于CSS像素,相对于浏览器视口左上角
scaleFactor:缩放后的相对缩放因子(> 1.0放大,<1.0缩小)
relativeSpeed:相对指针速度(以像素为单位)(默认值:800)
gestureSourceType:要生成哪种类型的输入事件(默认值:“default”,它会查询平台的首选输入类型),可选值:default, touch, mouse

返回:

成功返回true,否则返回false

func (*Tag) InputSendRoll

func (p *Tag) InputSendRoll(x float64, y float64, xDistance float64, yDistance float64, xOverscroll float64, yOverscroll float64, preventFling bool, speed int, gestureSourceType string, repeatCount int, repeatDelayMs int) bool

* 发送合成滚动手势 传参:

x:X坐标,相当于CSS像素,相对于浏览器视口左上角
y:Y坐标,相当于CSS像素,相对于浏览器视口左上角
xDistance:x滚动距离,沿X轴滚动的距离(正向左滚动)
yDistance:y滚动距离,沿Y轴滚动的距离(向上滚动)
xOverscroll:x滚动增量,除了给定距离之外,沿X轴向后滚动的附加像素数
yOverscroll:y滚动增量,除了给定距离之外,沿Y轴向后滚动的附加像素数
preventFling:不清楚意义,默认值为true,建议为true
speed:速度,以秒为单位的扫描速度(默认值:800)
gestureSourceType:手势类型,要生成哪种类型的输入事件(默认值:“default”,它会查询平台的首选输入类型),可选值:default, touch, mouse
repeatCount:重复次数,重复手势的次数(默认值:0)
repeatDelayMs:每次重复之间的毫秒数延迟(默认值:250)

返回:

成功返回true,否则返回false

func (*Tag) InputSendText

func (p *Tag) InputSendText(typ string, modifiers int, text string, isKeypad bool, isSysttemKey bool) bool

* 发送文本事件 传参:

typ:按键类型,可选值: keyDown, keyUp, rawKeyDown, char
modifiers:功能键,可选值:Alt = 1,Ctrl = 2,Meta/Command = 4,Shift = 8,默认为 = 0
text:欲发送的文本
isKeypad:事件是否从小键盘生成,true为是,false为否,一般为false
isSysttemKey:事件是否是系统键事件,true为是,false为否,一般为false

返回:

成功返回true,否则返回false

func (*Tag) InputSendTouch

func (p *Tag) InputSendTouch(typ string, touchs []Touch, modifiers int) bool

* 发送触摸点事件 传参:

typ:触摸事件的类型,TouchEnd和TouchCancel不能包含任何触摸点,而TouchStart和TouchMove必须至少包含一个,可选的值:touchStart,touchEnd,touchMove,touchCancel
touchs:触摸设备上的活动触摸点。每个任何改变点(与序列中的先前触摸事件相比)产生一个事件,逐个模拟按压/移动/释放点。
modifiers:功能键,可选值:Alt = 1,Ctrl = 2,Meta/Command = 4,Shift = 8,默认为 = 0

返回:

成功返回true,否则返回false

func (*Tag) ReLoad

func (p *Tag) ReLoad(ignoreCache bool, scriptToEvaluateOnLoad string, timeOut int, flag string) bool

* 重新载入(刷新) 传参:

ignoreCache:是否忽略缓存
scriptToEvaluateOnLoad:如果设置,脚本将被重新加载后注入被检查页面的所有帧,可空。
timeOut:等待页面加载完成时间,单位秒,为0表示不等待
flag:本参数在timeOut大于0时生效,如果flag为空字符串,则会等待到页面转圈结束为止,如果flag有值,则会等待到网页源码存在该值为止

返回:

成功返回true,否则返回false,注意:当需等待网页加载完成时,返回的true和false代表网页是否加载完成。

func (*Tag) TagCPUThrottlingRateSet

func (p *Tag) TagCPUThrottlingRateSet(rate float64) bool

* CPU限速:启用CPU限制来模拟缓慢的CPU 传参:

rate:节流率为减速因子(1为无油门,2为2倍减速等)

返回:

成功返回true,失败返回false

func (*Tag) TagCacheClear

func (p *Tag) TagCacheClear() bool

* 清除缓存 返回:

成功返回true,否则返回false

func (*Tag) TagCaptureScreenshot

func (p *Tag) TagCaptureScreenshot(format string, quality int, fromSurface bool, x int, y int, width int, height int) string

* 网页快照截图,拍摄一张当前页面的渲染图像 传参:

format:存储格式jpeg或png
quality:压缩质量,仅为jpeg格式下有效,取值范围0-100
fromSurface:从表面(Surface)获取屏幕截图,而不是视图(View)
x:截取指定区域,从x坐标开始,x、y、width、height均为0表示截取全网页
y:截取指定区域,从y坐标开始,x、y、width、height均为0表示截取全网页
width:截图指定区域的宽度,x、y、width、height均为0表示截取全网页
height:截图指定区域的高度,x、y、width、height均为0表示截取全网页

返回:

成功返回base64编码后的图片内容,失败返回空文本

func (*Tag) TagConsoleLogsGet

func (p *Tag) TagConsoleLogsGet(clear bool) []ConsoleLog

* 取控制台日志信息 传参:

clear:是否获取后清理日志,清理后下次获取将返回新的日志,不清理下次获取将返回继续累加的日志。

返回:

返回[]chrome.ConsoleLog切片

func (*Tag) TagDialogHandle

func (p *Tag) TagDialogHandle(accept bool, promptText string) bool

* 反馈对话框,接受或解除JavaScript启动的对话框(alert,confirm,prompt或onbeforeunload) 传参:

accept:接收或解除,true为接收对话框,false为解除对话框
promptText:输入文本,可空

返回:

成功返回true,失败返回false

func (*Tag) TagDialogHook

func (p *Tag) TagDialogHook(dopen func(d DialogOpen), dclose func(d DialogClose))

* 拦截标签页对话框事件,当标签页弹出或关闭对话框(alert,confirm,prompt,beforeunload)时自动触发; 若想取消拦截,则再次调用本方法,dopen和dclose参数传nil即可取消拦截 传参:

dopen:传入一个func函数,当对话框弹出时自动调用该函数,并将chrome.DialogOpen结构体对象作为形参传入,函数需定义形参,格式:func demo(d DialogOpen) {}
dclose:传入一个func函数,当对话框关闭时自动调用该函数,并将chrome.DialogClose结构体对象作为形参传入,函数需定义形参,格式:func demo(d DialogClose) {}

注意:如果两个事件都拦截,那么不想拦截的事件函数可传nil

func (*Tag) TagDialogIsHave

func (p *Tag) TagDialogIsHave() bool

* 检查当前页面是否已弹出alert,confirm,prompt,beforeunload等提示框 返回:

已弹出返回true,否则返回false

func (*Tag) TagFrameUpdate

func (p *Tag) TagFrameUpdate() bool

* 更新标签中的框架信息,当页面结构发生改变,应先调用本方法更新框架信息后才可执行后续操作 返回:

返回是否成功,更新成功后调用本对象的.Frames属性获取框架资源信息

func (*Tag) TagGeolocationOverrideSet

func (p *Tag) TagGeolocationOverrideSet(longitude float64, latitude float64, accuracy float64) bool

* 模拟地理位置,虚拟定位 传参:

longitude:经度
latitude:纬度
accuracy:精度

返回:

设置成功返回true,否则返回false

func (*Tag) TagHtmlGet

func (p *Tag) TagHtmlGet(contextId int) string

* 取标签页面源码 传参:

contextId:指定框架上下文id,传0表示默认主框架

返回:

网页源码

func (*Tag) TagJump

func (p *Tag) TagJump(url string, referer string, timeOut int, flag string) bool

* 跳转到新的URL地址 传参:

url:新的URL地址
referer:来路设置,可空
timeOut:等待页面加载完成时间,单位秒,为0表示不等待
flag:本参数在timeOut大于0时生效,如果flag为空字符串,则会等待到页面转圈结束为止,如果flag有值,则会等待到网页源码存在该值为止

返回:

成功返回true,否则返回false,注意:当需等待网页加载完成时,返回的true和false代表网页是否加载完成。

func (*Tag) TagLoadIsEnd

func (p *Tag) TagLoadIsEnd() bool

* 标签页是否已加载完成 返回:

标签对象加载完成返回true,否则返回false

func (*Tag) TagLoadWaitEnd

func (p *Tag) TagLoadWaitEnd(timeOut int, flag string) bool

* 等待标签页加载完成 传参:

timeOut:最长等待时间,单位秒
flag:如果flag为空字符串,则会等待到页面转圈结束为止,如果flag有值,则会等待到网页源码存在该值为止

返回:

页面加载完成返回true,否则返回false

func (*Tag) TagResourceContentGet

func (p *Tag) TagResourceContentGet(frameId string, url string, base64 bool) string

* 提取网页资源,比如提取网页图片音视频、JS、CSS等文件内容 传参:

frameId:指定框架的id,传空字符串表示默认主框架
url:本对象.Frames[0].Resources[]结构体中的url
base64:是否base64编码,如提取图片音视频则建议base64编码

返回:

提取的结果

func (*Tag) TagScreenClear

func (p *Tag) TagScreenClear() bool

* 清除设置屏幕的指标值 返回:

成功返回true,否则返回false

func (*Tag) TagScreenSet

func (p *Tag) TagScreenSet(width int, height int, deviceScaleFactor float64, mobile bool, flScale float64, screenWidth int, screenHeight int, positionX int, positionY int, screenOrientationType string, screenOrientationAngle int) bool

* 设置屏幕 覆盖设备屏幕尺寸的值(window.screen.width,window.screen.height,window.innerWidth, window.innerHeight和“device-width”/“device-height”)相关的CSS媒体查询结果) 传参:

width:宽度,覆盖的宽度值,像素单位,允许的范围(最小 0,最大 10000000),设置为0时,禁用覆盖
height:高度,覆盖的高度值,像素单位,允许的范围(最小 0,最大 10000000),设置为0时,禁用覆盖
deviceScaleFactor:设备比例因子,覆盖设备的比例因子值,设置为0时,禁用覆盖
mobile:移动模式,是否模拟移动设备,这包含视口元标记,覆盖 滚动条,文本自动调整等
flScale:缩放比例,应用于缩放生成的视图图像,忽略|fitWindow|模式
screenWidth:屏幕宽度,覆盖屏幕宽度值(以像素为单位)(最小值0,最大10000000)。 只用于| mobile == true |
screenHeight:屏幕高度,覆盖屏幕高度值(以像素为单位)(最小0,最大10000000)。 只用于| mobile == true |
positionX:视图位置X,在屏幕上覆盖视图X位置(以像素为单位)(最小值0,最大值10000000)。 只用于| mobile == true |
positionY:视图位置Y,在屏幕上覆盖视图Y位置(以像素为单位)(最小值0,最大值10000000)。 只用于| mobile == true |
screenOrientationType:屏幕方向类型,设置屏幕方向,可空,可选的值: portraitPrimary, portraitSecondary, landscapePrimary, landscapeSecondary
screenOrientationAngle:屏幕角度,所处方向的角度

返回:

成功返回true,否则返回false

func (*Tag) TagTouchEmulationEnabledSet

func (p *Tag) TagTouchEmulationEnabledSet(enable bool, configuration string) bool

* 启用仿真模拟触点设备 传参:

enable:是否启用仿真触摸事件,true为启用,false为不启用
configuration:手势事件类型,可选值mobile, desktop

返回:

成功返回true,否则返回false

func (*Tag) TagUserAgentSet

func (p *Tag) TagUserAgentSet(ua string) bool

* 置标签页用户代理标识UA 传参:

ua:需要设置的UserAgent

返回:

设置成功返回true,否则返回false

func (*Tag) WindowScrollBySet

func (p *Tag) WindowScrollBySet(contextId int, xNum int, yNum int) bool

* 窗口滚动条距离设置 传参:

contextId:指定框架上下文id,传0表示默认主框架
xNum:文档向右滚动的像素数
yNum:文档向下滚动的像素数

func (*Tag) WindowScrollToSet

func (p *Tag) WindowScrollToSet(contextId int, x int, y int) bool

* 窗口滚动条位置设置 传参:

contextId:指定框架上下文id,传0表示默认主框架
x:要在窗口文档显示区左上角显示的文档的x坐标
y:要在窗口文档显示区左上角显示的文档的y坐标

func (*Tag) WindowSet

func (p *Tag) WindowSet(left int, top int, width int, height int, windowState string) bool

* 浏览器窗口位置大小状态设置 传参:

left:设置浏览器离屏幕左边的位置,单位px,传-1表示不设置
top:设置浏览器离屏幕顶边的位置,单位px,传-1表示不设置
width:设置浏览器宽度,单位px,传-1表示不设置
height:设置浏览器高度,单位px,传-1表示不设置
windowState:窗口状态,可选值normal(普通), minimized(最小化), maximized(最大化), fullscreen(全屏),传空字符串表示不设置

返回:

成功返回true,否则返回false

type TagContext

type TagContext struct {
	Id      int    `json:"id"`
	Origin  string `json:"origin"`
	Name    string `json:"name"`
	FrameId string `json:"frameId"`
}

标签上下文

type Touch

type Touch struct {
	X             float64 `json:"x"`             //X坐标,相当于CSS像素,相对于浏览器视口左上角
	Y             float64 `json:"y"`             //Y坐标,相当于CSS像素,相对于浏览器视口左上角,0表示视口的顶部,Y随着进入视口底部而增加
	RadiusX       float64 `json:"radiusX"`       //触摸区域的X半径(默认值:1.0)
	RadiusY       float64 `json:"radiusY"`       //触摸区域的Y半径(默认值:1.0)
	RotationAngle float64 `json:"rotationAngle"` //旋转角度(默认值:0.0)
	Force         float64 `json:"force"`         //强制(默认值:1.0)
	Id            float64 `json:"id"`            //用于跟踪事件之间的触摸源的标识符在事件中必须是唯一的
}

触摸点结构体

Jump to

Keyboard shortcuts

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