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


        }

    });
})();

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

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

相关推荐
AI工具挖掘机9 小时前
Codex 桌面版上手:从安装到自己开发首个小游戏,0 基础快速入门,手把手教学
经验分享·ai·ai编程
三流架构师12 小时前
大厂面试资源合集
经验分享
杨连江12 小时前
清晨血压升高与晨起过敏性鼻炎清涕症状同步发作的机制关联性研究
经验分享
不大姐姐AI智能体13 小时前
实测教程:用 Codex 配合 HyperFrames,把公众号文章做成可渲染的讲解型视频
人工智能·经验分享·gpt·自动化·aigc
Jurio.13 小时前
tmux 安装与使用教程:SSH 断开后任务继续运行,终端分屏与多窗口管理
linux·经验分享·ssh·tmux
BomanGe116 小时前
NSK紧凑型FA系列精密滚珠丝杠技术解析
运维·服务器·网络·经验分享·规格说明书
天竺鼠不该去劝架17 小时前
RPA平台2026决策参考:如何通过PoC测试选出最优企业自动化方案
经验分享
天竺鼠不该去劝架19 小时前
企业流程自动化三类场景:如何选择适合自己的RPA/AI Agent
经验分享
一个人旅程~19 小时前
如何进行win11右键菜单优化(poweshell命令行与bat自动脚本方式)
windows·经验分享·macos·电脑
迷恋66619 小时前
论做好需求分析的重要性
经验分享·产品经理