当前位置: 首页 > news >正文

监听 edge大声朗读 样式变化

<msreadoutspan class="msreadout-line-highlight msreadout-inactive-highlight">黛玉方进入房时,只见两个人搀着一位鬓发如银的老母迎上来,黛玉便<msreadoutspan class="msreadout-word-highlight">知</msreadoutspan>是他</msreadoutspan> <msreadoutspan class="msreadout-line-highlight msreadout-inactive-highlight">黛玉一一拜见过。贾母又说:“请姑娘们来。今日远客才来,<msreadoutspan class="msreadout-word-highlight">可以</msreadoutspan>不必上学去</msreadoutspan>

<p>“血米放在陈家,又不会跑,何况族中还有着贺章族 <msreadoutspan class="msreadout-line-highlight"> 叔,家族随时都可 <msreadoutspan class="msreadout-word-highlight">动手</msreadoutspan> 。” </msreadoutspan> </p>

目的是将edge朗读的内容,发送到 有声小说书屋软件中,显示

方案一:

// 防止重复处理:改用文本内容作为 key(避免 DOM 元素重建导致重复) const processedTexts = new Set(); // 判断是否是“行级”朗读片段 function isLineSpan(el) { return ( el.tagName?.toLowerCase() === 'msreadoutspan' && el.classList.contains('msreadout-line-highlight') ); } // 全局变量:用于缓冲和定时器 let sentenceBuffer = []; let sendTimer = null; const SEND_DELAY = 150; // 合并窗口:150ms // 判断两个字符串之间是否应加空格(仅当前后都是英文字母时) function shouldAddSpace(prev, current) { if (!prev || !current) return false; const lastChar = prev[prev.length - 1]; const firstChar = current[0]; const isEnglishLetter = /[a-zA-Z]/; return isEnglishLetter.test(lastChar) && isEnglishLetter.test(firstChar); } // 处理一个朗读片段 function processLineSpan(span) { const fullText = span.textContent.trim(); if (!fullText) return; // ✅ 关键:用文本内容防重(不是 DOM 元素) if (processedTexts.has(fullText)) { // console.log('⏭️ 跳过重复文本:', fullText); return; } // 添加到已处理集合 processedTexts.add(fullText); // 防内存泄漏:只保留最近 50 条 if (processedTexts.size > 50) { const arr = Array.from(processedTexts); processedTexts.clear(); arr.slice(-30).forEach(t => processedTexts.add(t)); // 保留最近 30 条 } console.log('📜 收到片段:', fullText); sentenceBuffer.push(fullText); // 重置发送定时器 if (sendTimer) clearTimeout(sendTimer); sendTimer = setTimeout(() => { // 智能拼接句子 let combinedText = ''; for (let i = 0; i < sentenceBuffer.length; i++) { if (i === 0) { combinedText = sentenceBuffer[i]; } else { if (shouldAddSpace(sentenceBuffer[i - 1], sentenceBuffer[i])) { combinedText += ' ' + sentenceBuffer[i]; } else { combinedText += sentenceBuffer[i]; } } } // 清空缓冲区 sentenceBuffer = []; if (combinedText) { console.log('📤 发送完整句子:', combinedText); sendToTtsServer(combinedText); } }, SEND_DELAY); } // 发送文本到本地 TTS 服务 function sendToTtsServer(text) { fetch('http://127.0.0.1:8088/tts', { method: 'POST', body: text, headers: { 'Content-Type': 'text/plain; charset=utf-8' } }) .then(response => response.ok ? response.text() : Promise.reject(response.status)) .then(data => console.log('📡 服务器响应:', data)) .catch(error => console.error('💥 网络错误:无法连接到“有声小说书屋”程序', error)); } // 监听 DOM 变化 const observer = new MutationObserver(mutations => { for (const mutation of mutations) { if (mutation.type === 'childList') { for (const node of mutation.addedNodes) { if (node.nodeType === Node.ELEMENT_NODE) { // 如果新节点本身就是目标 span if (isLineSpan(node)) { processLineSpan(node); } // 如果新节点内部包含目标 span if (node.querySelectorAll) { try { node.querySelectorAll('msreadoutspan.msreadout-line-highlight').forEach(processLineSpan); } catch (e) { // 降级:某些环境可能不支持自定义标签选择器 const walk = (el) => { if (isLineSpan(el)) processLineSpan(el); el.children?.forEach(walk); }; walk(node); } } } } } } }); // 扫描页面中已存在的朗读行 document.querySelectorAll('msreadoutspan.msreadout-line-highlight').forEach(processLineSpan); // 开始监听整个页面 observer.observe(document.body, { childList: true, subtree: true }); console.log('监听页面朗读行(含嵌套)已启动...');

