Rank: Advanced Member
Groups: Moderators, Approved Joined: 4/23/2020(UTC) Posts: 162 ![China China](/Content/images/flags/CN.png) Thanks: 46 times Was thanked: 23 time(s) in 17 post(s)
|
Originally Posted by: Charles ![Go to Quoted Post Go to Quoted Post](/Themes/BlueGrey/icon_latest_reply.png) I can make the cross-domain function work. Could you change it to the Youdao Dict version? With reference to the Tampermonky
Code:sp.Sleep(400);
sp.SendModifiedVKeys([vk.LCONTROL], [vk.VK_C]);
sp.Sleep(100);
var text = clip.GetText();
text = text.replace('\r\n', '');
var currpt = sp.GetCurrentMousePoint();
var r={
"wd":0,//中英文字数
"nwd":0,//英数词数
"nb":0,//数字词数
"c":0,//字符数
"cb":0,//非空格字符
"r":0,//回车
"en":0,//英文字母数
"cn":0,//中文字数
"bl":0//非回车空格
};
var words = text.match(/\w+([’\']\w+)?/g)||[];//含撇号(如I'm)的单词视为一个词
var cnwords = text.match(/[\u4e00-\u9fa5]/g)||[];//统一中文字范围
r.nwd = words.length;
r.cn = cnwords.length;
var httpHandler = new HttpClientHandler();
httpHandler.AutomaticDecompression = host.flags(DecompressionMethods.GZip, DecompressionMethods.Deflate);
var client = new HttpClient(httpHandler);
var response;
var result;
var strTrans = '';
var json;
if(r.nwd == 1) {
client.BaseAddress = new Uri("https://dict.youdao.com/");
response = client.GetAsync("jsonapi?xmlVersion=5.1&jsonversion=2&q=" + text).Result;
result = response.Content.ReadAsStringAsync().Result;
json = JSON.parse(result);
var arr;
if(json.ec) {
arr = json.ec.word[0].trs;
} else {
arr = '';
strTrans = '未识别单词';
}
for(i = 0; i < arr.length; i++) {
if(i < arr.length -1) {
if(arr[i].tr[0].l.i[0].length < 25) {
strTrans = strTrans + arr[i].tr[0].l.i[0].slice(0, 25) + '\r\n';
} else if(arr[i].tr[0].l.i[0].length > 25 && arr[i].tr[0].l.i[0].length < 50) {
strTrans = strTrans + arr[i].tr[0].l.i[0].slice(0, 25) + '\r\n' +
arr[i].tr[0].l.i[0].slice(25, 50) + '\r\n';
} else if(arr[i].tr[0].l.i[0].length > 50 && arr[i].tr[0].l.i[0].length < 75) {
strTrans = strTrans + arr[i].tr[0].l.i[0].slice(0, 25) + '\r\n' +
arr[i].tr[0].l.i[0].slice(25, 50) + '\r\n' +
arr[i].tr[0].l.i[0].slice(50, 75) + '\r\n';
} else if(arr[i].tr[0].l.i[0].length > 75) {
strTrans = strTrans + arr[i].tr[0].l.i[0].slice(0, 25) + '\r\n' +
arr[i].tr[0].l.i[0].slice(25, 50) + '\r\n' +
arr[i].tr[0].l.i[0].slice(50, 75) + '\r\n' +
arr[i].tr[0].l.i[0].slice(75, 100) + '\r\n';
}
} else {
if(arr[i].tr[0].l.i[0].length < 25) {
strTrans = strTrans + arr[i].tr[0].l.i[0].slice(0, 25);
} else if(arr[i].tr[0].l.i[0].length > 25 && arr[i].tr[0].l.i[0].length < 50) {
strTrans = strTrans + arr[i].tr[0].l.i[0].slice(0, 25) + '\r\n' +
arr[i].tr[0].l.i[0].slice(25, 50);
} else if(arr[i].tr[0].l.i[0].length > 50 && arr[i].tr[0].l.i[0].length < 75) {
strTrans = strTrans + arr[i].tr[0].l.i[0].slice(0, 25) + '\r\n' +
arr[i].tr[0].l.i[0].slice(25, 50) + '\r\n' +
arr[i].tr[0].l.i[0].slice(50, 75);
} else if(arr[i].tr[0].l.i[0].length > 75) {
strTrans = strTrans + arr[i].tr[0].l.i[0].slice(0, 25) + '\r\n' +
arr[i].tr[0].l.i[0].slice(25, 50) + '\r\n' +
arr[i].tr[0].l.i[0].slice(50, 75) + '\r\n' +
arr[i].tr[0].l.i[0].slice(75, 100);
}
}
}
} else {
client.BaseAddress = new Uri("https://fanyi.youdao.com/");
response = client.GetAsync("translate?&doctype=json&type=AUTO&i=" + text).Result;
result = response.Content.ReadAsStringAsync().Result;
json = JSON.parse(result);
//StrokesPlus.Console.Log(json.translateResult[0][0].tgt);
var arr;
if(json.translateResult) {
arr = json.translateResult[0][0].tgt;
strTrans = arr;
clip.SetText(arr);
} else {
arr = '';
strTrans = '未识别划词';
}
}
//sp.StoreBool("Trans", true);
//sp.CreateTimer('Trans', 6000, -1, String.raw`sp.StoreBool("Trans", false);sp.DeleteTimer('Trans');`);
if(strTrans != null) {
//clip.SetText(strTrans);
sp.SaveString('strTrans', strTrans);
}
if(strTrans != null) {
var info = new DisplayTextInfo();
info.Opacity = 0.8;
info.Padding = 5;
info.MessageAlignment = 'Left';
info.MessageFont = new Font("Segoe UI Semibold", 8);
info.Location = (currpt.X + 10) + ',' + (currpt.Y + 35);
info.BackColor = "245,245,245";
info.ForeColor = 'black';
info.Duration = 3000;
info.Message = sp.GetSavedString('strTrans').trim();
info.UsePrimaryScreen = true;
sp.DisplayText(info);
}
httpHandler.Dispose();
client.Dispose();
response.Dispose();
Edited by user Sunday, October 9, 2022 1:58:02 PM(UTC)
| Reason: Not specified
|