Files
jackwener__opencli/clis/weibo/utils.js
Benjamin Liu dadf01b56f fix(weibo): unwrap page.evaluate envelope in read adapters (#1568)
* fix(weibo): unwrap page.evaluate envelope in read adapters (#1567)

`page.evaluate(...)` returns a `{ session, data }` envelope rather than
the raw IIFE return value, so all weibo cookie-strategy read adapters
silently dropped their results on v1.7.19:

- `getSelfUid` returned the envelope object instead of the uid string,
  so `'10001' + uid` produced `'10001[object Object]'` and every
  feed/me/favorites request hit a broken list_id.
- `feed`, `hot`, `comments`, `search`, `favorites` did `Array.isArray`
  on the envelope (always false) and returned `[]`.
- `me`, `user`, `post` returned the envelope wrapper itself instead of
  the inner profile/post object.

Same pattern as #1561 for xiaohongshu/rednote. Adds an
`unwrapEvaluateResult` helper to `clis/weibo/utils.js` (kept local
rather than cross-importing from `xiaohongshu/search.js` since weibo
is an unrelated site) and wraps every `await page.evaluate(...)` in
the 8 read adapters plus the two helper calls in `getSelfUid`.

Skipped `publish.js` (write command, out of scope for this read fix).

Verified live:
- `opencli weibo hot --limit 3` returns 3 real trending items
- `opencli weibo feed --limit 3` returns 3 timeline posts with
  correct `https://weibo.com/<uid>/<mblogid>` URLs (proves
  `getSelfUid` unwrap works)
- `opencli weibo me` returns the logged-in profile object
- All 20 weibo unit tests pass (6 new for `unwrapEvaluateResult`)

* fix(weibo): fail typed on malformed evaluate payloads

---------

Co-authored-by: jackwener <jakevingoo@gmail.com>
2026-05-14 22:34:43 +08:00

60 lines
2.3 KiB
JavaScript

/**
* Shared Weibo utilities — uid extraction.
*/
import { AuthRequiredError, CommandExecutionError } from '@jackwener/opencli/errors';
/**
* `page.evaluate` may return either the raw IIFE value or a
* `{ session, data }` envelope depending on the browser-bridge version.
* Adapter code that inspected the payload directly (e.g. `Array.isArray`,
* truthiness checks on uid strings) silently received the envelope wrapper
* instead of the inner value. This helper normalizes both shapes so callers
* can keep their existing checks unchanged.
*/
export function unwrapEvaluateResult(payload) {
if (payload && !Array.isArray(payload) && typeof payload === 'object' && 'session' in payload && 'data' in payload) {
return payload.data;
}
return payload;
}
export function requireArrayEvaluateResult(payload, label) {
if (!Array.isArray(payload)) {
if (payload && typeof payload === 'object' && 'error' in payload) {
throw new CommandExecutionError(`${label}: ${String(payload.error)}`);
}
throw new CommandExecutionError(`${label} returned malformed extraction payload`);
}
return payload;
}
export function requireObjectEvaluateResult(payload, label) {
if (!payload || Array.isArray(payload) || typeof payload !== 'object') {
throw new CommandExecutionError(`${label} returned malformed extraction payload`);
}
return payload;
}
/** Get the currently logged-in user's uid from Vue store or config API. */
export async function getSelfUid(page) {
const uid = unwrapEvaluateResult(await page.evaluate(`
(() => {
const app = document.querySelector('#app')?.__vue_app__;
const store = app?.config?.globalProperties?.$store;
const uid = store?.state?.config?.config?.uid;
if (uid) return String(uid);
return null;
})()
`));
if (uid)
return uid;
// Fallback: config API
const config = unwrapEvaluateResult(await page.evaluate(`
(async () => {
const resp = await fetch('/ajax/config/get_config', {credentials: 'include'});
if (!resp.ok) return null;
const data = await resp.json();
return data.ok && data.data?.uid ? String(data.data.uid) : null;
})()
`));
if (config)
return config;
throw new AuthRequiredError('weibo.com');
}