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

Navigate Hangup with custom url scheme #1448

Open
acfasj opened this issue Mar 19, 2024 · 1 comment
Open

Navigate Hangup with custom url scheme #1448

acfasj opened this issue Mar 19, 2024 · 1 comment

Comments

@acfasj
Copy link

acfasj commented Mar 19, 2024

What versions are you running?

$ go list -m github.com/chromedp/chromedp
github.com/chromedp/chromedp v0.9.5
$ google-chrome --version
Version 122.0.6261.129 (Official Build) (x86_64)
$ go version
go version go1.22.0 darwin/amd64

What did you do? Include clear steps.

  1. Run the code below, the process hangup
package main

import (
	"context"
	"fmt"
	"log"

	"github.com/chromedp/chromedp"
)

func main() {
	opts := append(chromedp.DefaultExecAllocatorOptions[:],
		chromedp.Flag("headless", false),
	)

	allocCtx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
	defer cancel()

	ctx, cancel := chromedp.NewContext(allocCtx)
	defer cancel()

	fmt.Println("Start running")

	err := chromedp.Run(ctx,
		chromedp.Navigate("https://wa.me/8615889343521"),
	)
	if err != nil {
		log.Fatal(err)
	}


	fmt.Println("End running =====>")
}

This is because of the script in side the page try to do location.href = 'whatsapp://send/?phone=8615889343521&text&type=phone_number&app_absent=0';
which leads to something bad happen
image

Can replace chromedp.Navigate("https://wa.me/8615889343521") above with your own local server with the html file below:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <body>
      <h1>TEST</h1>
    </body>
    <script>
      location.href =
        'whatsapp://send/?phone=8615889343521&text&type=phone_number&app_absent=0';
    </script>
  </body>
</html>

What did you expect to see?

The process exit, no hangup, can see End running =====> in console

What did you see instead?

The process hangup, never stop

@acfasj
Copy link
Author

acfasj commented Mar 19, 2024

I believe this was caused by loaderID never change:

chromedp/chromedp.go

Lines 630 to 634 in ebf842c

ListenTarget(lctx, func(ev interface{}) {
if loaderID != "" {
handleEvent(ev)
return
}

And location.href = 'whatsapp://send/?phone=8615889343521&text&type=phone_number&app_absent=0'; will make Chrome create a new loaderID, then in handleEvent, we have no way to match the loaderID, so it block forever

chromedp/chromedp.go

Lines 587 to 619 in ebf842c

handleEvent := func(ev interface{}) {
switch ev := ev.(type) {
case *network.EventRequestWillBeSent:
if ev.LoaderID == loaderID && ev.Type == network.ResourceTypeDocument {
reqID = ev.RequestID
}
case *network.EventLoadingFailed:
if ev.RequestID == reqID {
loadErr = fmt.Errorf("page load error %s", ev.ErrorText)
// If Canceled is true, we won't receive a
// loadEventFired at all.
if ev.Canceled {
finished = true
lcancel()
}
}
case *network.EventResponseReceived:
if ev.RequestID == reqID && resp != nil {
*resp = ev.Response
}
case *page.EventLifecycleEvent:
if ev.FrameID == frameID && ev.Name == "init" {
hasInit = true
}
case *page.EventLoadEventFired:
// Ignore load events before the "init"
// lifecycle event, as those are old.
if hasInit {
finished = true
lcancel()
}
}
}

acfasj added a commit to acfasj/chromedp that referenced this issue Mar 19, 2024
acfasj added a commit to acfasj/chromedp that referenced this issue Mar 19, 2024
acfasj added a commit to acfasj/chromedp that referenced this issue Mar 22, 2024
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

1 participant