// ==UserScript==
// @name 滑词答题
// @namespace ###
// @description 选中文字一会放开自动翻译
// @author ###
// @include *
// @version 1.0.0
// @grant GM_xmlhttpRequest
// @grant GM_addStyle
// @require https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js
// @require fs
// @license MIT
// @downloadURL https://update.greasyfork.org/scripts/399313/%E5%88%92%E8%AF%8D%E7%BF%BB%E8%AF%91.user.js
// @updateURL https://update.greasyfork.org/scripts/399313/%E5%88%92%E8%AF%8D%E7%BF%BB%E8%AF%91.meta.js
// ==/UserScript==
(function() {
'use strict';
var gv = {toLanguage:'zh-CN',isAutoCopy:true};
if (/zh/i.test(navigator.language)) gv.toLanguage='zh-CN';
else if (/ja/i.test(navigator.language)) gv.toLanguage='ja';
else if (/en/i.test(navigator.language)) gv.toLanguage='en';
function init() {
$(document).ready(function() {
var ctrlDown = false,
altDown = false,
ctrlKey = 17,
cmdKey = 91,
altKey = 18,
vKey = 86,
cKey = 67;
$(document).keydown(function(e) {
if (e.keyCode == ctrlKey || e.keyCode == cmdKey) ctrlDown = true;
if (e.keyCode == altKey) altDown = true;
}).keyup(function(e) {
if (e.keyCode == ctrlKey || e.keyCode == cmdKey) ctrlDown = false;
if (e.keyCode == altKey) altDown = false;
});
$(".no-copy-paste").keydown(function(e) {
if (ctrlDown && (e.keyCode == vKey || e.keyCode == cKey)) return false;
});
$(document).keydown(function(e) {
if (ctrlDown && (e.keyCode == cKey)) {gv.isAutoCopy = false};
if (altDown && (e.keyCode == cKey)){gv.isAutoCopy = true};
//if (ctrlDown && (e.keyCode == vKey)) console.log("Document catch Ctrl+V");
});
});
document.addEventListener('mousedown', function(e){
if (!$('#changetranslatebox') || $('#changetranslatebox').length == 0) {
createDisplay();
$('#changetranslatebox li').on('click', function(e){
gv.toLanguage = e.target.getAttribute('name')
$('.translatedropdown').css('display', '');
_translate();
$('.translatetext').text(e.target.innerText + ' SET SUCCESS!');
setTimeout(function() { $('.translatetext').empty();}, 1000);
});
}
if ($('#changetranslatebox').css('display') == 'block' && !checkClick(e)) {
$('#changetranslatebox').css('display', '');
$('.translatedropdown').css('display', '');
$('.translatetext').empty();
$('.translateexplain').empty();
}
clearTimeout(gv.timer);
gv.holdTime = false;
gv.timer = setTimeout(function() { gv.holdTime = true; }, 1000);
if (e.target.className == 'translateplaysound') {
$('.translateplaysound').addClass('translateplaysoundClick');
playSound();
} else if (e.target.className == 'translatelanguageset') {
$('.translatedropdown').css('display', 'block');
}
}, true);
document.addEventListener('mouseup', function(e){
if (gv.holdTime == true && window.getSelection().toString()) {
e.preventDefault()
e.stopPropagation();
gv.holdTime = false;
showBox(e.clientX, e.clientY);
gv.selectText = window.getSelection().toString();
//标记选择
console.log("选择文字",gv.selectText)
gv.encodeText = encodeURIComponent(gv.selectText);
_translate();
}
if ($('.translateplaysoundClick') && $('.translateplaysoundClick').length > 0) {
$('.translateplaysound').removeClass('translateplaysoundClick');
}
clearTimeout(gv.timer);
}, true);
}
function createDisplay() {
setStyle(['#changetranslatebox * {margin:0;padding:0;box-sizing:border-box;}','#changetranslatebox {min-height:24px;min-width:100px;max-width:360px;font:normal 12px/24px Helvetica, Tahoma, Arial, sans-serif;text-align: left;position: absolute;z-index: 2147483647;top: 22px;left: -35px;background: #fff;border: 1px solid #dcdcdc;-webkit-transition: opacity .218s;transition: opacity .218s;box-shadow: 0 1px 4px rgba(0,0,0,.2);padding: 5px 0;display: none;font-size: 12px;line-height: 20px;border-radius:3px;}','#changetranslatebox .translatecontentbox {margin:0 8px;color:#333;}','#changetranslatebox .translatecontentbox .translatetextbox{line-height:16px;border-bottom: 1px solid #ccc;padding: 2px 18px 9px 0;height: 25px;}','#changetranslatebox .translatecontentbox .translatetextbox div{vertical-align: middle;margin-right: 4px;color:#333;font-weight: normal;font-size:12px;}','#changetranslatebox .translatecontentbox .translatetextbox .translatetext{display: inline-block;font-size:14px;font-weight: bold;color:#333;}','#changetranslatebox .translatecontentbox .translatetextbox .translateplaysound {margin-left: 1px;cursor:pointer;display: inline-block;vertical-align: middle;width: 14px;height: 11px;overflow: hidden;background: url("data:image/gif;base64,R0lGODlhDgAZAIAAABy3/f///yH5BAAAAAAALAAAAAAOABkAAAI1jA+nC7ncXmg0RlTvndnt7jlcxkmjqWzotLbui4qxqBpUmoDl2Nk5GOKRSsCfDyer7ZYMSQEAOw==") no-repeat;text-decoration: none;}','#changetranslatebox .translatecontentbox .translatetextbox .translateplaysound.translateplaysoundClick {background-position:0 -14px;}','#changetranslatebox .translatecontentbox .translateexplain{padding: 2px 0 0 0;font-weight: normal;font-size:12px;}','#changetranslatebox .translatetiparrow {width: 0;height: 0;font-size: 0;line-height: 0;display: block;position: absolute;top: -16px;left: 10px;}','#changetranslatebox .translatetiparrow em, #changetranslatebox .translatetiparrow ins {width: 0;height: 0;font-size: 0;line-height: 0;display: block;position: absolute;border: 8px solid transparent;border-style: dashed dashed solid;}','#changetranslatebox .translatetiparrow em {border-bottom-color: #d8d8d8;font-style: normal;color: #c00;}','#changetranslatebox .translatetiparrow ins {border-bottom-color: #fff;top: 2px;text-decoration: underline;background:none !important}','#changetranslatebox .translatelanguageset {position:absolute;top:9px;right:10px;cursor: pointer;width: 14px;height: 14px;background: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA4AAAAOCAMAAAAolt3jAAAAdVBMVEUAAAAwi/+Zxv9urv9oq/9pq/9Elv81jf89kv8wiv8+kv8wiv8xi/8wi/9/t/9+t/9co/9Zof9Hl/9Gl/9ClP9Bk/85j//k8f/Z6v+Fu//x+P/e7f/b6//G3/+/2/+w0/+q0P+52P+42P+Lvv+IvP9wr/9vr/864/KKAAAAF3RSTlMAR/7s7OK7l5VuTyMTC/z7y8ihnYSAQ/Vmp/0AAAB9SURBVAjXVY9HDsQwDAPpVKf32E7v/3/iGtJhk7kNIIgkLH0QA3EgQHTX7AnhzWdLKo9hMWYZdkmaFFpZdJ6QRo7afH9TTmSlqcy4hmkarqMpazxq0m4GZK6e1I37rQ/q8n9cNZ9XHJRzUMFBcucaB9doTy55dSAET+gB/ABPjgqB+Q/YPgAAAABJRU5ErkJggg==") no-repeat;text-decoration: none;}','#changetranslatebox .translatelanguageset .translatedropdown {margin:0;padding:0;display:none;top:13px;right:-60px;position: absolute;background-color: #ffffff;width: 68px;overflow: auto;z-index: 1;border: 1px solid rgba(0,0,0,.2);box-shadow: 0 2px 4px rgba(0,0,0,.2);}','#changetranslatebox .translatelanguageset .translatedropdown li {list-style-type:none; color: black;padding: 6px 8px;margin:0px;text-decoration: none;display: block;text-align:center;}','#changetranslatebox .translatelanguageset .translatedropdown li:hover { background-color: #f1f1f1;}']);
$('<div id="changetranslatebox"><div class="translatecontentbox"><div class="translatetextbox"><div class="translatetext"></div><div class="translateplaysound"></div></div><div class="translateexplainbox"><div class="translateexplain"></div><div class="translateplaysound"></div></div></div><div class="translatetiparrow"><em></em><ins></ins></div><div class="translatelanguageset"><ul class="translatedropdown"><li name="zh-CN">中文</li><li name="ja">自动答案</li><li name="en">English</li></ul></div></div>').appendTo($(document.body));
}
function showBox(mouseX, mouseY) {
var changetranslatebox = document.getElementById('changetranslatebox');
var selectedRect = window.getSelection().getRangeAt(0).getBoundingClientRect();
var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
if (selectedRect.width) {if (getComputedStyle(document.body).position != 'static') {changetranslatebox.style.top = selectedRect.bottom - document.body.getBoundingClientRect().top + 8 + 'px';} else {changetranslatebox.style.top = selectedRect.bottom + scrollTop + 8 + 'px';}changetranslatebox.style.left = selectedRect.left + selectedRect.width / 2 - 18 + 'px';} else {changetranslatebox.style.top = mouseY - document.body.getBoundingClientRect().y + selectedRect.height + 8 + 'px';changetranslatebox.style.left = mouseX + selectedRect.width / 2 - 18 + 'px';}
changetranslatebox.style.display = 'block';
}
function setStyle(css){
if(typeof(css) == "string") GM_addStyle(css);
else for(var i=0;i<css.length;i++){GM_addStyle(css[i]) }
}
//中文表达式
var pattern = new RegExp("[\u4e00-\u9fa5]+");
function _translate() {
googleTrans.Execute(pattern.test(gv.selectText)?gv.encodeText:gv.selectText, 'auto', gv.toLanguage, (ret)=>{
parseResult(ret)
})
}
//输出文体
function parseResult(result) {
var explains = '';
var obj = result;
try{
if( typeof(obj) == 'string') gv.explains = obj;
else gv.explains = obj.join('\n');
$('.translateexplain').text(gv.explains);
if (navigator.clipboard && gv.isAutoCopy) {
//输出文字
navigator.clipboard.writeText(gv.selectText+'\n'+gv.explains);
}
}catch(e){
$('.translateexplain').text('翻译失败');
}
}
function checkClick(e) {
var path = e.path || e.composedPath();
if (path.indexOf($('#changetranslatebox').get(0)) > -1) return true;
else return false;
}
//朗读
function playSound(arraybuffer) {
var DAN =fs.readFile('./files/01.txt', 'utf8');
console.log(DAN);
console.log(codeInfo);
console.log(dataStr);
}
//答案
var DaAn ={
}
// 引入文件系统模块
// const fs = require('fs');
// 读取本地CSV文件
//fs.readFile('data.csv', 'utf-8', (err, data) => {
// if (err) throw err;
// 将CSV文件转换为JSON格式
// const jsonData = csvToJson(data);
//文件转化
function csvJSON(csv) {
var lines = csv.split("\n");
var result = [];
var headers = lines[0].split(",");
for (var i = 1; i < lines.length; i++) {
var obj = {};
var currentline = lines[i].split(",");
for (var j = 0; j < headers.length; j++) {
obj[headers[j].trim()] = currentline[j];
}
result.push(obj);
}
return result;
}
//谷歌翻译
var googleTrans = {
Execute: function (selectText, fromLanguage, toLanguage, h_onloadfn) {
GM_xmlhttpRequest({
method: "POST",
url: "https://translate.google.cn/_/TranslateWebserverUi/data/batchexecute",
headers: {
"Referer": `https://translate.google.cn/`,
"Cache-Control": "max-age=0",
"Content-Type": "application/x-www-form-urlencoded;charset=utf-8",
},
data: "f.req=" + (JSON.stringify([
[
["MkEWBc", JSON.stringify([
[selectText, fromLanguage, toLanguage, true],
[null]
]), null, "generic"]
]
])),
onl oad: function (r) {
setTimeout(function () {
try{
var resData=r.responseText;
//console.log("返回信息",resData)
var transData=JSON.parse(JSON.parse(resData.match(/\[{2}.*\]{2}/g)[0])[0][2]);
var transList=transData[1][0][0][5];
var transTexts=[];
for (let index = 0; index < transList.length; index++) {
var transItem = transList[index];
transTexts.push(transItem[0]);
}
h_onloadfn(transTexts);
console.log("返回信息",transTexts)
}catch(e){
h_onloadfn(gv.selectText);
}
}, 300);
},
one rror: function (e) {
console.log("谷歌错误")
console.error(e);
}
});
},
};
})();
标签:function,gv,答题,changetranslatebox,var,document,滑词,display
From: https://www.cnblogs.com/jubai/p/17904866.html