Pack chromedp

// Package chromedp is a high level Chrome DevTools Protocol client that simplifies driving browsers for scraping, unit testing, or profiling web pages using the CDP.// chromedp requires no third-party dependencies, implementing the async Chrome DevTools Protocol entirely in Go.// This package includes a number of simple examples. Additionally, https://github.com/chromedp/examples contains more complex examples.
chromedp 包 是一个 高等级 chrome devtools protocol 客户单,它简化了驱动浏览器抓取,单元测试,或使用CDP 分析web 页面。
chromedp 不需要依赖第三方,完全在Go中实现异步 chrome devtools protocol。
这个包包含了许多简单示例。另外,https://github.com/chromedp/examples 包含更多复杂示例

type Context struct{}

//Context is attached to any context.Context which is valid for use with Run.
上下文依附任务上下文,上下文可与Run一起使用。

Allocator Allocator

Allocator is used to create new browers. It is inherited from parent context where using NewContext
分配器用于创建新的浏览器。它继承使用NewContext的父上下文。

Browser *Browser

Browser is the browser being used in the context. It is inherited from the parent context where using NewContext.
Browser 是上下文中使用的浏览器。 他继承自使用 NewContext的父上下文。

Target *Target

Target is the target to run actions(commands) against. It is not inherited from the parent context, and typically each context will have its own unique Target pointing to a separate browser tab(page)
Target是要对其运行操作(命令)的目标。它不是从父上下文继承的,而且通常每个上下文都有自己唯一的目标指向单独的浏览器选项卡(页面)

targetID target.ID

targetID is set up by WithTargetID. If nil ,Run will pick the only unused page target, or create a new one.
targetID 是由 WithTargetID 设置的。如果 为nil,Run 将只选择未使用的页面目标,或者创建一个新的。

browserListeners []cancelableListener

targetListeners []cancelableListener

browserOpts []BrowserOption

browserOpts holds the browser options passed to NewContext via WithBrowserOption, so that they can later be used when allocating a browser in Run.
browserOpts保存通过WithBrowserOption传递给NewContext的浏览器选项,以便以后在分配运行中的浏览器时使用它们。

cancel func()

cancel simply cancels the context that was used to start Browser.This is userful to stop all activity and avoid deadlocks if we detect that the browser was closed or happend to crash.Note that this cancel function doesn’t do any waiting.
cancel只是取消用于启动浏览器的上下文。当我们检测到浏览器关闭或崩溃时,这可以用来停止所有活动并避免死锁。注意,这个cancel函数不做任何等待。

first bool

// first records whether this context created a brand new Chrome process. This is important, because its cancellation should stop the ntire browser and its handler, and not just a portion of its pages.
first记录这个上下文是否创建了一个全新的Chrome进程。这一点很重要,因为取消它应该会停止整个浏览器及其处理程序,而不仅仅是页面的一部分。

closedTarget sync.WaitGroup

// closedTarget allows waiting for a target’s page to be closed on cancellation.
closedTarget允许在取消时等待目标页面被关闭。

allocated chan struct{}

// allocated is closed when an allocated browser completely stops. If no browser needs to be allocated, the channel is simply not initialised and remains nil.
当已分配的浏览器完全停止时,allocated将关闭。如果不需要分配浏览器,通道就不会被初始化并保持为空。

cancelErr error

// cancelErr is the first error encountered when cancelling this context, for example if a browser’s temporary user data directory couldn’t be deleted.
cancelErr是取消此上下文时遇到的第一个错误,例如,如果浏览器的临时用户数据目录无法删除。

func NewContext()

