|
| 1 | +import type { DynamicData, SyncData } from '../common'; |
| 2 | + |
| 3 | +export async function DynamicXiaoheihe(data: SyncData) { |
| 4 | + const { title, content, images } = data.data as DynamicData; |
| 5 | + // 辅助函数:等待元素出现 |
| 6 | + function waitForElement(selector: string, timeout = 10000): Promise<Element> { |
| 7 | + return new Promise((resolve, reject) => { |
| 8 | + const element = document.querySelector(selector); |
| 9 | + if (element) { |
| 10 | + resolve(element); |
| 11 | + return; |
| 12 | + } |
| 13 | + |
| 14 | + const observer = new MutationObserver(() => { |
| 15 | + const element = document.querySelector(selector); |
| 16 | + if (element) { |
| 17 | + resolve(element); |
| 18 | + observer.disconnect(); |
| 19 | + } |
| 20 | + }); |
| 21 | + |
| 22 | + observer.observe(document.body, { |
| 23 | + childList: true, |
| 24 | + subtree: true, |
| 25 | + }); |
| 26 | + |
| 27 | + setTimeout(() => { |
| 28 | + observer.disconnect(); |
| 29 | + reject(new Error(`Element with selector "${selector}" not found within ${timeout}ms`)); |
| 30 | + }, timeout); |
| 31 | + }); |
| 32 | + } |
| 33 | + |
| 34 | + try { |
| 35 | + const titleEditorSelector = 'div.hb-cpt__editor-title .ProseMirror.hb-editor'; |
| 36 | + const contentEditorSelector = 'div.image-text__edit-content .ProseMirror.hb-editor'; |
| 37 | + |
| 38 | + // 等待编辑器元素出现 |
| 39 | + await waitForElement(contentEditorSelector); |
| 40 | + await new Promise((resolve) => setTimeout(resolve, 1000)); |
| 41 | + |
| 42 | + // 填写标题 |
| 43 | + if (title) { |
| 44 | + try { |
| 45 | + await waitForElement(titleEditorSelector); |
| 46 | + const titleEditor = document.querySelector(titleEditorSelector); |
| 47 | + if (titleEditor) { |
| 48 | + (titleEditor as HTMLElement).focus(); |
| 49 | + const titlePasteEvent = new ClipboardEvent('paste', { |
| 50 | + bubbles: true, |
| 51 | + cancelable: true, |
| 52 | + clipboardData: new DataTransfer(), |
| 53 | + }); |
| 54 | + titlePasteEvent.clipboardData!.setData('text/plain', title); |
| 55 | + titleEditor.dispatchEvent(titlePasteEvent); |
| 56 | + await new Promise((resolve) => setTimeout(resolve, 500)); |
| 57 | + } |
| 58 | + } catch { |
| 59 | + console.debug('未找到标题编辑器元素, 跳过标题填写'); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + // 填写正文 |
| 64 | + const contentEditor = document.querySelector(contentEditorSelector); |
| 65 | + if (!contentEditor) { |
| 66 | + console.debug('未找到正文编辑器元素'); |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + (contentEditor as HTMLElement).focus(); |
| 71 | + |
| 72 | + const contentPasteEvent = new ClipboardEvent('paste', { |
| 73 | + bubbles: true, |
| 74 | + cancelable: true, |
| 75 | + clipboardData: new DataTransfer(), |
| 76 | + }); |
| 77 | + contentPasteEvent.clipboardData!.setData('text/plain', content || ''); |
| 78 | + contentEditor.dispatchEvent(contentPasteEvent); |
| 79 | + |
| 80 | + // 处理媒体上传(图片和视频) |
| 81 | + if (images.length > 0) { |
| 82 | + const imageData = []; |
| 83 | + for (const file of images) { |
| 84 | + const response = await fetch(file.url); |
| 85 | + const blob = await response.blob(); |
| 86 | + const imageFile = new File([blob], file.name, { type: file.type }); |
| 87 | + console.log(`文件: ${imageFile.name} ${imageFile.type} ${imageFile.size}`); |
| 88 | + imageData.push(imageFile); |
| 89 | + } |
| 90 | + await new Promise((resolve) => setTimeout(resolve, 1000)); |
| 91 | + |
| 92 | + window.postMessage({ type: 'XIAOHEIHE_IMAGE_UPLOAD', images: imageData }, '*'); |
| 93 | + } |
| 94 | + |
| 95 | + // 判断是否自动发布 |
| 96 | + if (!data.isAutoPublish) return; |
| 97 | + |
| 98 | + // 等待一段时间确保文件上传完成 |
| 99 | + await new Promise((resolve) => setTimeout(resolve, 3000)); |
| 100 | + |
| 101 | + // 查找发布按钮 |
| 102 | + const publishButton = document.querySelector<HTMLButtonElement>('button.editor-publish__btn'); |
| 103 | + |
| 104 | + console.debug('sendButton', publishButton); |
| 105 | + |
| 106 | + if (publishButton) { |
| 107 | + // 如果找到发布按钮,检查是否可点击 |
| 108 | + let attempts = 0; |
| 109 | + while (publishButton.disabled && attempts < 10) { |
| 110 | + await new Promise((resolve) => setTimeout(resolve, 3000)); |
| 111 | + attempts++; |
| 112 | + console.debug(`Waiting for send button to be enabled. Attempt ${attempts}/10`); |
| 113 | + } |
| 114 | + |
| 115 | + if (publishButton.disabled) { |
| 116 | + console.debug('Send button is still disabled after 10 attempts'); |
| 117 | + return; |
| 118 | + } |
| 119 | + |
| 120 | + console.debug('sendButton clicked'); |
| 121 | + // 点击发布按钮 |
| 122 | + const clickEvent = new Event('click', { bubbles: true }); |
| 123 | + publishButton.dispatchEvent(clickEvent); |
| 124 | + } else { |
| 125 | + throw new Error('未找到发布按钮'); |
| 126 | + } |
| 127 | + } catch (error) { |
| 128 | + console.error('小黑盒发布过程中出错:', error); |
| 129 | + } |
| 130 | +} |
0 commit comments