From 1c793475a67b763855bb6dceb8bc917b9a86ec1e Mon Sep 17 00:00:00 2001 From: walker <13750528578@163.com> Date: Sun, 7 Jun 2026 23:02:18 +0800 Subject: [PATCH] =?UTF-8?q?fix(xhs):=20=E9=80=82=E9=85=8D=E8=87=AA?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E5=8F=91=E5=B8=83=E6=8C=89=E9=92=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/xhs/publisher.go | 43 ++++++++++++++++++++++++++++- internal/xhs/publisher_test.go | 50 ++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 1 deletion(-) diff --git a/internal/xhs/publisher.go b/internal/xhs/publisher.go index 896170e..b995896 100644 --- a/internal/xhs/publisher.go +++ b/internal/xhs/publisher.go @@ -375,16 +375,57 @@ func (p *rodPage) PublishOnlySelf(ctx context.Context, request PublishRequest) e } } defaultXHSLogger("only-self publish click submit") - clicked, err := p.clickByText("button", "^发布$") + clicked, err := p.clickOnlySelfPublishButton() if err != nil { return err } + if !clicked { + clicked, err = p.clickByText("button", "^发布$") + if err != nil { + return err + } + } if !clicked { return fmt.Errorf("only-self publish action not found") } return nil } +func (p *rodPage) clickOnlySelfPublishButton() (bool, error) { + clicked := false + err := rodTry(func() { + clicked = p.page.MustEval(`() => { + const isVisible = (node) => { + if (!node) return false; + const rect = node.getBoundingClientRect(); + const style = window.getComputedStyle(node); + return rect.width > 0 && rect.height > 0 && style.display !== 'none' && style.visibility !== 'hidden'; + }; + const fire = (node, type) => node.dispatchEvent(new MouseEvent(type, { bubbles: true, cancelable: true, view: window, button: 0 })); + const nodes = Array.from(document.querySelectorAll('xhs-publish-btn[is-publish="true"][submit-disabled="false"]')); + for (const node of nodes) { + if (!isVisible(node)) continue; + const submitText = (node.getAttribute('submit-text') || '').replace(/\s+/g, ' ').trim(); + const text = (node.innerText || node.textContent || '').replace(/\s+/g, ' ').trim(); + if (submitText !== '发布' && text !== '发布') continue; + node.scrollIntoView({ block: 'center' }); + fire(node, 'mousedown'); + fire(node, 'mouseup'); + node.click(); + return true; + } + return false; + }`).Bool() + if clicked { + p.page.MustWaitStable() + } + }) + if err != nil { + return false, err + } + return clicked, nil +} + func (p *rodPage) ConfirmOnlySelfPublished(ctx context.Context) error { if err := ctx.Err(); err != nil { return err diff --git a/internal/xhs/publisher_test.go b/internal/xhs/publisher_test.go index 8105e59..5f55456 100644 --- a/internal/xhs/publisher_test.go +++ b/internal/xhs/publisher_test.go @@ -77,6 +77,56 @@ func testPage(t *testing.T) *rod.Page { return page } +func TestClickOnlySelfPublishButtonClicksCustomElement(t *testing.T) { + page := testPage(t) + html := ` + +
+ +