// NewContext creates a chromedp context from the parent context. The parent context’s Allocator is inherited, defaulting to an ExecAllocator with DefaultExecAllocatorOptions.// If the parent context contains an allocated Browser, the child context inherits it, and its first Run creates a new tab on that browser. Otherwise, its first Run will allocate a new browser.// Cancelling the returned context will close a tab or an entire browser, depending on the logic described above. To cancel a context while checking for errors, see Cancel.// Note that NewContext doesn’t allocate nor start a browser; that happens the first time Run is used on the context. first time Run is used on the context.

  • NewContext 从父上下文创建一个 chromedp上下文。父上下文被继承,默认 一个 DefaultExecAllocatorOption 的 ExecAllocator。
  • 如果父上下文包含一个分配的浏览器,子上下文继承它,且它第一次运行在浏览器上创建一个新标签。否则,它第一次运行将分配一个新浏览器。
  • 取消返回的上文将关闭一个标签或整个浏览器,根据以上逻辑描述。若要在检查错误时取消上下文,请参见取消。
  • 注意,NewContext既不分配也不启动浏览器;第一次在上下文上使用Run时会发生这种情况。


FromContext(ctx context.Context) *Context{}

FromContext extracts the Context data stored inside a context.Context.
FromContext提取存储在Context. Context中的上下文数据。

func Cancel(ctx context.Context) error{}

Cancel cancels a chromedp context, waits for its resources to be cleaned up, and returns any error encountered during that process.
Cancel取消chromedp上下文,等待其资源被清理,并返回在该过程中遇到的任何错误.

If the context allocated a browser, the browser will be closed gracefully by Cancel. A timeout can be attached to this context to determine how long to wait for the browser to close itself:
如果上下文分配了一个浏览器,浏览器将通过取消优雅地关闭。超时可以附加到这个上下文,以确定浏览器关闭自己需要等待多长时间:

  1. tctx, tcancel := context.WithTimeout(ctx, 10 * time.Second)
  2. defer tcancel()
  3. chromedp.Cancel(tctx)

Usually a “defer cancel()” will be enough for most use cases. However, Cancel is the better option if one wants to gracefully close a browser, or catch nderlying errors happening during cancellation.
通常一个“defer cancel()”对于大多数用例就足够了。然而,如果想要优雅地关闭浏览器,或者捕捉取消过程中发生的底层错误,那么Cancel是更好的选择。

func Run(ctx context.Context, actions …Action) error {}

Run runs an action against context. The provided context must be a valid chromedp context, typically created via NewContext.
Run针对上下文运行一个操作。提供的上下文必须是有效的chromedp上下文,通常通过NewContext创建。
Note that the first time Run is called on a context, a browser will be allocated via Allocator. Thus, it’s generally a bad idea to use a context timeout on the first Run call, as it will stop the entire browser.
注意,第一次运行是在上下文上调用的,浏览器将通过Allocator分配。因此,在第一次运行调用时使用上下文超时通常不是一个好主意,因为它将停止整个浏览器

type ContextOption = func(*Context)

ContextOption is a context option.
ContextOption是一个上下文选项。

func WithTargetID(id target.ID) ContextOption{}

WithTargetID sets up a context to be attached to an existing target, instead of creating a new one.
WithTargetID设置一个上下文来附加到现有的目标,而不是创建一个新的目标。

func WithLogf(f func(string, …interface{})) ContextOption {}

WithLogf is a shortcut for WithBrowserOption(WithBrowserLogf(f)).
WithLogf是WithBrowserOption(WithBrowserLogf(f))的快捷方式。

func WithErrorf(f func(string, …interface{})) ContextOption {}

WithErrorf is a shortcut for WithBrowserOption(WithBrowserErrorf(f)).
WithErrorf是WithBrowserOption(WithBrowserErrorf(f))的快捷方式。

func WithDebugf(f func(string, …interface{})) ContextOption {}

WithDebugf is a shortcut for WithBrowserOption(WithBrowserDebugf(f)).
WithDebugf是WithBrowserOption(WithBrowserDebugf(f))的快捷方式。

func WithBrowserOption(opts …BrowserOption) ContextOption {}

WithBrowserOption allows passing a number of browser options to the allocator when allocating a new browser.
WithBrowserOption允许在分配新浏览器时向分配器传递许多浏览器选项。
As such, this context option can only be used when NewContext is allocating a new browser.
因此,这个上下文选项只能在NewContext分配新浏览器时使用。