{"id":13413933,"url":"https://github.com/mafredri/cdp","last_synced_at":"2025-05-14T16:02:01.743Z","repository":{"id":19263719,"uuid":"84717184","full_name":"mafredri/cdp","owner":"mafredri","description":"Package cdp provides type-safe bindings for the Chrome DevTools Protocol (CDP), written in the Go programming language.","archived":false,"fork":false,"pushed_at":"2024-11-09T20:20:46.000Z","size":2695,"stargazers_count":751,"open_issues_count":17,"forks_count":45,"subscribers_count":18,"default_branch":"main","last_synced_at":"2025-04-12T01:53:29.705Z","etag":null,"topics":["cdp","chrome-debugging-protocol","chrome-devtools-protocol","debugging","devtools-protocol","rpc"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mafredri.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-03-12T10:25:41.000Z","updated_at":"2025-04-08T05:00:38.000Z","dependencies_parsed_at":"2024-11-08T20:23:04.022Z","dependency_job_id":"f87ec8cd-1bc7-443e-8462-b260eede3dd7","html_url":"https://github.com/mafredri/cdp","commit_stats":{"total_commits":373,"total_committers":10,"mean_commits":37.3,"dds":0.02680965147453085,"last_synced_commit":"e5a75ebc0b46b7dbf5553fc2ff6f2b11754d65ea"},"previous_names":[],"tags_count":75,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mafredri%2Fcdp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mafredri%2Fcdp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mafredri%2Fcdp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mafredri%2Fcdp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mafredri","download_url":"https://codeload.github.com/mafredri/cdp/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248505873,"owners_count":21115354,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cdp","chrome-debugging-protocol","chrome-devtools-protocol","debugging","devtools-protocol","rpc"],"created_at":"2024-07-30T20:01:53.016Z","updated_at":"2025-04-12T01:53:37.298Z","avatar_url":"https://github.com/mafredri.png","language":"Go","readme":"# cdp\n\n[![Coverage Status][codecov-badge]][codecov] [![Go Report Card][goreportcard-badge]][goreportcard] [![GoDoc][gopkg-badge]][gopkg]\n\nPackage `cdp` provides type-safe bindings for the [Chrome DevTools Protocol][chrome-devtools-protocol] (CDP), written in the Go programming language. The bindings are generated (by [cdpgen][cdpgen]) from the latest [tip-of-tree (tot)][tip-of-tree] protocol definitions and are mainly intended for use with Google Chrome or Chromium, however, they can be used with any debug target ([Node.js][node-debugging], [Edge DevTools Protocol][edge-devtools-protocol], [Safari][ios-webkit-debug-proxy], etc.) that implement the protocol.\n\nThis package can be used for any kind of browser automation, scripting or debugging via the Chrome DevTools Protocol.\n\nA big motivation for `cdp` is to expose the full functionality of the Chrome DevTools Protocol and provide it in a discoverable and self-documenting manner.\n\nProviding high-level browser automation is a non-goal for this project. That being said, `cdp` hopes to improve the ergonomics of working with the protocol by providing primitives better suited for Go and automating repetitive tasks.\n\n## Features\n\n* Discoverable API for the Chrome DevTools Protocol (GoDoc, autocomplete friendly)\n* Contexts as a first-class citizen (for timeouts and cancellation)\n* Simple and synchronous event handling (no callbacks)\n* Concurrently safe\n* No silent or hidden errors\n* Do what the user expects\n* Match CDP types to Go types wherever possible\n* Separation of concerns (avoid mixing CDP and RPC)\n\n## Installation\n\n```console\n$ go get -u github.com/mafredri/cdp\n```\n\n## Documentation\n\nSee [API documentation][gopkg] for package, API descriptions and examples. Examples can also be found in this repository, see the [simple][simple-example], [advanced][advanced-example], [logging][logging-example] and [incognito][incognito-example] examples.\n\n## Usage\n\nThe main packages are `cdp` and `rpcc`, the former provides the CDP bindings and the latter handles the RPC communication with the debugging target.\n\nTo connect to a debug target, a WebSocket debugger URL is needed. For example, if Chrome is running with `--remote-debugging-port=9222` the debugger URL can be found at [localhost:9222/json](http://localhost:9222/json). The `devtool` package can also be used to query the DevTools JSON API (see example below).\n\nHere is an example of using `cdp`:\n\n[embedmd]:# (example_test.go)\n```go\npackage main\n\nimport (\n\t\"bufio\"\n\t\"context\"\n\t\"fmt\"\n\t\"io/ioutil\"\n\t\"log\"\n\t\"os\"\n\t\"time\"\n\n\t\"github.com/mafredri/cdp\"\n\t\"github.com/mafredri/cdp/devtool\"\n\t\"github.com/mafredri/cdp/protocol/dom\"\n\t\"github.com/mafredri/cdp/protocol/page\"\n\t\"github.com/mafredri/cdp/rpcc\"\n)\n\nfunc main() {\n\terr := run(5 * time.Second)\n\tif err != nil {\n\t\tlog.Fatal(err)\n\t}\n}\n\nfunc run(timeout time.Duration) error {\n\tctx, cancel := context.WithTimeout(context.Background(), timeout)\n\tdefer cancel()\n\n\t// Use the DevTools HTTP/JSON API to manage targets (e.g. pages, webworkers).\n\tdevt := devtool.New(\"http://127.0.0.1:9222\")\n\tpt, err := devt.Get(ctx, devtool.Page)\n\tif err != nil {\n\t\tpt, err = devt.Create(ctx)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t}\n\n\t// Initiate a new RPC connection to the Chrome DevTools Protocol target.\n\tconn, err := rpcc.DialContext(ctx, pt.WebSocketDebuggerURL)\n\tif err != nil {\n\t\treturn err\n\t}\n\tdefer conn.Close() // Leaving connections open will leak memory.\n\n\tc := cdp.NewClient(conn)\n\n\t// Open a DOMContentEventFired client to buffer this event.\n\tdomContent, err := c.Page.DOMContentEventFired(ctx)\n\tif err != nil {\n\t\treturn err\n\t}\n\tdefer domContent.Close()\n\n\t// Enable events on the Page domain, it's often preferrable to create\n\t// event clients before enabling events so that we don't miss any.\n\tif err = c.Page.Enable(ctx); err != nil {\n\t\treturn err\n\t}\n\n\t// Create the Navigate arguments with the optional Referrer field set.\n\tnavArgs := page.NewNavigateArgs(\"https://www.google.com\").\n\t\tSetReferrer(\"https://duckduckgo.com\")\n\tnav, err := c.Page.Navigate(ctx, navArgs)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\t// Wait until we have a DOMContentEventFired event.\n\tif _, err = domContent.Recv(); err != nil {\n\t\treturn err\n\t}\n\n\tfmt.Printf(\"Page loaded with frame ID: %s\\n\", nav.FrameID)\n\n\t// Fetch the document root node. We can pass nil here\n\t// since this method only takes optional arguments.\n\tdoc, err := c.DOM.GetDocument(ctx, nil)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\t// Get the outer HTML for the page.\n\tresult, err := c.DOM.GetOuterHTML(ctx, \u0026dom.GetOuterHTMLArgs{\n\t\tNodeID: \u0026doc.Root.NodeID,\n\t})\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tfmt.Printf(\"HTML: %s\\n\", result.OuterHTML)\n\n\t// Capture a screenshot of the current page.\n\tscreenshotName := \"screenshot.jpg\"\n\tscreenshotArgs := page.NewCaptureScreenshotArgs().\n\t\tSetFormat(\"jpeg\").\n\t\tSetQuality(80)\n\tscreenshot, err := c.Page.CaptureScreenshot(ctx, screenshotArgs)\n\tif err != nil {\n\t\treturn err\n\t}\n\tif err = ioutil.WriteFile(screenshotName, screenshot.Data, 0o644); err != nil {\n\t\treturn err\n\t}\n\n\tfmt.Printf(\"Saved screenshot: %s\\n\", screenshotName)\n\n\tpdfName := \"page.pdf\"\n\tf, err := os.Create(pdfName)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tpdfArgs := page.NewPrintToPDFArgs().\n\t\tSetTransferMode(\"ReturnAsStream\") // Request stream.\n\tpdfData, err := c.Page.PrintToPDF(ctx, pdfArgs)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tsr := c.NewIOStreamReader(ctx, *pdfData.Stream)\n\tr := bufio.NewReader(sr)\n\n\t// Write to file in ~r.Size() chunks.\n\t_, err = r.WriteTo(f)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\terr = f.Close()\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tfmt.Printf(\"Saved PDF: %s\\n\", pdfName)\n\n\treturn nil\n}\n```\n\nFor more information, consult the [documentation](#documentation).\n\n## Acknowledgements\n\nThe Go implementation of gRPC ([grpc-go](https://github.com/grpc/grpc-go)) has been a source of inspiration for some of the design decisions made in the `cdp` and `rpcc` packages. Some ideas have also been borrowed from the `net/rpc` package from the standard library.\n\n## Resources\n\n* [Chrome DevTools Protocol][chrome-devtools-protocol]\n    * [Viewer (latest tip-of-tree)][tip-of-tree] official protocol API docs\n    * [Repository (GitHub)](https://github.com/chromedevtools/devtools-protocol) please [file issues](https://github.com/ChromeDevTools/devtools-protocol/issues) at this repo if you have concerns or problems with the Chrome DevTools Protocol\n    * [Mailing list](https://groups.google.com/forum/#!forum/chrome-debugging-protocol)\n* [Getting Started with Headless Chrome](https://developers.google.com/web/updates/2017/04/headless-chrome)\n* [Awesome chrome-devtools: Chrome DevTools Protocol](https://github.com/ChromeDevTools/awesome-chrome-devtools#chrome-devtools-protocol)\n* [RemoteDebug](http://remotedebug.org/) is an initiative to bring remote debugging (e.g. CDP) to all modern browsers\n    * [Protocol Compatibility Tables](https://compatibility.remotedebug.org/)\n\n[cdpgen]: https://github.com/mafredri/cdp/tree/master/cmd/cdpgen\n[simple-example]: https://github.com/mafredri/cdp/blob/master/example_test.go\n[advanced-example]: https://github.com/mafredri/cdp/blob/master/example_advanced_test.go\n[logging-example]: https://github.com/mafredri/cdp/blob/master/example_logging_test.go\n[incognito-example]: https://github.com/mafredri/cdp/blob/master/example_incognito_test.go\n\n[chrome-devtools-protocol]: https://chromedevtools.github.io/devtools-protocol/\n[tip-of-tree]: https://chromedevtools.github.io/devtools-protocol/tot/\n[node-debugging]: https://nodejs.org/en/docs/guides/debugging-getting-started/\n[edge-devtools-protocol]: https://docs.microsoft.com/en-us/microsoft-edge/devtools-protocol/\n[ios-webkit-debug-proxy]: https://github.com/google/ios-webkit-debug-proxy\n\n[travis]: https://travis-ci.org/mafredri/cdp\n[travis-badge]: https://travis-ci.org/mafredri/cdp.svg\n[codecov]: https://codecov.io/gh/mafredri/cdp\n[codecov-badge]: https://codecov.io/gh/mafredri/cdp/branch/master/graph/badge.svg\n[goreportcard]: https://goreportcard.com/report/github.com/mafredri/cdp\n[goreportcard-badge]: https://goreportcard.com/badge/github.com/mafredri/cdp\n[gopkg]: https://pkg.go.dev/github.com/mafredri/cdp\n[gopkg-badge]: https://pkg.go.dev/badge/mafredri/cdp\n","funding_links":[],"categories":["Testing","Go","測試","Template Engines","测试","Chrome DevTools Protocol","\u003cspan id=\"测试-testing\"\u003e测试 Testing\u003c/span\u003e","Mock","Selenium and browser control tools","测试相关`测试库和测试数据集生成库`","测试相关"],"sub_categories":["Advanced Console UIs","HTTP Clients","Selenium and browser control tools","Selenium and browser control tools.","高級控制台界面","HTTP客户端","Libraries for driving the protocol (or a layer above)","\u003cspan id=\"高级控制台用户界面-advanced-console-uis\"\u003e高级控制台用户界面 Advanced Console UIs\u003c/span\u003e","交流","Mock","查询语","高级控制台界面"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmafredri%2Fcdp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmafredri%2Fcdp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmafredri%2Fcdp/lists"}