fix(xiaohongshu): scan composer media from document.body so native text-to-image works - #2297
Open
liuxinyea wants to merge 1 commit into
Open
fix(xiaohongshu): scan composer media from document.body so native text-to-image works#2297liuxinyea wants to merge 1 commit into
liuxinyea wants to merge 1 commit into
Conversation
opencli's currentComposerMediaCount() picked the composer root via
titleEl.closest('form, [class*=publish], ...'), but Xiaohongshu's new
React DOM renders the image/card editor in a different subtree, so the
matched root never contained the generated media and the count was
always 0. That broke the native '--card-text' (文字生成图片) flow with
'expected at least N visible media item(s), got 0'.
- Use document.body as the scan root so generated cards are found.
- Add 'image, svg' to the media selector for completeness.
This is the maintained fork of @jackwener/opencli (liuxinyea/OpenCLI).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Problem
opencli xiaohongshu publish --card-text(小红书原生的「文字生成图片」/ text-to-image card flow) always aborts with:Root cause
currentComposerMediaCount()located the composer's DOM root by walking up from the title element:Xiaohongshu's current React renderer mounts the image / card editor in a different subtree than the title. So
closest(...)resolves to a container that never contains the generated card media — the scan always returns0, and the publisher refuses to submit even though the cards were generated correctly.Fix
document.bodyinstead of the narrowclosest(...)match, so generated cards are always found regardless of where XHS mounts the editor.image, svg(alongsideimg, video, canvas) for completeness.const titleEl = Array.from(document.querySelectorAll('input, textarea, [contenteditable="true"]')) .map((sel) => Array.from(document.querySelectorAll(sel))) .flat() .find((el) => visibleBox(el)); - const root = titleEl?.closest('form, [class*="publish"], [class*="editor"], [class*="note"]') || document.body; + const root = document.body; const seen = new Set(); let count = 0; - for (const el of Array.from(root.querySelectorAll('img, video, canvas, [style*="background-image"]'))) { + for (const el of Array.from(root.querySelectorAll('img, image, svg, video, canvas, [style*="background-image"]'))) { if (!visibleMedia(el)) continue;Verification
opencli xiaohongshu publish --card-text "..." --card-style 简约 --draftnow generates the cards and passes the media-count gate (previouslygot 0).Trade-offs
document.bodyis broader than the old root, but safe: the function already filters each candidate viavisibleMedia(el)+getBoundingClientRect(), so unrelated images elsewhere on the page are excluded.rootback down.Related issue: (none — discovered via a real automation run; link one here if it exists)
Type of Change
Checklist
dist/compiled) and verified the--card-textflow manually end-to-end.currentComposerMediaCount; DOM detection needs a live XHS session. (Happy to add a regression test if a headless harness is available.)Documentation (if adding/modifying an adapter)
Screenshots / Output
Before
After (draft publish with
--card-text)