Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion internal/xhs/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
50 changes: 50 additions & 0 deletions internal/xhs/publisher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,56 @@ func testPage(t *testing.T) *rod.Page {
return page
}

func TestClickOnlySelfPublishButtonClicksCustomElement(t *testing.T) {
page := testPage(t)
html := `<!doctype html>
<html>
<head><meta charset="utf-8"></head>
<body>
<xhs-publish-btn is-publish="true" submit-disabled="false" submit-text="发布" style="display:block;width:120px;height:40px;" onclick="window.clickedPublish = true"></xhs-publish-btn>
</body>
</html>`
page.MustNavigate("data:text/html;charset=utf-8," + url.PathEscape(html))
page.MustWaitLoad()
page.MustElement("body")

clicked, err := (&rodPage{page: page}).clickOnlySelfPublishButton()
if err != nil {
t.Fatalf("clickOnlySelfPublishButton() error = %v", err)
}
if !clicked {
t.Fatal("clickOnlySelfPublishButton() clicked = false, want true")
}
if !page.MustEval(`() => window.clickedPublish === true`).Bool() {
t.Fatal("custom publish element was not clicked")
}
}

func TestClickOnlySelfPublishButtonIgnoresDisabledCustomElement(t *testing.T) {
page := testPage(t)
html := `<!doctype html>
<html>
<head><meta charset="utf-8"></head>
<body>
<xhs-publish-btn is-publish="true" submit-disabled="true" submit-text="发布" style="display:block;width:120px;height:40px;" onclick="window.clickedPublish = true"></xhs-publish-btn>
</body>
</html>`
page.MustNavigate("data:text/html;charset=utf-8," + url.PathEscape(html))
page.MustWaitLoad()
page.MustElement("body")

clicked, err := (&rodPage{page: page}).clickOnlySelfPublishButton()
if err != nil {
t.Fatalf("clickOnlySelfPublishButton() error = %v", err)
}
if clicked {
t.Fatal("clickOnlySelfPublishButton() clicked disabled element")
}
if page.MustEval(`() => window.clickedPublish === true`).Bool() {
t.Fatal("disabled custom publish element was clicked")
}
}

type fakePublishPage struct {
calls []string
uploaded []string
Expand Down