mkenney/go-chrome

events don't return any values

Closed this issue · 4 comments

What version of go-chrome are you using (tagged commit, commit hash, or master)?

master at 64a3e5d

What behavior do you expect? What are you trying to accomplish?

Tried to get information about Requests and Frames in Event Handlers (Network().OnRequestWillBeSent and Page().OnFrameNavigated)

What behavior are you experiencing instead?

The *page.FrameNavigatedEvent and *network.RequestWillBeSentEvent were empty, meaning all their fields are set to the nil values.

How can this behavior be reproduced?

package main

import (
	"fmt"
	"os"
	"time"

	"github.com/mkenney/go-chrome/tot"
	"github.com/mkenney/go-chrome/tot/cdtp/network"
	"github.com/mkenney/go-chrome/tot/cdtp/page"
	"github.com/sanity-io/litter"
)

var flags = &chrome.Flags{
	"remote-debugging-port": 9222,

	// Puppeteer - Headless Mode
	"headless":        nil,
	"disable-gpu":     nil,
	"hide-scrollbars": nil,
	"mute-audio":      nil,

	// Puppeteer - Default Args
	"disable-background-networking":          nil,
	"disable-background-timer-throttling":    nil,
	"disable-client-side-phishing-detection": nil,
	"disable-default-apps":                   nil,
	"disable-dev-shm-usage":                  nil,
	"disable-extensions":                     nil,
	"disable-hang-monitor":                   nil,
	"disable-popup-blocking":                 nil,
	"disable-prompt-on-repost":               nil,
	"disable-sync":                           nil,
	"disable-translate":                      nil,
	"metrics-recording-only":                 nil,
	"no-first-run":                           nil,
	"safebrowsing-disable-auto-update":       nil,

	// Puppeteer - Automation Args
	"enable-automation": nil,
	"password-store":    "basic",
	"use-mock-keychain": nil,

	// From alpeware/chrome-headless-trunk (docker image) /usr/bin/start.sh
	"no-sandbox":    nil,
	"user-data-dir": os.TempDir(),
}

func main() {
	browser := chrome.New(flags, `/usr/bin/google-chrome`, os.TempDir(), "", "")
	if err := browser.Launch(); nil != err {
		panic(err)
	}

	defer func() {
		if err := browser.Close(); err != nil {
			fmt.Println(err)
		}
	}()

	t, err := browser.NewTab("")
	if err != nil {
		panic(err)
	}

	if enableResult := <-t.Page().Enable(); enableResult.Err != nil {
		panic(enableResult.Err)
	}

	if enableResult := <-t.Network().Enable(&network.EnableParams{}); enableResult.Err != nil {
		panic(enableResult.Err)
	}

	t.Page().OnFrameNavigated(func(event *page.FrameNavigatedEvent) {
		litter.Dump(event)
	})

	t.Network().OnRequestWillBeSent(func(event *network.RequestWillBeSentEvent) {
		litter.Dump(event)
	})

	if res := <-t.Page().Navigate(&page.NavigateParams{URL: "https://www.google.com"}); res.Err != nil {
		panic(res.Err)
	}

	time.Sleep(time.Second * 3)
}

@destructive-dragon thank you for reporting that!

It looks like I didn't catch that the devtools protocol developers made the odd choice of returning event data in the params key instead of the result key, though I suppose I can see the logic:

{
    "method": "Page.frameNavigated",
    "params": {
        "frame": {
            "id": "E324DBD95C562C6FFE95EE3C78050FEB",
            "loaderId": "DDC6163186E7C11BAF3B7E8937D77AE4",
            "mimeType": "text/html",
            "securityOrigin": "https://www.google.com",
            "url": "https://www.google.com/"
        }
    }
}

I'll put up a patch shortly to address that.

@destructive-dragon I believe thats fixed and your test script looks like it's behaving as expected, do you mind checking out the issue-94 branch and confirming that it solves your issue?

Yes, thanks! That resolved this issue!

Merged, thanks!