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


        }

    });
})();

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

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

相关推荐
LilySesy3 小时前
ABAP+&SFSY-FORMPAGES&变成星号
经验分享·sap·abap·erp系统·smartforms
Allen_LVyingbo4 小时前
数智读书笔记系列035《未来医疗:医疗4.0引领第四次医疗产业变革》
人工智能·经验分享·笔记·健康医疗
智者知已应修善业8 小时前
【51单片机用数码管显示流水灯的种类是按钮控制数码管加一和流水灯】2022-6-14
c语言·经验分享·笔记·单片机·嵌入式硬件·51单片机
孞㐑¥11 小时前
Linux之Socket 编程 UDP
linux·服务器·c++·经验分享·笔记·网络协议·udp
June bug15 小时前
【软考中级·软件评测师】下午题·面向对象测试之架构考点全析:分层、分布式、微内核与事件驱动
经验分享·分布式·职场和发展·架构·学习方法·测试·软考
GEO科技1 天前
从SEO到GEO:AI时代的品牌大模型种草与数字营销重构
经验分享
会的全对٩(ˊᗜˋ*)و1 天前
【数据结构】栈
数据结构·经验分享·
zzywxc7871 天前
如何高效清理C盘、释放存储空间,让电脑不再卡顿。
经验分享·缓存·性能优化·电脑
deming_su1 天前
企业流程知识:《超越再造:以流程为中心的组织如何改变我们的工作和生活》读书笔记
经验分享
Li Zi11 天前
STM32 ADC(DMA)双缓冲采集+串口USART(DMA)直接传输12位原始数据到上位机显示并保存WAV格式音频文件 收藏住绝对实用!!!
经验分享·stm32·单片机·嵌入式硬件