{"record":{"id":"d6b05c67b02c6eeb","repo":"jackwener/OpenCLI","slug":"wechat-image-upload-returned-a-malformed-editor-im","errorCode":null,"errorMessage":"WeChat image upload returned a malformed editor image payload.","messagePattern":"WeChat image upload returned a malformed editor image payload\\.","errorType":"exception","errorClass":"CommandExecutionError","httpStatus":null,"severity":"error","filePath":"clis/weixin/create-draft.js","lineNumber":108,"sourceCode":"        editor.focus();\n        if (editor.querySelector('[contenteditable=\"false\"]')) editor.innerHTML = '';\n        document.execCommand('selectAll', false, null);\n        document.execCommand('insertText', false, ${JSON.stringify(text)});\n        editor.dispatchEvent(new InputEvent('input', { bubbles: true }));\n        return { ok: true };\n    })()`);\n}\n\nasync function readCdnImageKeys(page) {\n    const keys = await evaluate(page, `(() => {\n        var editor = document.querySelector('#ueditor_0');\n        if (!editor) return [];\n        return Array.from(editor.querySelectorAll('img')).map(function(img) {\n            return img.getAttribute('data-src') || img.getAttribute('src') || '';\n        }).filter(function(src) { return /(?:mmbiz|qpic\\\\.cn)/i.test(src); });\n    })()`);\n    if (!Array.isArray(keys)) {\n        throw new CommandExecutionError('WeChat image upload returned a malformed editor image payload.');\n    }\n    return keys.map(String);\n}\n\nasync function injectImageFile(page, image) {\n    if (typeof page.setFileInput === 'function') {\n        try {\n            await page.setFileInput([image.absPath], IMAGE_FILE_INPUT_SELECTOR);\n            return;\n        } catch (error) {\n            if (!isRecoverableFileInputError(error)) {\n                const message = error instanceof Error ? error.message : String(error);\n                throw new CommandExecutionError(`WeChat image upload failed: ${message}`);\n            }\n        }\n    }\n\n    const base64 = fs.readFileSync(image.absPath).toString('base64');","sourceCodeStart":90,"sourceCodeEnd":126,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/weixin/create-draft.js#L90-L126","documentation":"Thrown by readCdnImageKeys when the in-page script that collects existing CDN image URLs (data-src/src attributes matching mmbiz or qpic.cn) from the article editor body does not return an array. The library expects evaluate() to yield an array of strings; anything else means the editor DOM or the evaluate bridge behaved unexpectedly.","triggerScenarios":"The evaluate call returns null/undefined because the editor container selector no longer matches, the page navigated away mid-operation, the in-page script threw and the evaluate bridge returned a non-array error shape, or the editor markup changed in a WeChat front-end update.","commonSituations":"WeChat updated the editor DOM classes/ids; the command ran against a page that is not the article editor; the session degraded so the editor content area is missing; evaluate serialization differences in the automation driver.","solutions":["Confirm the script is running on the actual WeChat article editor page (textarea#title present) before calling readCdnImageKeys.","Check for a WeChat editor DOM update and update the editor content selector used by the library.","Retry the operation; transient page states can make the evaluate result undefined.","Log the raw evaluate result to see what shape came back and adjust parsing accordingly."],"exampleFix":"// before\nconst keys = await evaluate(page, `...`);\nif (!Array.isArray(keys)) {\n    throw new CommandExecutionError('WeChat image upload returned a malformed editor image payload.');\n}\n// after\nconst raw = await evaluate(page, `...`);\nconst keys = Array.isArray(raw) ? raw : (raw && Array.isArray(raw.keys) ? raw.keys : []);\nif (!Array.isArray(raw)) console.warn('editor image payload malformed, treating as empty:', raw);","handlingStrategy":"validation","validationCode":"await page.wait(2);\nconst editorReady = await evaluate(page, '!!document.querySelector(\"textarea#title\") && !!document.querySelector(\".edui-body-container, #edui1_contentplaceholder, [contenteditable]\")');\nif (editorReady !== true) throw new Error('editor not ready for image key read');","typeGuard":"function isStringArray(v) { return Array.isArray(v) && v.every(x => typeof x === 'string'); }","tryCatchPattern":"try {\n    await createDraftCommand(opts);\n} catch (e) {\n    if (/malformed editor image payload/i.test(e.message)) {\n        return createDraftCommand(opts); // editor DOM may have been momentarily unavailable\n    }\n    throw e;\n}","preventionTips":["Always run the command against a fully loaded editor page.","Retry once on this error before investigating — it is often transient.","Pin/monitor WeChat editor DOM changes that affect image attributes.","Log raw evaluate results when debugging selector drift."],"tags":["wechat","dom-parsing","payload-validation","scraping"],"backgroundTag":"unexpected-response-shape","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}