Skip to content

Commit 17155ad

Browse files
authored
fix: toutiao title empty (#14)
1 parent 616881a commit 17155ad

2 files changed

Lines changed: 24 additions & 32 deletions

File tree

src/background.js

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -720,34 +720,30 @@ function fillContentOnPage(content, platformId) {
720720
}
721721
// 今日头条
722722
else if (host.includes('toutiao.com')) {
723-
// 填充标题 - 头条使用 textarea 作为标题输入
724-
const titleInput = await waitFor('textarea[placeholder*="标题"], input[placeholder*="标题"], .editor-title textarea, .title-input textarea')
723+
// 填充标题 - 头条使用 textarea
724+
const titleInput = await waitFor('textarea[placeholder*="标题"]')
725725
if (titleInput) {
726726
titleInput.focus()
727-
// 使用 nativeInputValueSetter 触发 React 状态更新
728-
const nativeInputValueSetter = Object.getOwnPropertyDescriptor(window.HTMLTextAreaElement.prototype, 'value')?.set
729-
|| Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, 'value')?.set
730-
if (nativeInputValueSetter) {
731-
nativeInputValueSetter.call(titleInput, title)
732-
} else {
733-
titleInput.value = title
734-
}
735-
titleInput.dispatchEvent(new Event('input', { bubbles: true }))
727+
// 模拟用户输入
728+
const nativeSetter = Object.getOwnPropertyDescriptor(window.HTMLTextAreaElement.prototype, 'value').set
729+
nativeSetter.call(titleInput, title)
730+
titleInput.dispatchEvent(new InputEvent('input', { bubbles: true, data: title, inputType: 'insertText' }))
736731
titleInput.dispatchEvent(new Event('change', { bubbles: true }))
737-
console.log('[COSE] 头条标题填充成功')
732+
titleInput.dispatchEvent(new Event('blur', { bubbles: true }))
733+
console.log('[COSE] 头条标题填充成功:', title)
738734
} else {
739735
console.log('[COSE] 头条未找到标题输入框')
740736
}
741737

742738
// 等待编辑器加载
743-
await new Promise(resolve => setTimeout(resolve, 1000))
739+
await new Promise(resolve => setTimeout(resolve, 500))
744740

745-
// 头条使用富文本编辑器
746-
const editor = document.querySelector('.ProseMirror, [contenteditable="true"], .editor-content')
741+
// 头条使用 ProseMirror 富文本编辑器
742+
const editor = document.querySelector('.ProseMirror')
747743
if (editor) {
748744
editor.focus()
749745
editor.innerHTML = body || contentToFill.replace(/\n/g, '<br>')
750-
editor.dispatchEvent(new Event('input', { bubbles: true }))
746+
editor.dispatchEvent(new InputEvent('input', { bubbles: true }))
751747
console.log('[COSE] 头条内容填充成功')
752748
} else {
753749
console.log('[COSE] 头条未找到编辑器')

src/platforms/toutiao.js

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,34 +25,30 @@ async function fillToutiaoContent(content, waitFor, setInputValue) {
2525
const { title, body, markdown } = content
2626
const contentToFill = body || markdown || ''
2727

28-
// 填充标题 - 头条使用 textarea 作为标题输入
29-
const titleInput = await waitFor('textarea[placeholder*="标题"], input[placeholder*="标题"], .editor-title textarea, .title-input textarea')
28+
// 填充标题 - 头条使用 textarea
29+
const titleInput = await waitFor('textarea[placeholder*="标题"]')
3030
if (titleInput) {
3131
titleInput.focus()
32-
// 使用 nativeInputValueSetter 触发 React 状态更新
33-
const nativeInputValueSetter = Object.getOwnPropertyDescriptor(window.HTMLTextAreaElement.prototype, 'value')?.set
34-
|| Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, 'value')?.set
35-
if (nativeInputValueSetter) {
36-
nativeInputValueSetter.call(titleInput, title)
37-
} else {
38-
titleInput.value = title
39-
}
40-
titleInput.dispatchEvent(new Event('input', { bubbles: true }))
32+
// 模拟用户输入
33+
const nativeSetter = Object.getOwnPropertyDescriptor(window.HTMLTextAreaElement.prototype, 'value').set
34+
nativeSetter.call(titleInput, title)
35+
titleInput.dispatchEvent(new InputEvent('input', { bubbles: true, data: title, inputType: 'insertText' }))
4136
titleInput.dispatchEvent(new Event('change', { bubbles: true }))
42-
console.log('[COSE] 头条标题填充成功')
37+
titleInput.dispatchEvent(new Event('blur', { bubbles: true }))
38+
console.log('[COSE] 头条标题填充成功:', title)
4339
} else {
4440
console.log('[COSE] 头条未找到标题输入框')
4541
}
4642

4743
// 等待编辑器加载
48-
await new Promise(resolve => setTimeout(resolve, 1000))
44+
await new Promise(resolve => setTimeout(resolve, 500))
4945

50-
// 头条使用富文本编辑器
51-
const editor = document.querySelector('.ProseMirror, [contenteditable="true"], .editor-content')
46+
// 头条使用 ProseMirror 富文本编辑器
47+
const editor = document.querySelector('.ProseMirror')
5248
if (editor) {
5349
editor.focus()
5450
editor.innerHTML = contentToFill.replace(/\n/g, '<br>')
55-
editor.dispatchEvent(new Event('input', { bubbles: true }))
51+
editor.dispatchEvent(new InputEvent('input', { bubbles: true }))
5652
console.log('[COSE] 头条内容填充成功')
5753
} else {
5854
console.log('[COSE] 头条未找到编辑器')

0 commit comments

Comments
 (0)