Skip to content

Commit

Permalink
Fixed some chatgpt buttons
Browse files Browse the repository at this point in the history
Send button query path differs when image upload is enabled.
Regenerate and continue buttons texts have changed.
  • Loading branch information
igolaizola committed Oct 15, 2023
1 parent b4d0231 commit 588e294
Showing 1 changed file with 28 additions and 11 deletions.
39 changes: 28 additions & 11 deletions pkg/chatgpt/chatgpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,12 @@ var errTooManyRequests = errors.New("chatgpt: too many requests")
var editMessageRegex = regexp.MustCompile(`^!(\d+)?(.*)`)

func (r *rw) sendMessage(msg string) error {
sendButton := "textarea + button"
sendButtons := []string{
// When upload image is disabled
"textarea + button",
// When upload image is enabled
"textarea + div + button",
}
sendButtonQuery := chromedp.ByQuery
want := 0

Expand Down Expand Up @@ -453,7 +458,7 @@ func (r *rw) sendMessage(msg string) error {
}

// Set send button path
sendButton = submitPath
sendButtons = []string{submitPath}
sendButtonQuery = chromedp.BySearch
want = editNum
}
Expand Down Expand Up @@ -537,11 +542,23 @@ func (r *rw) sendMessage(msg string) error {
// Click on the send button
d := time.Duration(200+rand.Intn(200)) * time.Millisecond
<-time.After(d)
if err := chromedp.Run(r.ctx,
chromedp.WaitVisible(sendButton, sendButtonQuery),
chromedp.Click(sendButton, sendButtonQuery),
); err != nil {
return fmt.Errorf("chatgpt: couldn't click button: %w", err)
ctx, cancel = context.WithCancel(r.ctx)
defer cancel()
for _, sendButton := range sendButtons {
sendButton := sendButton
go func() {
if err := chromedp.Run(ctx,
chromedp.WaitVisible(sendButton, sendButtonQuery),
chromedp.Click(sendButton, sendButtonQuery),
); err == nil {
cancel()
}
}()
}
select {
case <-ctx.Done():
case <-r.ctx.Done():
return fmt.Errorf("chatgpt: waiting for send button: %w", r.ctx.Err())
}

// Wait for the response
Expand All @@ -557,7 +574,7 @@ func (r *rw) sendMessage(msg string) error {
select {
case <-time.After(100 * time.Millisecond):
case <-r.ctx.Done():
return r.ctx.Err()
return fmt.Errorf("chatgpt: waiting for response: %w", r.ctx.Err())
}
}

Expand All @@ -566,7 +583,7 @@ func (r *rw) sendMessage(msg string) error {
select {
case <-time.After(100 * time.Millisecond):
case <-r.ctx.Done():
return r.ctx.Err()
return fmt.Errorf("chatgpt: waiting for the regeneration button: %w", r.ctx.Err())
}

// Obtain the html of the main div
Expand All @@ -585,10 +602,10 @@ func (r *rw) sendMessage(msg string) error {
var regenerateFound bool
var continueIndex int
doc.Find("form button").Each(func(i int, s *goquery.Selection) {
if strings.Contains(strings.ToLower(s.Text()), "continue generating") {
if strings.Contains(strings.ToLower(s.Text()), "continue") {
continueIndex = i + 1
}
if strings.Contains(strings.ToLower(s.Text()), "regenerate response") {
if strings.Contains(strings.ToLower(s.Text()), "regenerate") {
regenerateFound = true
}
})
Expand Down

0 comments on commit 588e294

Please sign in to comment.