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


        }

    });
})();

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

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

相关推荐
星哥说事16 小时前
Rocky Linux 9 源码包安装php7
linux·经验分享
慕洋男孩20 小时前
错误经验一:计算两个整数a和b的和
c语言·经验分享·错误经验
WPG大大通2 天前
TBT 5、TBT 4 和 USB4 的差异概述
经验分享·笔记·电脑·显示器·usb·充电
Joker—H3 天前
【Java】JUC并发(线程的方法、多线程的同步并发)
java·开发语言·经验分享·idea
ChongYu重玉4 天前
【node/vue】css制作可3D旋转倾斜的图片,朝向鼠标
javascript·css·vue.js·经验分享·笔记·node.js·vue
源代码•宸5 天前
C++高频知识点(十三)
开发语言·c++·经验分享·面经
漠效5 天前
Duplicate cleaner pro 的使用技巧
windows·经验分享
小程序华东同舟求职5 天前
Ampace厦门新能安科技Verify 测评演绎数字推理及四色测评考点分析、SHL真题题库
人工智能·经验分享·科技·面试·职场和发展·求职招聘
TeleostNaCl5 天前
一次因 luci 无法登录而通过 SSH 升级 OpenWrt 固件的经验总结
运维·网络·经验分享·ssh·智能路由器
waiting&fighting5 天前
《低欲望社会》+《银发经济学》整理
经验分享