GithubHelp home page GithubHelp logo

Friendly API to hijack requests about rod HOT 3 CLOSED

go-rod avatar go-rod commented on July 18, 2024
Friendly API to hijack requests

from rod.

Comments (3)

ysmood avatar ysmood commented on July 18, 2024 1

The final design I think will be like:

func main () {
	router := browser.HijackRequests("a.com", "b.com")
	defer router.Stop()

	router.Host("a.com").
	GET("/user/:id", func(ctx rod.ProxyContext) {
		if ctx.Param("id") != "abc" {
			ctx.Next()
			return
		}

		ctx.JSON(mock_user_data)
	}).
	DELETE("/user/:id", func(ctx rod.ProxyContext) {
		ctx.Write(200, "OK")
	})

	router.POST("/upload", func(ctx rod.ProxyContext) {
		fmt.Println(ctx.Form())
		ctx.Next()
	})

	go router.Run()
}

I will try to use https://github.com/gin-gonic/gin directly if it's not too much work.

from rod.

NormalPunch avatar NormalPunch commented on July 18, 2024
	browser.Hijack(`https://*/*`, func(ctx Context) (pass_to_browser bool) {
		// deal with specific page
		if Equal(ctx.Page, browser.Pages()[0]) {
			switch ctx.Request.Host {
			case "api.example.com":
				{
					switch ctx.Request.Path {
					case "/status":
						{
							// mock
							ctx.JSON(200, rod.H{
								"message": "ok",
							})
							return false
						}
					case "/check":
						{
							// modify a header and pass to browser to continue the request
							ctx.Request.Header.Set("Token", "my token")
							return true
						}
					}
				}
			case "npmjs.com":
				{
					// modify the host and pass to browser to continue the request
					ctx.Request.Host = "cdn.jsdelivr.net"
					return true
				}
			}
		}

		return true
	})

I don't know if this is possible with CDP but I think hijack api like this will be very useful and, powerful.

from rod.

ysmood avatar ysmood commented on July 18, 2024

Usually, we don't design like pass_to_browser, we use middleware like interface:

go browser.Hijack("/user/:id", func(ctx rod.ProxyContext) (stop bool) {
    ctx.Next()
    return false
})

from rod.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.