Files
jackwener__opencli/clis/zhihu/comment.js
Benjamin Liu c86b6826a4 fix(zhihu): fix identity detection, comment, answer, and search (#1207)
* fix(zhihu): fix identity detection, comment, answer, and search

Identity detection: Zhihu removed __INITIAL_STATE__ and moved the
user avatar from a profile link into a button. Added fallback that
extracts the user slug from the header avatar alt text.

Comment and answer: Zhihu moved the comment editor into a Modal
and changed the submit button behavior, breaking the UI-based
write flow. Replaced with direct API calls (POST /api/v4/answers/
{id}/comments and POST /api/v4/questions/{id}/answers) which are
reliable and much simpler.

Search: Zhihu's search API now returns mixed result types (ads,
education, hot_timing) alongside search_result. Updated the filter
to select by object.type (answer/article/question) and increased
fetch size to compensate for non-content results.

Fixes #1198

* fix(zhihu): rewrite like, follow, favorite to use API

Same DOM breakage as comment/answer. Replaced UI-based click
flows with direct Zhihu API calls for all write commands.

* fix(zhihu): harden api write regressions

---------

Co-authored-by: jackwener <jakevingoo@gmail.com>
2026-04-29 15:01:33 +08:00

55 lines
2.8 KiB
JavaScript

import { CliError, CommandExecutionError } from '@jackwener/opencli/errors';
import { cli, Strategy } from '@jackwener/opencli/registry';
import { assertAllowedKinds, parseTarget } from './target.js';
import { buildResultRow, requireExecute, resolveCurrentUserIdentity, resolvePayload } from './write-shared.js';
cli({
site: 'zhihu',
name: 'comment',
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 }),
});
var data = await resp.json();
if (!resp.ok) return { ok: false, status: resp.status, message: data.error ? data.error.message : 'unknown error' };
if (!data || !data.id) return { ok: false, status: resp.status, message: 'Comment API response did not include a created comment id' };
return { ok: true, id: data.id, url: data.url };
})()`);
if (!apiResult?.ok) {
throw new CliError('COMMAND_EXEC', apiResult?.message || 'Failed to create comment');
}
return buildResultRow(`Commented on ${target.kind} ${target.id}`, target.kind, rawTarget, 'created', {
author_identity: authorIdentity,
created_url: apiResult.url || '',
});
},
});