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);
                // 设置一个定时器,让提示框在一段时间后自动消失
            }


        }

    });
})();

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

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

相关推荐
虹科数字化与AR8 小时前
电力巡检AR眼镜怎么选?重量、防护、续航、摄像头与佩戴方式
经验分享·增强现实·电力巡检
2601_9672127210 小时前
安全型电源轨道系统合规选型技术指标与行业技术路线分析
大数据·经验分享·安全
光视界10 小时前
机器人慧眼:Ensenso 3D相机在汽车装配与物流分拣中的应用
经验分享·3d相机
qiushijin9611 小时前
【2026.12】日语能力考试N1、N2、N3、N4、N5历年真题及答案合集PDF(2010-2026年)
经验分享·pdf
思陌Ai算法定制13 小时前
医疗大模型怎么微调?EpiScreen微调开源LLM读临床笔记早筛癫痫,AUC最高0.98、医生协作提升14.3%
经验分享
我命由我1234514 小时前
田野笔记极简理解
经验分享·笔记·学习·职场和发展·求职招聘·职场发展·学习方法
hgfsjk17 小时前
Temu Y2 利润计算器:从定价公式到店铺利润落地
大数据·经验分享·笔记·其他
qiushijin9617 小时前
【2026】考研政治历年真题及答案解析合集PDF(2003-2026年)
经验分享·pdf
AIHR数智引擎20 小时前
蚂蚁350亿换不来智能体飞轮?拆中台和流水线
人工智能·经验分享·职场和发展·ai-native·aihr
聚美智数20 小时前
网站TDK查询-TDK查询-SEO优化
经验分享