方案二;

// 防止重复处理:记录已处理的外层 line span 元素 const processedLineSpans = new Set(); // 判断是否是“完整句子”的外层容器 function isLineContainer(el) { return ( el.tagName?.toLowerCase() === 'msreadoutspan' && el.classList.contains('msreadout-line-highlight') ); } // 发送完整句子到 TTS 服务 function sendToTtsServer(text) { if (!text.trim()) return; fetch('http://127.0.0.1:8088/tts', { method: 'POST', body: text, headers: { 'Content-Type': 'text/plain; charset=utf-8' } }) .then(response => response.ok ? response.text() : Promise.reject(response.status)) .then(data => console.log('📡 TTS 发送成功:', data)) .catch(error => console.error('💥 网络错误:无法连接到“有声小说书屋”程序', error)); } // 处理一个完整的句子容器 function processLineContainer(span) { if (processedLineSpans.has(span)) return; processedLineSpans.add(span); // ✅ 关键:textContent 自动合并所有嵌套文本(忽略内部标签) const fullText = span.textContent.trim(); if (fullText) { console.log('📤 发送完整句子:', fullText); sendToTtsServer(fullText); } } // MutationObserver:监听新插入的节点 const observer = new MutationObserver(mutations => { for (const mutation of mutations) { if (mutation.type === 'childList') { for (const node of mutation.addedNodes) { if (node.nodeType === Node.ELEMENT_NODE) { // 如果新节点本身就是 line 容器 if (isLineContainer(node)) { processLineContainer(node); } // 如果新节点内部包含 line 容器(比如批量插入) if (node.querySelectorAll) { try { node.querySelectorAll('msreadoutspan.msreadout-line-highlight') .forEach(processLineContainer); } catch (e) { // 降级遍历(兼容性兜底) const walk = (el) => { if (isLineContainer(el)) processLineContainer(el); el.children?.forEach(walk); }; walk(node); } } } } } } }); // 扫描页面中已存在的完整句子(防止遗漏初始内容) document.querySelectorAll('msreadoutspan.msreadout-line-highlight') .forEach(processLineContainer); // 开始监听整个页面 observer.observe(document.body, { childList: true, subtree: true }); console.log('监听页面完整朗读行(一次性发送)已启动...');

方案三

http://www.cnnetsun.cn/news/88356.html

相关文章:

  • 网安零基础必冲!upload-labs 文件上传漏洞保姆级通关教程
  • vue基于Springboot框架 新能源充电桩报修管理系统
  • v3基于SpringBoot的酒店管理系统
  • Git安装Windows版本并配置清华镜像用于TensorFlow贡献开发
  • Langchain-Chatchat 0.3.1 Windows本地部署指南
  • 私有云ACK:企业智能化转型的安全基座与算力引擎
  • Docker部署Qwen3-14B及GPU加速实战
  • SWIR相机
  • vLLM 0.11.0 发布:全面移除 V0 引擎,性能与多模态支持再升级
  • 从零开始:使用Git安装TensorRT及其依赖组件
  • 模块十八.集合
  • FLUX.1-dev服装生成LoRA模型体验
  • 使用nexus3搭建自己的制品服务器
  • 38、Linux 邮件与网页浏览实用指南
  • 41、互联网服务实用指南
  • LLaMA-Factory微调与模型中断续训实战
  • GitHub项目实践:Fork并定制你的个性化Anything-LLM前端界面
  • pythonstudy Day37
  • Linly-Talker结合RAG技术实现知识增强型虚拟客服系统
  • 用Deepseek-v3.1在Trae中编写AI中继程序
  • LobeChat能否实现思维导图输出?结构化内容展示尝试
  • 开源5G基站硬件参数
  • C#开发桌面应用调用GPT-SoVITS REST API实战
  • Dify Docker部署与使用全指南
  • 数组作为参数
  • 蜜罐技术-德迅猎鹰
  • Daily Report — Day 9 (Beta)
  • Seed-Coder-8B-Base与SonarQube智能集成路径
  • 基于CentOS7 DM8单机部署配置记录-20251216
  • 大模型入门:预训练、微调和蒸馏,一篇文章全掌握