jackwener/OpenCLI · error · CommandExecutionError

Browser session required for zhihu comment

Error message

Browser session required for zhihu comment

What it means

The `zhihu comment` command requires a live browser page session because it navigates to the target URL and performs the comment write in the browser. CommandExecutionError (code COMMAND_EXEC) is thrown at the top of func when `page` is falsy, i.e. the command was invoked without a connected browser session. This is a precondition guard, not a runtime failure.

Source

Thrown at clis/zhihu/comment.js:22

import { buildResultRow, requireExecute, resolveCurrentUserIdentity, resolvePayload } from './write-shared.js';
cli({
    site: 'zhihu',
    name: 'comment',
    access: 'write',
    description: 'Create a top-level comment on a Zhihu answer or article',
    domain: 'zhihu.com',
    strategy: Strategy.COOKIE,
    browser: true,
    args: [
        { name: 'target', positional: true, required: true, help: 'Zhihu target URL or typed target' },
        { name: 'text', positional: true, help: 'Comment text' },
        { name: 'file', help: 'Comment text file path' },
        { name: 'execute', type: 'boolean', help: 'Actually perform the write action' },
    ],
    columns: ['status', 'outcome', 'message', 'target_type', 'target', 'author_identity', 'created_url'],
    func: async (page, kwargs) => {
        if (!page)
            throw new CommandExecutionError('Browser session required for zhihu comment');
        requireExecute(kwargs);
        const rawTarget = String(kwargs.target);
        const target = assertAllowedKinds('comment', parseTarget(rawTarget));
        const payload = await resolvePayload(kwargs);
        await page.goto(target.url);
        await page.wait(3);
        const authorIdentity = await resolveCurrentUserIdentity(page);
        const apiResult = await page.evaluate(`(async () => {
            var targetKind = ${JSON.stringify(target.kind)};
            var targetId = ${JSON.stringify(target.id)};
            var content = ${JSON.stringify(payload)};
            var resourceType = targetKind === 'answer' ? 'answers' : 'articles';
            var url = 'https://www.zhihu.com/api/v4/' + resourceType + '/' + targetId + '/comments';
            var resp = await fetch(url, {
                method: 'POST',
                credentials: 'include',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({ content: content }),

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Start/reconnect the browser session: launch the opencli-connected Chrome (or run the command via the normal CLI entry which connects the browser) and retry.
  2. Check the extension/daemon status and reconnect if the browser was restarted.
  3. If calling the API programmatically, obtain a page from the browser adapter and pass it instead of null.

Example fix

// before
await cli.run('zhihu', 'comment', { target: '...', file: '...' }); // no browser
// after
const browser = await opencli.browser.connect();
const page = await browser.newPage();
await zhihuComment(page, { target: '...', file: '...', execute: true });
Defensive patterns

Strategy: validation

Validate before calling

if (!page) throw new Error('Connect a browser session before running zhihu comment'); // or check daemon status via opencli browser status

Type guard

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

Try / catch

try {
  await zhihuComment(page, kwargs);
} catch (e) {
  if (e.code === 'COMMAND_EXEC' && /Browser session required/i.test(e.message)) {
    page = await connectBrowserAndNewPage();
    return zhihuComment(page, kwargs);
  }
  throw e;
}

Prevention

When it happens

Trigger: Invoking `opencli zhihu comment ...` when the browser daemon is not running, the extension is not connected, or the adapter was called programmatically with no page argument — so `page` is null/undefined at func entry.

Common situations: Running the CLI on a machine where the opencli browser daemon was never started; the Chrome extension disconnected after a browser restart; calling the CLI function directly in tests or scripts without establishing a browser session.

Related errors


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