// 在 X 长文编辑页的页面主环境(world: 'MAIN')里执行。 // chrome.scripting.executeScript 会把函数序列化后注入,所以这个函数必须自包含,不能引用外部变量。 // 编辑器是 Draft.js:通过 React fiber 拿到 editorState / onChange,直接操作内容;文字和图片用模拟粘贴写入。 export function xEngine(cmd, arg) { const ZW = ''; const composer = () => document.querySelector('[data-testid="composer"]'); const titleBox = () => [...document.querySelectorAll('textarea')].find(t => t.placeholder === 'Add a title'); const coverInput = () => document.querySelector('input[data-testid="fileInput"]'); const button = (label, root = document) => [...root.querySelectorAll('[role=button],button')].find(b => (b.getAttribute('aria-label') || b.innerText || '').trim() === label); const draft = () => { const ed = composer(); if (!ed) return null; const key = Object.keys(ed).find(k => k.startsWith('__reactFiber')); for (let f = ed[key]; f; f = f.return) { const p = f.memoizedProps; if (p && p.editorState && typeof p.onChange === 'function') return p; } return null; }; const push = (fn, changeType = 'insert-fragment') => { const p = draft(), es = p.editorState, c = es.getCurrentContent(); p.onChange(es.constructor.push(es, c.set('blockMap', fn(c.getBlockMap(), c)), changeType)); }; const newKey = () => Math.random().toString(36).slice(2, 7); // 生成 n 个无样式的字符元数据(从任意非空段落借一个,清掉样式和链接) const plainChars = (bm, n) => { const src = bm.find(b => b.getLength() > 0); if (!src) return null; const meta = src.getCharacterList().first().set('style', src.getCharacterList().first().getStyle().clear()).set('entity', null); let cl = src.getCharacterList().clear(); for (let i = 0; i < n; i++) cl = cl.push(meta); return cl; }; // 光标强制放到最后一块末尾(DOM selection 对 Draft 不可靠) const selectEnd = () => { const p = draft(), es = p.editorState, last = es.getCurrentContent().getLastBlock(); const sel = es.getSelection().merge({ anchorKey: last.getKey(), focusKey: last.getKey(), anchorOffset: last.getLength(), focusOffset: last.getLength(), isBackward: false, hasFocus: true, }); p.onChange(es.constructor.forceSelection(es, sel)); }; // 末尾补一个空段落,保证图片插在独立的一行 const appendEmpty = () => { const p = draft(), es = p.editorState, c = es.getCurrentContent(), last = c.getLastBlock(); if (last.getType() === 'unstyled' && last.getText() === '') return; const key = newKey(); const block = last.merge({ key, type: 'unstyled', text: '', depth: 0, characterList: last.getCharacterList().clear(), data: last.getData().clear() }); const sel = es.getSelection().merge({ anchorKey: key, focusKey: key, anchorOffset: 0, focusOffset: 0, isBackward: false, hasFocus: true }); const next = es.constructor.push(es, c.set('blockMap', c.getBlockMap().set(key, block)), 'split-block'); p.onChange(es.constructor.forceSelection(next, sel)); }; // 先 focus 再同步设光标并立即派发粘贴:focus 会触发 Draft 从 DOM 回读选区, // 中间若隔了一个 tick,选区可能变成跨块的范围,粘贴就会把已有内容整段替换掉 const paste = (dt) => { setTimeout(() => { const ed = composer(); ed.focus(); selectEnd(); ed.dispatchEvent(new ClipboardEvent('paste', { clipboardData: dt, bubbles: true, cancelable: true })); }, 150); }; const fileFrom = ({ b64, name, mime }) => { const bin = atob(b64), u = new Uint8Array(bin.length); for (let i = 0; i < bin.length; i++) u[i] = bin.charCodeAt(i); return new File([u], name, { type: mime }); }; const state = () => { const ed = composer(); if (!ed) return { composer: false }; const blocks = [...ed.querySelectorAll('[data-block="true"]')]; return { composer: true, draftOk: !!draft(), blocks: blocks.length, imgs: ed.querySelectorAll('img').length, busy: !!document.querySelector('[role=progressbar]'), textBlocks: blocks.filter(b => b.innerText.trim()).length, hasCover: !!button('Remove photo'), dialogs: document.querySelectorAll('[role=dialog]').length, title: (titleBox() || {}).value || '', url: location.href, }; }; switch (cmd) { case 'state': return state(); case 'setTitle': { const ta = titleBox(); if (!ta) return { ok: false, error: '找不到标题输入框' }; Object.getOwnPropertyDescriptor(HTMLTextAreaElement.prototype, 'value').set.call(ta, arg); ta.dispatchEvent(new Event('input', { bubbles: true })); return { ok: true }; } case 'pasteHTML': { // 先垫一个零宽占位段落:片段第一块会并入光标所在块并继承它的格式,占位块替真正的内容吃掉这次合并 const html = `
${ZW}
` + arg; const dt = new DataTransfer(); dt.setData('text/html', html); // X 发现 text/plain 像 Markdown(行首 - / 1. / > / # / |)时会改走自己的 Markdown 导入, // 不用我们的 HTML,还会把草稿整段替换。每行行首垫零宽字符,让它认不出来(cleanup 会清掉) const plain = html.replace(/