jackwener/OpenCLI · error · CommandExecutionError

Browser session required for bilibili comment

Error message

Browser session required for bilibili comment

What it means

Posting a comment on Bilibili requires a browser session (cookies/CSRF). The command's func receives `page`; when it is null/undefined (no browser session established) the library throws CommandExecutionError instead of attempting an unauthenticated write that would fail.

Source

Thrown at clis/bilibili/comment.js:34

}

cli({
    site: 'bilibili',
    name: 'comment',
    access: 'write',
    description: '在 B站视频下发表评论或回复(官方 API,需登录;消息里的 @用户 会被解析为真实提及)',
    domain: 'www.bilibili.com',
    strategy: Strategy.COOKIE,
    args: [
        { name: 'bvid', required: true, positional: true, help: 'Video BV ID / URL / b23.tv short link' },
        { name: 'message', required: true, positional: true, help: 'Comment text. Any @username in it is resolved to a real mention' },
        { name: 'parent', type: 'int', help: 'top-level/root rpid to reply under (omit for a top-level comment)' },
        { name: 'execute', type: 'boolean', help: 'Actually post the comment. Without it the command refuses to write.' },
    ],
    columns: ['rpid', 'bvid', 'oid', 'message', 'url'],
    func: async (page, kwargs) => {
        if (!page) {
            throw new CommandExecutionError('Browser session required for bilibili comment');
        }
        const message = String(kwargs.message ?? '').trim();
        if (!message)
            throw new ArgumentError('bilibili comment message cannot be empty');
        // Write guard: posting is public and irreversible-ish, so require an explicit opt-in.
        if (!kwargs.execute)
            throw new ArgumentError('Refusing to post: pass --execute to actually publish this comment');
        const parent = kwargs.parent != null ? readPositiveInteger(kwargs.parent, 'parent') : null;
        let bvid;
        try {
            bvid = await resolveBvid(kwargs.bvid);
        }
        catch (error) {
            throw new ArgumentError(`Cannot resolve Bilibili BV ID from input: ${String(kwargs.bvid ?? '')}`, error instanceof Error ? error.message : String(error));
        }
        // Resolve bvid → aid (the reply API addresses videos by aid, as `oid`)
        const view = await apiGet(page, '/x/web-interface/view', { params: { bvid } });
        const viewData = requireOkPayload(view, 'view');

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Run the bilibili login subcommand first to establish a browser session
  2. Ensure the environment can launch a browser (headless mode in CI)
  3. Re-login if the previous session expired
  4. Pass a valid Playwright page object when calling func programmatically

Example fix

// before
npx opencli bilibili comment --bvid BV1xx --message hi
// after
npx opencli bilibili login
npx opencli bilibili comment --bvid BV1xx --message hi
Defensive patterns

Strategy: validation

Validate before calling

if (!page) throw new Error('Run `bilibili login` to establish a browser session before commenting');

Type guard

const hasSession = (page) => page != null && typeof page.evaluate === 'function';

Try / catch

try { await commentCmd(); } catch (e) { if (String(e.message).includes('Browser session required')) { await login(); return commentCmd(); } throw e; }

Prevention

When it happens

Trigger: Running `bilibili comment` in a context where no browser session exists — e.g. running without prior `bilibili login`, headless environment without a launched browser, or a caller invoking the func with page=null.

Common situations: CI/cron environments with no browser; forgetting to run the login subcommand first; session expired and page torn down; calling the exported func programmatically without a page.

Related errors


AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29). Data as JSON: /api/errors/c349cae1e39a68f8. Report an issue: GitHub.