|
| 1 | +import { CliError, CommandExecutionError } from '@jackwener/opencli/errors'; |
| 2 | +import { cli, Strategy } from '@jackwener/opencli/registry'; |
| 3 | +import type { IPage } from '@jackwener/opencli/types'; |
| 4 | +import { assertAllowedKinds, parseTarget, type ZhihuTarget } from './target.js'; |
| 5 | +import { buildResultRow, requireExecute, resolveCurrentUserIdentity, resolvePayload } from './write-shared.js'; |
| 6 | + |
| 7 | +const ANSWER_AUTHOR_SCOPE_SELECTOR = '.AuthorInfo, .AnswerItem-authorInfo, .ContentItem-meta, [itemprop="author"]'; |
| 8 | + |
| 9 | +cli({ |
| 10 | + site: 'zhihu', |
| 11 | + name: 'answer', |
| 12 | + description: 'Answer a Zhihu question', |
| 13 | + domain: 'www.zhihu.com', |
| 14 | + strategy: Strategy.UI, |
| 15 | + browser: true, |
| 16 | + args: [ |
| 17 | + { name: 'target', positional: true, required: true, help: 'Zhihu question URL or typed target' }, |
| 18 | + { name: 'text', positional: true, help: 'Answer text' }, |
| 19 | + { name: 'file', help: 'Answer text file path' }, |
| 20 | + { name: 'execute', type: 'boolean', help: 'Actually perform the write action' }, |
| 21 | + ], |
| 22 | + columns: ['status', 'outcome', 'message', 'target_type', 'target', 'created_target', 'created_url', 'author_identity'], |
| 23 | + func: async (page: IPage | null, kwargs: Record<string, unknown>) => { |
| 24 | + if (!page) throw new CommandExecutionError('Browser session required for zhihu answer'); |
| 25 | + |
| 26 | + requireExecute(kwargs); |
| 27 | + const rawTarget = String(kwargs.target); |
| 28 | + const target = assertAllowedKinds('answer', parseTarget(rawTarget)); |
| 29 | + const questionTarget = target as Extract<ZhihuTarget, { kind: 'question' }>; |
| 30 | + const payload = await resolvePayload(kwargs); |
| 31 | + |
| 32 | + await page.goto(target.url); |
| 33 | + const authorIdentity = await resolveCurrentUserIdentity(page); |
| 34 | + |
| 35 | + const entryPath = await page.evaluate(`(() => { |
| 36 | + const currentUserSlug = ${JSON.stringify(authorIdentity)}; |
| 37 | + const answerAuthorScopeSelector = ${JSON.stringify(ANSWER_AUTHOR_SCOPE_SELECTOR)}; |
| 38 | + const readAnswerAuthorSlug = (node) => { |
| 39 | + const authorScopes = Array.from(node.querySelectorAll(answerAuthorScopeSelector)); |
| 40 | + const slugs = Array.from(new Set(authorScopes |
| 41 | + .flatMap((scope) => Array.from(scope.querySelectorAll('a[href^="/people/"]'))) |
| 42 | + .map((link) => (link.getAttribute('href') || '').match(/^\\/people\\/([A-Za-z0-9_-]+)/)?.[1] || null) |
| 43 | + .filter(Boolean))); |
| 44 | + return slugs.length === 1 ? slugs[0] : null; |
| 45 | + }; |
| 46 | + const restoredDraft = !!document.querySelector('[contenteditable="true"][data-draft-restored], textarea[data-draft-restored]'); |
| 47 | + const composerCandidates = Array.from(document.querySelectorAll('[contenteditable="true"], textarea')).map((editor) => { |
| 48 | + const container = editor.closest('form, .AnswerForm, .DraftEditor-root, [data-za-module*="Answer"]') || editor.parentElement; |
| 49 | + const text = 'value' in editor ? editor.value || '' : (editor.textContent || ''); |
| 50 | + const submitButton = Array.from((container || document).querySelectorAll('button')).find((node) => /发布|提交/.test(node.textContent || '')); |
| 51 | + const nestedComment = Boolean(container?.closest('[data-comment-id], .CommentItem')); |
| 52 | + return { editor, container, text, submitButton, nestedComment }; |
| 53 | + }).filter((candidate) => candidate.container && candidate.submitButton && !candidate.nestedComment); |
| 54 | + const hasExistingAnswerByCurrentUser = Array.from(document.querySelectorAll('[data-zop-question-answer], article')).some((node) => { |
| 55 | + return readAnswerAuthorSlug(node) === currentUserSlug; |
| 56 | + }); |
| 57 | + return { |
| 58 | + entryPathSafe: composerCandidates.length === 1 |
| 59 | + && !String(composerCandidates[0].text || '').trim() |
| 60 | + && !restoredDraft |
| 61 | + && !hasExistingAnswerByCurrentUser, |
| 62 | + hasExistingAnswerByCurrentUser, |
| 63 | + }; |
| 64 | + })()`) as { entryPathSafe?: boolean; hasExistingAnswerByCurrentUser?: boolean }; |
| 65 | + |
| 66 | + if (entryPath.hasExistingAnswerByCurrentUser) { |
| 67 | + throw new CliError('ACTION_NOT_AVAILABLE', 'zhihu answer only supports creating a new answer when the current user has not already answered this question'); |
| 68 | + } |
| 69 | + if (!entryPath.entryPathSafe) { |
| 70 | + throw new CliError('ACTION_NOT_AVAILABLE', 'Answer editor entry path was not proven side-effect free'); |
| 71 | + } |
| 72 | + |
| 73 | + const editorState = await page.evaluate(`(async () => { |
| 74 | + const composerCandidates = Array.from(document.querySelectorAll('[contenteditable="true"], textarea')).map((editor) => { |
| 75 | + const container = editor.closest('form, .AnswerForm, .DraftEditor-root, [data-za-module*="Answer"]') || editor.parentElement; |
| 76 | + const text = 'value' in editor ? editor.value || '' : (editor.textContent || ''); |
| 77 | + const submitButton = Array.from((container || document).querySelectorAll('button')).find((node) => /发布|提交/.test(node.textContent || '')); |
| 78 | + const nestedComment = Boolean(container?.closest('[data-comment-id], .CommentItem')); |
| 79 | + return { editor, container, text, submitButton, nestedComment }; |
| 80 | + }).filter((candidate) => candidate.container && candidate.submitButton && !candidate.nestedComment); |
| 81 | + if (composerCandidates.length !== 1) return { editorState: 'unsafe', anonymousMode: 'unknown' }; |
| 82 | + const { editor, text } = composerCandidates[0]; |
| 83 | + const anonymousLabeledControl = |
| 84 | + (composerCandidates[0].container && composerCandidates[0].container.querySelector('[aria-label*="匿名"], [title*="匿名"]')) |
| 85 | + || Array.from((composerCandidates[0].container || document).querySelectorAll('label, button, [role="switch"], [role="checkbox"]')).find((node) => /匿名/.test(node.textContent || '')) |
| 86 | + || null; |
| 87 | + const anonymousToggle = |
| 88 | + anonymousLabeledControl?.matches?.('input[type="checkbox"], [role="switch"], [role="checkbox"], button') |
| 89 | + ? anonymousLabeledControl |
| 90 | + : anonymousLabeledControl?.querySelector?.('input[type="checkbox"], [role="switch"], [role="checkbox"], button') |
| 91 | + || null; |
| 92 | + let anonymousMode = 'unknown'; |
| 93 | + if (anonymousToggle) { |
| 94 | + const ariaChecked = anonymousToggle.getAttribute && anonymousToggle.getAttribute('aria-checked'); |
| 95 | + const checked = 'checked' in anonymousToggle ? anonymousToggle.checked === true : false; |
| 96 | + if (ariaChecked === 'true' || checked) anonymousMode = 'on'; |
| 97 | + else if (ariaChecked === 'false' || ('checked' in anonymousToggle && anonymousToggle.checked === false)) anonymousMode = 'off'; |
| 98 | + } |
| 99 | + return { |
| 100 | + editorState: editor && !text.trim() ? 'fresh_empty' : 'unsafe', |
| 101 | + anonymousMode, |
| 102 | + }; |
| 103 | + })()`) as { editorState?: string; anonymousMode?: 'on' | 'off' | 'unknown' }; |
| 104 | + |
| 105 | + if (editorState.editorState !== 'fresh_empty') { |
| 106 | + throw new CliError('ACTION_NOT_AVAILABLE', 'Answer editor was not fresh and empty'); |
| 107 | + } |
| 108 | + if (editorState.anonymousMode !== 'off') { |
| 109 | + throw new CliError('ACTION_NOT_AVAILABLE', 'Anonymous answer mode could not be proven off for zhihu answer'); |
| 110 | + } |
| 111 | + |
| 112 | + const editorCheck = await page.evaluate(`(async () => { |
| 113 | + const textToInsert = ${JSON.stringify(payload)}; |
| 114 | + const composerCandidates = Array.from(document.querySelectorAll('[contenteditable="true"], textarea')).map((editor) => { |
| 115 | + const container = editor.closest('form, .AnswerForm, .DraftEditor-root, [data-za-module*="Answer"]') || editor.parentElement; |
| 116 | + const submitButton = Array.from((container || document).querySelectorAll('button')).find((node) => /发布|提交/.test(node.textContent || '')); |
| 117 | + const nestedComment = Boolean(container?.closest('[data-comment-id], .CommentItem')); |
| 118 | + return { editor, container, submitButton, nestedComment }; |
| 119 | + }).filter((candidate) => candidate.container && candidate.submitButton && !candidate.nestedComment); |
| 120 | + if (composerCandidates.length !== 1) return { editorContent: '', bodyMatches: false }; |
| 121 | + const { editor } = composerCandidates[0]; |
| 122 | + editor.focus(); |
| 123 | + if ('value' in editor) { |
| 124 | + editor.value = ''; |
| 125 | + editor.dispatchEvent(new Event('input', { bubbles: true })); |
| 126 | + editor.value = textToInsert; |
| 127 | + editor.dispatchEvent(new Event('input', { bubbles: true })); |
| 128 | + } else { |
| 129 | + editor.textContent = ''; |
| 130 | + document.execCommand('insertText', false, textToInsert); |
| 131 | + editor.dispatchEvent(new InputEvent('input', { bubbles: true, data: textToInsert, inputType: 'insertText' })); |
| 132 | + } |
| 133 | + await new Promise((resolve) => setTimeout(resolve, 200)); |
| 134 | + const content = 'value' in editor ? editor.value : (editor.textContent || ''); |
| 135 | + return { editorContent: content, bodyMatches: content === textToInsert }; |
| 136 | + })()`) as { editorContent?: string; bodyMatches?: boolean }; |
| 137 | + |
| 138 | + if (editorCheck.editorContent !== payload || !editorCheck.bodyMatches) { |
| 139 | + throw new CliError('OUTCOME_UNKNOWN', 'Answer editor content did not exactly match the requested payload before publish'); |
| 140 | + } |
| 141 | + |
| 142 | + const proof = await page.evaluate(`(async () => { |
| 143 | + const normalize = (value) => value.replace(/\\s+/g, ' ').trim(); |
| 144 | + const answerAuthorScopeSelector = ${JSON.stringify(ANSWER_AUTHOR_SCOPE_SELECTOR)}; |
| 145 | + const readAnswerAuthorSlug = (node) => { |
| 146 | + const authorScopes = Array.from(node.querySelectorAll(answerAuthorScopeSelector)); |
| 147 | + const slugs = Array.from(new Set(authorScopes |
| 148 | + .flatMap((scope) => Array.from(scope.querySelectorAll('a[href^="/people/"]'))) |
| 149 | + .map((link) => (link.getAttribute('href') || '').match(/^\\/people\\/([A-Za-z0-9_-]+)/)?.[1] || null) |
| 150 | + .filter(Boolean))); |
| 151 | + return slugs.length === 1 ? slugs[0] : null; |
| 152 | + }; |
| 153 | + const composerCandidates = Array.from(document.querySelectorAll('[contenteditable="true"], textarea')).map((editor) => { |
| 154 | + const container = editor.closest('form, [role="dialog"], .AnswerForm, .DraftEditor-root, [data-za-module*="Answer"]') || editor.parentElement; |
| 155 | + const submitButton = Array.from((container || document).querySelectorAll('button')).find((node) => /发布|提交/.test(node.textContent || '')); |
| 156 | + const nestedComment = Boolean(container?.closest('[data-comment-id], .CommentItem')); |
| 157 | + return { editor, container, submitButton, nestedComment }; |
| 158 | + }).filter((candidate) => candidate.container && candidate.submitButton && !candidate.nestedComment); |
| 159 | + if (composerCandidates.length !== 1) return { createdTarget: null, createdUrl: null, authorIdentity: null, bodyMatches: false }; |
| 160 | + const submitScope = composerCandidates[0].container || document; |
| 161 | + const submit = Array.from(submitScope.querySelectorAll('button')).find((node) => /发布|提交/.test(node.textContent || '')); |
| 162 | + submit && submit.click(); |
| 163 | + await new Promise((resolve) => setTimeout(resolve, 1500)); |
| 164 | + const href = location.href; |
| 165 | + const match = href.match(/question\\/(\\d+)\\/answer\\/(\\d+)/); |
| 166 | + const targetHref = match ? '/question/' + match[1] + '/answer/' + match[2] : null; |
| 167 | + const answerContainer = targetHref |
| 168 | + ? Array.from(document.querySelectorAll('[data-zop-question-answer], article')).find((node) => { |
| 169 | + const dataAnswerId = node.getAttribute('data-answerid') || node.getAttribute('data-zop-question-answer') || ''; |
| 170 | + if (dataAnswerId && dataAnswerId.includes(match[2])) return true; |
| 171 | + return Array.from(node.querySelectorAll('a[href*="/answer/"]')).some((link) => { |
| 172 | + const hrefValue = link.getAttribute('href') || ''; |
| 173 | + return hrefValue.includes(targetHref); |
| 174 | + }); |
| 175 | + }) |
| 176 | + : null; |
| 177 | + const authorSlug = answerContainer ? readAnswerAuthorSlug(answerContainer) : null; |
| 178 | + const bodyNode = |
| 179 | + answerContainer?.querySelector('[itemprop="text"]') |
| 180 | + || answerContainer?.querySelector('.RichContent-inner') |
| 181 | + || answerContainer?.querySelector('.RichText') |
| 182 | + || answerContainer; |
| 183 | + const bodyText = normalize(bodyNode?.textContent || ''); |
| 184 | + return match |
| 185 | + ? { |
| 186 | + createdTarget: 'answer:' + match[1] + ':' + match[2], |
| 187 | + createdUrl: href, |
| 188 | + authorIdentity: authorSlug, |
| 189 | + bodyMatches: bodyText === normalize(${JSON.stringify(payload)}), |
| 190 | + } |
| 191 | + : { createdTarget: null, createdUrl: null, authorIdentity: authorSlug, bodyMatches: false }; |
| 192 | + })()`) as { |
| 193 | + createdTarget?: string | null; |
| 194 | + createdUrl?: string | null; |
| 195 | + authorIdentity?: string | null; |
| 196 | + bodyMatches?: boolean; |
| 197 | + }; |
| 198 | + |
| 199 | + if (proof.authorIdentity !== authorIdentity) { |
| 200 | + throw new CliError('OUTCOME_UNKNOWN', 'Answer was created but authorship could not be proven for the frozen current user'); |
| 201 | + } |
| 202 | + if (!proof.createdTarget || !proof.bodyMatches || proof.createdTarget.split(':')[1] !== questionTarget.id) { |
| 203 | + throw new CliError('OUTCOME_UNKNOWN', 'Created answer proof did not match the requested question or payload'); |
| 204 | + } |
| 205 | + |
| 206 | + return buildResultRow(`Answered question ${questionTarget.id}`, target.kind, rawTarget, 'created', { |
| 207 | + created_target: proof.createdTarget, |
| 208 | + created_url: proof.createdUrl, |
| 209 | + author_identity: authorIdentity, |
| 210 | + }); |
| 211 | + }, |
| 212 | +}); |
0 commit comments