diff --git a/internal/xhs/publisher.go b/internal/xhs/publisher.go index 986f6f6..a6402f3 100644 --- a/internal/xhs/publisher.go +++ b/internal/xhs/publisher.go @@ -176,6 +176,9 @@ func (p *rodPage) FillContent(ctx context.Context, content string, tags []string } } if text != "" && len(topicTags) > 0 { + if err := focusEditableAtEnd(field); err != nil { + return fmt.Errorf("focus editor: %w", err) + } if err := rodTry(func() { field.MustInput("\n") }); err != nil { @@ -190,12 +193,26 @@ func (p *rodPage) FillContent(ctx context.Context, content string, tags []string return nil } +func focusEditableAtEnd(field *rod.Element) error { + return rodTry(func() { + field.MustEval(`() => { + const editor = this; + editor.focus(); + const selection = window.getSelection(); + if (!selection) return; + const range = document.createRange(); + range.selectNodeContents(editor); + range.collapse(false); + selection.removeAllRanges(); + selection.addRange(range); + }`) + }) +} + func (p *rodPage) inputTopicByKeyboard(field *rod.Element, tag string) error { timeouts := p.effectiveTimeouts() - if err := rodTry(func() { - field.MustClick() - }); err != nil { - return fmt.Errorf("click editor: %w", err) + if err := focusEditableAtEnd(field); err != nil { + return fmt.Errorf("focus editor: %w", err) } if err := p.typeTopicTrigger(); err != nil { return fmt.Errorf("type topic trigger: %w", err) diff --git a/internal/xhs/publisher_test.go b/internal/xhs/publisher_test.go index 60ed2fe..cb95537 100644 --- a/internal/xhs/publisher_test.go +++ b/internal/xhs/publisher_test.go @@ -1724,6 +1724,130 @@ func TestFillContentAcceptsHighlightedTopicNode(t *testing.T) { } } +func TestFillContentAppendsTopicsAfterExistingText(t *testing.T) { + page := testPage(t) + + html := ` + +
+ +