选中文本智能语音播报,脚本内填写正确appid与密钥后开启翻译即可(默认关闭)
// ==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?",
onl oad: 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);
};
});
};
}