Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can I check Internet activity #986

Closed
duolabmeng6 opened this issue Dec 1, 2023 · 3 comments
Closed

Can I check Internet activity #986

duolabmeng6 opened this issue Dec 1, 2023 · 3 comments
Labels
question Questions related to rod

Comments

@duolabmeng6
Copy link

Since I didn't find an example of blocking network requests in the example, let me ask that I want to see all network activity in the browser

@duolabmeng6 duolabmeng6 added the question Questions related to rod label Dec 1, 2023
@duolabmeng6 duolabmeng6 changed the title Can I block network requests Can I check Internet activity Dec 1, 2023
Copy link

github-actions bot commented Dec 1, 2023

Please add a valid Rod Version: v0.0.0 to your issue. Current version is v0.114.5

generated by check-issue

@duolabmeng6
Copy link
Author

package main

import (
	"fmt"
	"github.com/go-rod/rod"
	"github.com/go-rod/rod/lib/launcher"
	"github.com/go-rod/rod/lib/proto"
	"time"
)

func main() {
	url := launcher.New().Headless(false).MustLaunch()
	browser := rod.New().ControlURL(url).MustConnect()
	page := browser.MustPage("")

	go func() {
		page.EachEvent(func(e *proto.NetworkRequestWillBeSent) {
			fmt.Printf("Request: %s %s\n", e.Request.Method, e.Request.URL)
		}, func(r *proto.NetworkResponseReceived) {
			//How do I get the contents of the body here
			//m := proto.NetworkGetResponseBody{RequestID: r.RequestID}
			//r, _ = m.Call(page)

		})()
	}()
	time.Sleep(3 * time.Second)

	page.MustNavigate("https://www.baidu.com")

	select {}
}

How do I get the contents of the body here

@duolabmeng6
Copy link
Author

I hope it helps others

package main

import (
	"fmt"
	"github.com/go-rod/rod"
	"github.com/go-rod/rod/lib/launcher"
	"github.com/go-rod/rod/lib/proto"
	"time"
)

func main() {
	url := launcher.New().Headless(false).MustLaunch()
	browser := rod.New().ControlURL(url).MustConnect()
	page := browser.MustPage("")

	go func() {
		page.EachEvent(func(e *proto.NetworkRequestWillBeSent) {
			fmt.Printf("Request: %s %s\n", e.Request.Method, e.Request.URL)
		}, func(e *proto.NetworkResponseReceived) {
			reply, err := (proto.NetworkGetResponseBody{RequestID: e.RequestID}).Call(page)
			if err != nil {
				fmt.Println(err)
			}
			fmt.Println(reply.Body)
		})()
	}()
	time.Sleep(3 * time.Second)
	page.MustNavigate("https://www.baidu.com")
	select {}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Questions related to rod
Projects
None yet
Development

No branches or pull requests

1 participant