Files
wehub-resource-sync 9b395f5cc3
Build Chrome Extension / build (push) Waiting to run
Trigger Website Rebuild (Docs Updated) / dispatch (push) Waiting to run
E2E Headed Chrome / e2e-headed (macos-15) (push) Has been cancelled
E2E Headed Chrome / e2e-headed (ubuntu-latest) (push) Has been cancelled
E2E Headed Chrome / e2e-headed (windows-latest) (push) Has been cancelled
CI / build (macos-latest) (push) Has been cancelled
CI / build (ubuntu-latest) (push) Has been cancelled
CI / build (windows-latest) (push) Has been cancelled
CI / unit-test (push) Has been cancelled
CI / bun-test (push) Has been cancelled
CI / adapter-test (push) Has been cancelled
CI / smoke-test (macos-latest) (push) Has been cancelled
CI / smoke-test (ubuntu-latest) (push) Has been cancelled
Security Audit / audit (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:39:48 +08:00

56 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',
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 }),
});
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 || '',
});
},
});