Chrome扩展开发:Skype网页版聊天输入框无法输入文本触发发送
解决方案
Skype网页版输入框基于Draft.js富文本框架实现,内部维护独立的状态管理,直接修改DOM内容无法同步到框架状态,因此需要通过编辑器原生支持的操作方式填充内容。
完整实现代码
send(text) { // 获取输入框编辑器元素 const editor = document.querySelector('.public-DraftEditor-content[contenteditable="true"]'); if (!editor) return; // 激活编辑器焦点 editor.focus(); // 清空编辑器原有内容 document.execCommand('selectAll', false, null); document.execCommand('delete', false, null); // 插入需要发送的文本 document.execCommand('insertText', false, text); // 触发发送按钮点击 const sendBtn = document.querySelector('[aria-label="Отправить сообщение"]'); if (sendBtn) { sendBtn.dispatchEvent(new Event('click', { bubbles: true })); } }
注意事项
- 如果页面是其他语言版本,需要把输入框和发送按钮的
aria-label属性值替换为对应语言的文本,也可以用类名选择器保证兼容性 - 执行方法前需要确保聊天窗口已经完全加载,输入框和发送按钮已渲染到DOM中
- 如果
execCommand方法在后续浏览器版本中失效,可以替换为剪贴板写入+粘贴的方案实现文本填充
内容的提问来源于stack exchange,提问作者veb1
相关产品推荐
相关产品推荐

