Files
wehub-resource-sync 9b395f5cc3
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
Build Chrome Extension / build (push) Has been cancelled
Trigger Website Rebuild (Docs Updated) / dispatch (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:39:48 +08:00

51 lines
2.4 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 } from './write-shared.js';
cli({
site: 'zhihu',
name: 'follow',
access: 'write',
description: 'Follow a Zhihu user or question',
domain: 'www.zhihu.com',
strategy: Strategy.COOKIE,
browser: true,
args: [
{ name: 'target', positional: true, required: true, help: 'Zhihu target URL or typed target' },
{ name: 'execute', type: 'boolean', help: 'Actually perform the write action' },
],
columns: ['status', 'outcome', 'message', 'target_type', 'target'],
func: async (page, kwargs) => {
if (!page)
throw new CommandExecutionError('Browser session required for zhihu follow');
requireExecute(kwargs);
const rawTarget = String(kwargs.target);
const target = assertAllowedKinds('follow', parseTarget(rawTarget));
await page.goto('https://www.zhihu.com');
await page.wait(2);
const apiResult = await page.evaluate(`(async () => {
var targetKind = ${JSON.stringify(target.kind)};
var targetId = ${JSON.stringify(target.kind === 'user' ? target.slug : target.id)};
var url;
if (targetKind === 'question') {
url = 'https://www.zhihu.com/api/v4/questions/' + targetId + '/followers';
} else if (targetKind === 'user') {
url = 'https://www.zhihu.com/api/v4/members/' + targetId + '/followers';
} else {
return { ok: false, message: 'unsupported target type: ' + targetKind };
}
var resp = await fetch(url, { method: 'POST', credentials: 'include' });
if (!resp.ok) {
var data = {};
try { data = await resp.json(); } catch(e) {}
return { ok: false, message: data.error ? data.error.message : 'HTTP ' + resp.status };
}
return { ok: true };
})()`);
if (!apiResult?.ok) {
throw new CliError('COMMAND_EXEC', apiResult?.message || 'Failed to follow');
}
return buildResultRow(`Followed ${target.kind} ${target.kind === 'user' ? target.slug : target.id}`, target.kind, rawTarget, 'applied');
},
});