From 574263dac65a9260debfdd76a50f32abb18342af Mon Sep 17 00:00:00 2001 From: walker <13750528578@163.com> Date: Fri, 12 Jun 2026 10:53:37 +0800 Subject: [PATCH] =?UTF-8?q?fix(xhs):=20=E8=AE=A9=E8=AF=9D=E9=A2=98?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0=E5=88=B0=E6=AD=A3=E6=96=87=E6=9C=AB=E5=B0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/xhs/publisher.go | 25 +++++-- internal/xhs/publisher_test.go | 124 +++++++++++++++++++++++++++++++++ 2 files changed, 145 insertions(+), 4 deletions(-) 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 := ` + +
+ +