智能语音点读机

Posted marHangover

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了智能语音点读机相关的知识,希望对你有一定的参考价值。

智能语音点读机

选中文本智能语音播报,脚本内填写正确appid与密钥后开启翻译即可(默认关闭)

使用脚本前需先安装脚本管理器(2选1即可):1. tampermonkey  2.脚本猫

百度翻译api申请地址:百度翻译开放平台

脚本更新地址:https://scriptcat.org/script-show-page/299

// ==UserScript==
// @name         智能语音点读机
// @namespace    https://bbs.tampermonkey.net.cn/
// @version      0.2.2
// @description  哪里不会点哪里~!选中文本自动语音播报~!语音翻译功能需自行申请翻译api,填写正确后开启翻译即可(开启后选中文本在鼠标附近会出现翻译按钮)~!
// @author       张仨
// @match        *://*/*
// @grant        unsafeWindow
// @grant        GM_xmlhttpRequest
// @connect      fanyi-api.baidu.com
// @require      https://cdn.jsdelivr.net/npm/ddkr-zhangsan@1.0.0/md5.js
// ==/UserScript==

unsafeWindow.onload = function () 
    \'use strict\';
    var pitch = 1;   //音调,取值范围(0 - 2) 默认值:1
    var rate = 0.7;  //语速,取值范围(0.1 - 10) 默认值:1
    var Translate = false; //是否开启翻译 默认false不开启 改为true则开启
    var LeftOffset = 50;   //翻译按钮偏移量 数值越大越往右偏移
    var UpOffset = 0;      //翻译按钮偏移量 数值越大越往下偏移
    var appid = \'123456\';   //百度翻译APP ID 请自行申请
    var key = \'0123456789\';  //密钥
    function speak(sentence) 
        var utterance = new SpeechSynthesisUtterance(sentence);
        utterance.pitch = pitch;
        utterance.rate = rate;
        window.speechSynthesis.speak(utterance);
    ;
    window.addEventListener(\'mouseup\', () => 
        var txt = window.getSelection();
        speak(txt, pitch, rate)
    )
    function newAjax() 
        var salt = (new Date).getTime();
        var query = window.getSelection().toString();
        var from = \'auto\';
        var to;
        var pattern = new RegExp("[\\u4E00-\\u9FA5]+");
        if (pattern.test(query)) 
            to = \'en\';
         else 
            to = \'zh\';
        ;
        var str1 = appid + query + salt + key;
        var sign = MD5(str1);
        GM_xmlhttpRequest(
            method: "POST",
            responseType: "json",
            headers: 
                "Content-type": "application/x-www-form-urlencoded"
            ,
            data: `q=$encodeURI(query)&from=$from&to=$to&appid=$appid&salt=$salt&sign=$sign`,
            url: "https://fanyi-api.baidu.com/api/trans/vip/translate?",
            onload: function (xhr) 
                if (xhr.response.error_code) 
                    var flag = confirm("请检查你的appid与密钥是否正确,如未申请API可点击对话框中的确定按钮跳转到百度翻译开放平台进行申请通用翻译API");
                    if (flag) 
                        window.open("https://api.fanyi.baidu.com/product/11");
                    
                 else 
                    var TextTranslate = xhr.response.trans_result[0].dst;
                    speak(TextTranslate, pitch, rate);
                
            
        );
    ;
    if (Translate) 
        document.addEventListener("mouseup", (event) => 
            event = event || window.event;
            var left = event.clientX;
            var top = event.clientY;
            var TranslationButton = document.querySelector(\'#speech-translation\');
            var getSelection = window.getSelection().toString();
            if (getSelection != \'\') 
                if (TranslationButton) 
                    document.body.removeChild(TranslationButton);
                ;
                TranslationButton = document.createElement("button");
                document.body.appendChild(TranslationButton);
                TranslationButton.style.left = left + LeftOffset + "px";
                TranslationButton.style.top = top + UpOffset + "px";
                TranslationButton.id = "speech-translation";
                TranslationButton.innerHTML = `
                <span id=\'btn-translation\'>翻译</span>
                <style>
                #speech-translation
                    position: fixed;
                    width: 60px;
                    height: 30px;
                    cursor: pointer;
                    z-index: 9999999;
                    border-style: none;
                    outline: none;
                    border-radius: 50px;
                    background: #36c8b0;
                    box-shadow:  6px 6px 13px #79e6d3,
                    -6px -6px 13px #ffffff;
                    display: flex;
                    align-items: center;
                    justify-content: center;
                
                #btn-translation
                    color: #0078d4;
                    font-size: 18px;
                    font-family: Playball, cursive;
                
                </style>
                `;
                TranslationButton.addEventListener(\'mouseup\', function (e) 
                    e.stopPropagation();
                );
                TranslationButton.onclick = function () 
                    newAjax();
                ;
             else if (TranslationButton) 
                document.body.removeChild(TranslationButton);
            ;
        );
    ;

以上是关于智能语音点读机的主要内容,如果未能解决你的问题,请参考以下文章

聚焦北京明年科博会-少儿智能教育产品展(主展区)

2017苏州夏日萌宝大赛来啦!晒晒娃,iPad Air迪士尼家庭套票儿童点读机带回家!

巨头汇集教育硬件,智能作业灯何以突围?

新型复读机——微信语音

Qbot6.讯飞文字转语音Api使用/VITS派蒙复读机实现

通知可可宝贝iPad版上线啦!