StrokesPlus.net教程及脚本持续更新

划词翻译 v0.3

划词后,中文Google翻译,英文句子Google翻译,英文单词有道翻译,结果保存剪切板,同时显示在鼠标处

自动6秒关闭结果显示,鼠标点击和滚动关闭显示需要在鼠标事件中设置(见下方)

由于是使用自带的sp.DisplayText显示结果,需要截取结果,当前长句中英、英中翻译结果换行是根据长度截取,效果不好,暂时没有更好的显示办法,如果使用其他显示,需要调整,不影响直接从剪切板粘贴

sp.Sleep(400);
sp.SendModifiedVKeys([vk.LCONTROL], [vk.VK_C]);
sp.Sleep(100);
var text = clip.GetText();
text = text.replace('\r\n', '');
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.cn > 0) {
    client.BaseAddress = new Uri("https://translate.googleapis.com/translate_a/");
    response = client.GetAsync("single?client=gtx&sl=zh-CN&tl=en&dt=t&q=" + text).Result;
    result = response.Content.ReadAsStringAsync().Result;
    json = JSON.parse(result);
    for (var i = 0; i < json[0].length; i++) {
        strTrans += json[0][i][0];
    }

    clip.SetText(strTrans);
    strTrans = strTrans.replace(/(\S+\s*){1,10}/g, "$&\n").trim();
} else if(r.nwd > 1) {
    client.BaseAddress = new Uri("https://translate.googleapis.com/translate_a/");
    response = client.GetAsync("single?client=gtx&sl=auto&tl=zh-CN&dt=t&q=" + text).Result;
    result = response.Content.ReadAsStringAsync().Result;
    json = JSON.parse(result);
    for (var i = 0; i < json[0].length; i++) {
        strTrans += json[0][i][0];
    }
    clip.SetText(strTrans);
    if(strTrans.length < 25) {
        strTrans = strTrans;
    } else if(strTrans.length > 25 && strTrans.length <= 50) {
        strTrans = strTrans.slice(0, 25) + '\r\n' +
                                strTrans.slice(25, 50);
    } else if(strTrans.length > 50 && strTrans.length <= 75) {
        strTrans = strTrans.slice(0, 25) + '\r\n' +
                                strTrans.slice(25, 50) + '\r\n' +
                                strTrans.slice(50, 75);
    } else if(strTrans.length > 75 && strTrans.length <= 100) {
        strTrans = strTrans.slice(0, 25) + '\r\n' +
                                strTrans.slice(25, 50) + '\r\n' +
                                strTrans.slice(50, 75) + '\r\n' +
                                strTrans.slice(75, 100);
    } else if(strTrans.length > 100 && strTrans.length <= 125) {
        strTrans = strTrans.slice(0, 25) + '\r\n' +
                                strTrans.slice(25, 50) + '\r\n' +
                                strTrans.slice(50, 75) + '\r\n' +
                                strTrans.slice(75, 100) + '\r\n' +
                                strTrans.slice(100, 125);
    } else if(strTrans.length > 125 && strTrans.length <= 150) {
        strTrans = strTrans.slice(0, 25) + '\r\n' +
                                strTrans.slice(25, 50) + '\r\n' +
                                strTrans.slice(50, 75) + '\r\n' +
                                strTrans.slice(75, 100) + '\r\n' +
                                strTrans.slice(100, 125) + '\r\n' +
                                strTrans.slice(125, 150);
    } else if(strTrans.length > 150) {
        strTrans = strTrans.slice(0, 25) + '\r\n' +
                                strTrans.slice(25, 50) + '\r\n' +
                                strTrans.slice(50, 75) + '\r\n' +
                                strTrans.slice(75, 100) + '\r\n' +
                                strTrans.slice(100, 125) + '\r\n' +
                                strTrans.slice(125, 150) + '\r\n' +
                                strTrans.slice(150, 175);
    }


} else 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 = json.ec.word[0].trs;
    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);
            }
        }
    }
}

//StrokesPlus.Console.Log(strTrans);
if(strTrans != null) {
    sp.StoreString('strTrans', strTrans);
}

sp.StoreBool("Trans", true);
sp.CreateTimer('Trans', 6000, -1, String.raw`sp.StoreBool("Trans", false);sp.DeleteTimer('Trans');`);      

httpHandler.Dispose();
client.Dispose();
response.Dispose();


第二部分

添加到全局动作-启动/退出-启动(开启)

重要:如果已经添加过DisplayTextInfo的LangWatch计时器,直接复制添加 var currentMouseLocation} 之间的代码(即开始结束之间)

sp.CreateTimer("LangWatch",
               250,
               250,
               `var info = new DisplayTextInfo();
                 
               info.TitleFont = new Font('Segoe UI', 12, host.flags(FontStyle.Bold));
               info.MessageFont = new Font("Segoe UI Semibold", 12);
               info.BackColor = "15,179,98";  //绿
               info.ForeColor = "255,255,255";
               info.Duration = 1000;
               info.Opacity = 0.5; //透明度
               info.Location = "190,809"; //翻译结果显示坐标,相对于屏幕分辨率,当前是屏幕左下角
               //info.Location = "bottom";
               //info.FadeSteps = 10;
               info.Padding = 10;
               info.Message = "";

                //开始
              var currentMouseLocation = sp.GetCurrentMousePoint();
              var currpt = sp.GetStoredPoint("mouseClick"); 
              if(sp.GetStoredBool("Trans")) {
                        info.Opacity = 0.6;
                        info.Padding = 5;   
                        info.MessageAlignment = 'Left';
                        info.MessageFont = new Font("Segoe UI Semibold", 8); //结果字体大小,13寸屏幕检验8/10,大于13寸建议12/14
                        //info.Location = (currpt.X + 10) + ',' + (currpt.Y + 35); //鼠标点击位置,需要在鼠标左键点击脚本中添加 var currentMouseLocation = sp.GetCurrentMousePoint(); sp.StorePoint("mouseClick", currentMouseLocation);
                        info.Location = (currentMouseLocation.X + 10) + ',' + (currentMouseLocation.Y + 35); //鼠标位置
                        info.BackColor = "245,245,245"; 
                        info.ForeColor = 'black';
                        //info.Duration = 3000;
                        //info.Message = clip.GetText();
                        //info.Message = sp.GetSavedString('strTrans');
                        info.Message = sp.GetStoredString('strTrans');
                   }
                //结束

                if(info.Message.length > 0) {
                    if(sp.GetStoredBool("LangShown")) {
                        sp.DisplayTextUpdate(info);
                    } else {
                        sp.DisplayText(info);
                        sp.StoreBool("LangShown", true);
                    }
                } else {
                    //sp.DisplayTextClose();
                    sp.StoreBool("LangShown", false);
                }
               `
);

左键点击关闭

添加到鼠标事件-左键单击 sp.DeleteStoredString('strTrans');

鼠标滚轮关闭

添加到鼠标事件-鼠标滚轮最下方else里面sp.DeleteStoredString('strTrans');

中英文有道翻译

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();