jackwener/OpenCLI · error · CommandExecutionError

Browser session required for bilibili subtitle

Error message

Browser session required for bilibili subtitle

What it means

The bilibili subtitle command uses Strategy.COOKIE and needs a logged-in browser session: it reads cookies and signs Wbi requests through the live page. The func receives `page` from the CLI runner; when no browser session exists (page is null/undefined) it throws this CommandExecutionError before doing anything. It is a precondition guard, not a network failure.

Source

Thrown at clis/bilibili/subtitle.js:19

import { cli, Strategy } from '@jackwener/opencli/registry';
import { AuthRequiredError, CommandExecutionError, EmptyResultError } from '@jackwener/opencli/errors';
import { apiGet, resolveBvid, parsePageArg, selectVideoPart } from './utils.js';
cli({
    site: 'bilibili',
    name: 'subtitle',
    access: 'read',
    description: '获取 Bilibili 视频的字幕',
    domain: 'www.bilibili.com',
    strategy: Strategy.COOKIE,
    args: [
        { name: 'bvid', required: true, positional: true, help: 'Bilibili 视频 BV ID(如 BV1xx411c7mD),或视频 URL / b23.tv 短链' },
        { name: 'lang', required: false, help: '字幕语言代码 (如 zh-CN, en-US, ai-zh),默认取第一个' },
        { name: 'page', required: false, help: '分P 选集序号(从 1 开始)。多 P 视频取该集字幕;缺省取默认 P1' },
    ],
    columns: ['index', 'from', 'to', 'content'],
    func: async (page, kwargs) => {
        if (!page)
            throw new CommandExecutionError('Browser session required for bilibili subtitle');
        const bvid = await resolveBvid(kwargs.bvid);
        const selectedPage = parsePageArg(kwargs.page);
        // 1. 通过 view API 拿 cid。
        //    以前的实现走 page.goto(/video/<bvid>) + window.__INITIAL_STATE__.videoData.cid,
        //    bangumi 绑定的 bvid(番剧/纪录片/电影/综艺)页面 state 不在 videoData 而在 epList,
        //    导致 SELECTOR 错。view API 接受任何 bvid(UGC + PGC 都通),且不依赖 DOM 结构。
        let view;
        try {
            view = await apiGet(page, '/x/web-interface/view', { params: { bvid } });
        }
        catch (err) {
            throw new CommandExecutionError(`获取视频信息失败: ${err?.message || err}`);
        }
        if (view?.code !== 0) {
            throw new CommandExecutionError(`获取视频信息失败: ${view?.message ?? 'unknown'} (${view?.code})`);
        }
        // --page 给定时用该集 cid(selectVideoPart 越界抛错);缺省取整集默认 cid(P1,旧行为)。
        const cid = selectedPage != null ? selectVideoPart(view?.data, selectedPage).cid : view?.data?.cid;

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Launch/attach a Chrome session the CLI can use (same profile/debug port the CLI expects)
  2. Log in to bilibili.com in that Chrome profile, then re-run the command
  3. If calling func programmatically, pass a valid connected page object instead of null/undefined
Defensive patterns

Strategy: validation

Validate before calling

if (!page) throw new Error('Attach a logged-in Chrome session before calling bilibili subtitle');

Type guard

function hasPage(p) { return p != null && typeof p.evaluate === 'function'; }

Try / catch

try {
  await run(['bilibili', 'subtitle', bvid]);
} catch (e) {
  if (String(e.message).includes('Browser session required')) {
    // start/attach the CLI's Chrome session and login to bilibili.com, then retry
  } else throw e;
}

Prevention

When it happens

Trigger: Running `opencli bilibili subtitle <bvid>` without an active/logged-in Chrome session that the CLI can attach to, or invoking the exported func directly with page=null/undefined.

Common situations: Chrome not running or not started with the CLI's debugging port; user forgot to log into bilibili.com; running in CI/headless environment with no browser; calling the command programmatically without providing the page argument.

Related errors


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