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

is it possible to use chromedp for node js debugging? #913

Closed
poonai opened this issue Sep 18, 2021 · 6 comments
Closed

is it possible to use chromedp for node js debugging? #913

poonai opened this issue Sep 18, 2021 · 6 comments

Comments

@poonai
Copy link

poonai commented Sep 18, 2021

I want to hit nodejs debugger api using chromedp.

Is it possible to use chromedp since nodejs also exposing chrome dev tool protocol
https://nodejs.org/api/debugger.html#debugger_advanced_usage

@ZekeLu
Copy link
Member

ZekeLu commented Sep 18, 2021

No, chromedp does not work with Node.js debugger.

First, Node.js uses URL like ws://127.0.0.1:9229/48a5b28a-550c-471b-b5e1-d13dd7165df9, which is considered as invalid because it does not contain /devtools/browser/. chromedp will try to detect the URL from http://127.0.0.1:9229/json/version, but the response is something like this:

{
  "Browser": "node.js/v14.17.6",
  "Protocol-Version": "1.1"
}

Which does not contain the expected WebSocket URL.

This logic was introduced by chromedp@v.0.7.3. See #817. I have tried chromedp@v.0.7.2, but it still does not work.

Try 1 with chromedp@v.0.7.2:

package main

import (
	"context"
	"log"

	"github.com/chromedp/chromedp"
)

func main() {
	actx, cancel := chromedp.NewRemoteAllocator(context.Background(),
		"ws://127.0.0.1:9229/21e0413d-d55b-44dc-a96e-a3197f9d16c8",
	)
	defer cancel()

	ctx, cancel := chromedp.NewContext(actx, chromedp.WithDebugf(log.Printf))
	defer cancel()

	if err := chromedp.Run(ctx); err != nil {
		log.Fatal(err)
	}
}

Output:

2021/09/18 23:39:19 -> {"id":1,"method":"Target.createTarget","params":{"url":"about:blank"}}
2021/09/18 23:39:19 <- {"error":{"code":-32601,"message":"'Target.createTarget' wasn't found"},"id":1}
2021/09/18 23:39:19 'Target.createTarget' wasn't found (-32601)

Try 2 with chromedp@v.0.7.2:

package main

import (
	"context"
	"log"

	"github.com/chromedp/chromedp"
)

func main() {
	actx, cancel := chromedp.NewRemoteAllocator(context.Background(),
		"ws://127.0.0.1:9229/21e0413d-d55b-44dc-a96e-a3197f9d16c8",
	)
	defer cancel()

	ctx, cancel := chromedp.NewContext(actx, chromedp.WithDebugf(log.Printf))
	defer cancel()

	targets, err := chromedp.Targets(ctx)
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("%+v", targets)
}

Output:

2021/09/18 23:38:17 -> {"id":1,"method":"Target.getTargets"}
2021/09/18 23:38:17 <- {"error":{"code":-32601,"message":"'Target.getTargets' wasn't found"},"id":1}
2021/09/18 23:38:17 'Target.getTargets' wasn't found (-32601)

@poonai
Copy link
Author

poonai commented Sep 19, 2021

Ahh, Thanks for the detailed answer. I guess, I just have to write my own client.

It's awesome that you folks separated generated types from https://github.com/chromedp/cdproto chromedp. It'll be even more easy to write the client.

@ZekeLu
Copy link
Member

ZekeLu commented Sep 19, 2021

Try 3 with chromedp@v.0.7.2:

It seems that we can work with the debugger domain like this. I'm not sure whether this is what you want, but it will give you some clues.

package main

import (
	"context"
	"log"

	"github.com/chromedp/cdproto/cdp"
	"github.com/chromedp/cdproto/debugger"
	"github.com/chromedp/chromedp"
)

func main() {
	actx, cancel := chromedp.NewRemoteAllocator(context.Background(),
		"ws://127.0.0.1:9229/21e0413d-d55b-44dc-a96e-a3197f9d16c8",
	)
	defer cancel()

	ctx, cancel := chromedp.NewContext(actx, chromedp.WithDebugf(log.Printf))
	defer cancel()

	if err := run(ctx,
		chromedp.ActionFunc(func(ctx context.Context) error {
			_, err := debugger.Enable().Do(ctx)
			return err
		}),
		debugger.SetBreakpointsActive(false),
	); err != nil {
		log.Fatal(err)
	}
}

func run(ctx context.Context, actions ...chromedp.Action) error {
	// to make sure chromedp.initContextBrowser is called
	if err := chromedp.Run(ctx); err != nil && err.Error() != "'Target.createTarget' wasn't found (-32601)" {
		return err
	}
	c := chromedp.FromContext(ctx)
	// to run actions with the Browser executor
	// chromedp.Run will run actions with the Target executor
	return chromedp.Tasks(actions).Do(cdp.WithExecutor(ctx, c.Browser))
}

@poonai
Copy link
Author

poonai commented Sep 20, 2021

I tried with it. But I got error

2021/09/20 13:17:29 could not dial "ws://127.0.0.1:9229/a8fa298b-20eb-418f-bd73-0a06a45fa4a6": handshake error: bad HTTP protocol version

Btw, does chromedp handles event from the remote server eg event https://chromedevtools.github.io/devtools-protocol/tot/Debugger/#event-breakpointResolved

@ZekeLu
Copy link
Member

ZekeLu commented Sep 20, 2021

For event handling, please see https://pkg.go.dev/github.com/chromedp/chromedp#ListenBrowser and https://pkg.go.dev/github.com/chromedp/chromedp#ListenTarget.

For the bad HTTP protocol version issue, can you describe the steps (including the version of the Node.js) to reproduce it?

@ZekeLu
Copy link
Member

ZekeLu commented Feb 6, 2022

Closing due to lack of activity.

@ZekeLu ZekeLu closed this as completed Feb 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants