TemperMonkey用于通义千问网页版回答完毕自动朗读的脚本代码

javascript 复制代码
// ==UserScript==
// @name         通义千问回答完毕-朗读结果
// @namespace    http://tampermonkey.net/
// @version      2024-04-26
// @description  try to take over the world!
// @author       You
// @match        https://tongyi.aliyun.com/qianwen*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=aliyun.com
// @require      http://libs.baidu.com/jquery/2.0.0/jquery.min.js
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Your code here...
    // 获取目标div元素
    const targetDiv = document.querySelector('div.operateBtn--zFx6rSR0');

    // 记录初始class值
    let previousClass = targetDiv.className;

    var msg;

    var $tooltip;

    // 创建一个MutationObserver实例,传入回调函数
    const observer = new MutationObserver((mutationsList, observer) => {
        for (let mutation of mutationsList) {
            if (mutation.type === 'attributes' && mutation.attributeName === 'class') {
                const newClass = targetDiv.className;
                if (previousClass !== newClass) {
                    if(["operateBtn", " ", "stop"].every(a => previousClass.includes(a))){



                        $tooltip.remove();


                        msg=new SpeechSynthesisUtterance("通义千问:"+$("div.tongyi-ui-markdown:last").text()+"回答完毕。");
                        //msg.pitch=0;
                        speechSynthesis.speak(msg);
                    };
                    previousClass = newClass; // 更新记录的class值
                    if(["operateBtn", " ", "stop"].every(a => newClass.includes(a))){
                        speechSynthesis.cancel();
                    }
                }
            }
        }
    });

    // 配置观察选项(观察class属性变化)
    const config = { attributes: true, attributeFilter: ['class'] };

    // 开始观察目标节点
    observer.observe(targetDiv, config);
    //navigator.clipboard.writeText("speechSynthesis.cancel()");
    var obj_ta=$("textarea.ant-input.textarea--g7EUvnQR");

    obj_ta.on('keydown', function(event) {
        if(obj_ta.val()==""){
            // 检查按下的是否是回车键(keyCode 13 或 which 13)
            if (event.keyCode === 13 || event.which === 13) {
                speechSynthesis.cancel();
            }
        }else{
            //无效?(2024-5-7 19:58:21)
            // 检查按下的是否是回车键(keyCode 13 或 which 13)
            if ((event.keyCode === 13 || event.which === 13) && !event.shiftKey) {
                /*msg=new SpeechSynthesisUtterance("稍等。");
                //msg.pitch=0;
                speechSynthesis.speak(msg);*/
                // 创建一个提示框元素
                $tooltip = $(`<div class='tooltip' style='position: fixed;top: 70%;left: 55%;transform: translate(-50%, -50%);
z-index: 1000;padding: 10px;background-color: white;color: yellow;border-radius: 5px;text-align: center;font-size:30px;'>
请等待,生成中,生成时间不确定,完毕自动朗读(在输入框输入回车键可停止朗读)。</div>`);
                // 将提示框添加到页面中,可以根据需要调整位置,这里假设添加到body中
                $("body").append($tooltip);
                // 设置一个定时器,让提示框在一段时间后自动消失
            }


        }

    });
})();

有改进、优化的地方可以提出建议。

有自己好用的网站脚本代码可以分享。

相关推荐
2501_9269783310 小时前
从“数字果蝇” 到“数字意识”以及AGI的构建思路
大数据·人工智能·经验分享·算法·ai写作·agi
时光瀚海12 小时前
跨境旅游出海指南:如何通过国际短信(SMS)精准营销美国华裔银发族?
人工智能·经验分享·旅游·跨境电商·短信营销
赵谨言13 小时前
摘要本研究旨在构建一套基于OpenCV与CNN融合技术的银行卡号自动识别系统,重点解决不同银行卡号字体格式差异、倾斜污损等复杂场景下的识别难题
大数据·开发语言·经验分享·python
怪侠_岭南一只猿13 小时前
爬虫阶段一实战练习题:爬取豆瓣电影 Top250 复盘
css·经验分享·爬虫·python·学习·正则表达式
LaughingZhu13 小时前
Product Hunt 每日热榜 | 2026-03-11
大数据·数据库·人工智能·经验分享·搜索引擎
COHESION_201014 小时前
Tulsimer树脂在盐酸除铁中的应用及工艺适配性分析
经验分享·其他
Metaphor69215 小时前
使用 Python 将 PDF 文档转换为图片(Spire.PDF 教程)
经验分享
路弥行至16 小时前
linux运行脚本出现错误信息 /bin/bash^M: bad interpreter解决方法
linux·运维·开发语言·经验分享·笔记·其他·bash
赵谨言17 小时前
基于YOLOv5的火灾检测研究是当前计算机视觉和消防安全领域的重要研究方向
大数据·开发语言·经验分享·python
Night_Elf17 小时前
OpenClaw:完全零成本在Windows本机部署OpenClaw免费大模型指南
经验分享·自动化·运维开发·程序员创富