xiaohongshu search: MutationObserver.observe(document.body) throws when body is not parsed yet
Summary
opencli xiaohongshu search <query> fails deterministically against the current
xiaohongshu.com front-end. Two symptoms alternate between runs:
TIMEOUT — xiaohongshu search content timed out after 5s(most runs)COMMAND_EXEC — Xiaohongshu search failed: TypeError: Failed to execute 'observe' on 'MutationObserver': parameter 1 is not of type 'Node'. at <anonymous>:18:14
Symptom 2 is the smoking gun: the injected wait script runs before
document.body exists, so observer.observe(document.body, …) throws. When the
throw is swallowed, the caller sees the generic 5s timeout instead.
Environment
- opencli 1.8.8 (latest on npm at time of writing), Node v22.22.3, macOS 26.3 (Apple Silicon)
- Browser Bridge extension v1.0.24,
opencli doctor→Extension: connected, connectivity 0.0s - Logged in:
opencli xiaohongshu whoami→{"logged_in": true, "username": "…"} - Page state when the failure happens: the search results page IS rendered (verified by screenshot — 4 note cards with titles/authors/likes and the "大家都在搜" panel). So it is not a login wall, not a risk-control block, and not a selector problem.
Root cause
clis/xiaohongshu/search.js → WAIT_FOR_CONTENT_JS:
const found = detect();
if (found) return resolve(found);
const observer = new MutationObserver(() => { … });
observer.observe(document.body, { childList: true, subtree: true }); // <- body may still be null
setTimeout(() => { observer.disconnect(); resolve('timeout'); }, 5000);page.evaluate(WAIT_FOR_CONTENT_JS) is issued immediately after page.goto(url),
so on a cold/slow navigation document.body is still null inside the async IIFE.
The evaluate promise then rejects (symptom 2) or the thrown error prevents the
5s timer wiring and the caller reports a timeout (symptom 1).
Worth noting: the detection selector itself is fine. Running the adapter's own selector on the live page returns a match:
document.querySelector('section.note-item, section:has(a[href*="/search_result/"]), section:has(a[href*="/explore/"])')
// -> <section …> (40 anchors match a[href*="/search_result/"])Minimal fix (verified locally)
Defer the observer until document.body exists:
let observer = null;
const finish = (value) => { if (observer) { try { observer.disconnect(); } catch (e) {} } resolve(value); };
const start = () => {
const found = detect();
if (found) return finish(found);
observer = new MutationObserver(() => {
const result = detect();
if (result) finish(result);
});
observer.observe(document.body, { childList: true, subtree: true });
setTimeout(() => finish('timeout'), 5000);
};
if (document.body) {
start();
} else {
const bodyTimer = setInterval(() => {
if (document.body) { clearInterval(bodyTimer); start(); }
}, 50);
setTimeout(() => { clearInterval(bodyTimer); finish('timeout'); }, 5000);
}After this change, opencli xiaohongshu search "关键词" --limit 5 -f json returns
real results (title / author / author_url / likes / url / published_at).
Note on the 5s budget
With the body guard in place the wait succeeds, but 5s is tight for a cold
navigation on a slow connection. Making CONTENT_WAIT_SECONDS configurable via
env (e.g. OPENCLI_XHS_CONTENT_WAIT) would make this robust — the daemon-level
OPENCLI_BROWSER_COMMAND_TIMEOUT does not affect it, which is confusing because
the CLI's own hint message suggests --timeout / OPENCLI_BROWSER_COMMAND_TIMEOUT
right next to this error.
Source: jackwener/OpenCLI