wwwwwllllk 发表于 2023-2-24 21:17:43

使用auto.js写一个翻译小工具

本帖最后由 wwwwwllllk 于 2023-2-24 21:19 编辑

# 写这个工具的原因
1.在某款app,英语我看不懂,正常情况下它有翻译该段内容的按钮。但是有的时候就没有,我很困扰。每次都得复制出去打开一个app或者网页去翻译一下。很麻烦。

2.如何写英文,搜索输入法自带翻译功能(很nice)


!(data/attachment/forum/202302/24/204950d8kcvl7xnlii9wtl.png)


但是它就没有英语转中文

!(data/attachment/forum/202302/24/205225grgd3i9uukla66vv.png)

!(data/attachment/forum/202302/24/205443mvqucv313svrmqvv.png)



# 思路,踩坑,以及疑惑点
1.我之前写的油猴脚本https://bbs.tampermonkey.net.cn/thread-2326-1-1.html ,思路一模一样(里面有一个MD5工具类这种就是复制过来的,所以不要被吓倒)
2.下面的代码我就把appId,key写为空了,需要的自己去百度翻译申请即可,免费的。
3.这个工具只是解决了我的需求,如果不满足,可以自己去写。
4.工具我用的是auto.jsPro(花钱买的总算是起了一次作用,它的vscode调试工具也比之前好了),auto.js4才是无敌,pro几乎写不了针对tb,pdd,jd等脚本,因为无法对其布局分析,所以大家用auto.js4或者其它开源免费auto.js即可。

## 踩坑点
1.我觉得小白就时常会被一些信息干扰,但是我之前也是这样的。所以我简单的介绍一些东西方便大家入门。
2.auto.js官网   https://pro.autojs.org/docs/zh/v8/dialogs.html#dialogs-select-title-items-callback(auto.js4也就是之前开源的那一款,auto.jspro它们是有区别的,但是不影响我们使用),引擎不一样,它们的区别就是类似于vue2,vue3,vue3兼容了vue2的写法,还衍生了一些新的写法。所以对于我们写一些小工具。我们就用之前的写法就可以。因为是兼容的。然后就是需要什么就去官方文档搜索里面输什么。不懂你在百度看看别人写的。

3. 说下我上面踩的坑。最开始我是没有加悬浮按钮,直接input框输入完成点确定调接口返回接口很顺利。为了用的顺手我加了悬浮按钮(我想让按钮移动,百度到一个复制过来报错了,我没找到解决方法,我就放到一个差不多的位置了),然后就是悬浮按钮点击以后触发弹出input框这里又出问题了。input框还没输完,结框就出来了。

通过百度我发现
```
// 前面加ui以后顺序没问题,但是会在一个新的界面操作,把我app盖住了这肯定不行
// 去掉ui就有问题
"ui";
rawInput("请输入您的名字", "").then(name => {
    alert("您的名字是" + name);
});
```


下面这样写就没问题了
```
rawInput("请输入您的名字", "", name => {
alert("您的名字是" + name);
});
```
4.打包apk安装包以后也不能用,我也不知道什么原因

# 代码
```
window = floaty.rawWindow(
<vertical>
      <button id="按钮"text="翻译" style="Widget.AppCompat.Button.Colored"/>
</vertical>
)
setInterval(() => {}, 1000);
window.setPosition(900, 500);


window.按钮.click(function(){
rawInput("请输入英语", "", inputWords => {
    alert("结果为: " + translateContent(inputWords));
});
});

function translateContent(inputWords) {
var salt = new Date().getTime();
var appid = ""; // 写成自己的
var key = "";   // 写成自己的
if(!appid || !key){
      alert("请自己前往百度翻译平台网站申请appid和key是免费的,最好不要泄露,因为1秒只能调用一次。多人使用可能就会出现问题了")
}
var str1 = appid + inputWords + salt + key;
var sign = MD5(str1);
var from = "en";
var to = "zh";
const res = http.get(`http://api.fanyi.baidu.com/api/trans/vip/translate?q=${inputWords}&appid=${appid}&salt=${salt}&from=${from}&to=${to}&sign=${sign}`);
// console.log(res.body.string())
let result = JSON.parse(res.body.string()).trans_result.dst
return result
}

function MD5(string) {
function RotateLeft(lValue, iShiftBits) {
    return (lValue << iShiftBits) | (lValue >>> (32 - iShiftBits));
}

function AddUnsigned(lX, lY) {
    var lX4, lY4, lX8, lY8, lResult;
    lX8 = lX & 0x80000000;
    lY8 = lY & 0x80000000;
    lX4 = lX & 0x40000000;
    lY4 = lY & 0x40000000;
    lResult = (lX & 0x3fffffff) + (lY & 0x3fffffff);
    if (lX4 & lY4) {
      return lResult ^ 0x80000000 ^ lX8 ^ lY8;
    }
    if (lX4 | lY4) {
      if (lResult & 0x40000000) {
      return lResult ^ 0xc0000000 ^ lX8 ^ lY8;
      } else {
      return lResult ^ 0x40000000 ^ lX8 ^ lY8;
      }
    } else {
      return lResult ^ lX8 ^ lY8;
    }
}

function F(x, y, z) {
    return (x & y) | (~x & z);
}
function G(x, y, z) {
    return (x & z) | (y & ~z);
}
function H(x, y, z) {
    return x ^ y ^ z;
}
function I(x, y, z) {
    return y ^ (x | ~z);
}

function FF(a, b, c, d, x, s, ac) {
    a = AddUnsigned(a, AddUnsigned(AddUnsigned(F(b, c, d), x), ac));
    return AddUnsigned(RotateLeft(a, s), b);
}

function GG(a, b, c, d, x, s, ac) {
    a = AddUnsigned(a, AddUnsigned(AddUnsigned(G(b, c, d), x), ac));
    return AddUnsigned(RotateLeft(a, s), b);
}

function HH(a, b, c, d, x, s, ac) {
    a = AddUnsigned(a, AddUnsigned(AddUnsigned(H(b, c, d), x), ac));
    return AddUnsigned(RotateLeft(a, s), b);
}

function II(a, b, c, d, x, s, ac) {
    a = AddUnsigned(a, AddUnsigned(AddUnsigned(I(b, c, d), x), ac));
    return AddUnsigned(RotateLeft(a, s), b);
}

function ConvertToWordArray(string) {
    var lWordCount;
    var lMessageLength = string.length;
    var lNumberOfWords_temp1 = lMessageLength + 8;
    var lNumberOfWords_temp2 =
      (lNumberOfWords_temp1 - (lNumberOfWords_temp1 % 64)) / 64;
    var lNumberOfWords = (lNumberOfWords_temp2 + 1) * 16;
    var lWordArray = Array(lNumberOfWords - 1);
    var lBytePosition = 0;
    var lByteCount = 0;
    while (lByteCount < lMessageLength) {
      lWordCount = (lByteCount - (lByteCount % 4)) / 4;
      lBytePosition = (lByteCount % 4) * 8;
      lWordArray =
      lWordArray |
      (string.charCodeAt(lByteCount) << lBytePosition);
      lByteCount++;
    }
    lWordCount = (lByteCount - (lByteCount % 4)) / 4;
    lBytePosition = (lByteCount % 4) * 8;
    lWordArray = lWordArray | (0x80 << lBytePosition);
    lWordArray = lMessageLength << 3;
    lWordArray = lMessageLength >>> 29;
    return lWordArray;
}

function WordToHex(lValue) {
    var WordToHexValue = '',
      WordToHexValue_temp = '',
      lByte,
      lCount;
    for (lCount = 0; lCount <= 3; lCount++) {
      lByte = (lValue >>> (lCount * 8)) & 255;
      WordToHexValue_temp = '0' + lByte.toString(16);
      WordToHexValue =
      WordToHexValue +
      WordToHexValue_temp.substr(WordToHexValue_temp.length - 2, 2);
    }
    return WordToHexValue;
}

function Utf8Encode(string) {
    string = string.replace(/\r\n/g, '\n');
    var utftext = '';

    for (var n = 0; n < string.length; n++) {
      var c = string.charCodeAt(n);

      if (c < 128) {
      utftext += String.fromCharCode(c);
      } else if (c > 127 && c < 2048) {
      utftext += String.fromCharCode((c >> 6) | 192);
      utftext += String.fromCharCode((c & 63) | 128);
      } else {
      utftext += String.fromCharCode((c >> 12) | 224);
      utftext += String.fromCharCode(((c >> 6) & 63) | 128);
      utftext += String.fromCharCode((c & 63) | 128);
      }
    }

    return utftext;
}

var x = Array();
var k, AA, BB, CC, DD, a, b, c, d;
var S11 = 7,
    S12 = 12,
    S13 = 17,
    S14 = 22;
var S21 = 5,
    S22 = 9,
    S23 = 14,
    S24 = 20;
var S31 = 4,
    S32 = 11,
    S33 = 16,
    S34 = 23;
var S41 = 6,
    S42 = 10,
    S43 = 15,
    S44 = 21;

string = Utf8Encode(string);

x = ConvertToWordArray(string);

a = 0x67452301;
b = 0xefcdab89;
c = 0x98badcfe;
d = 0x10325476;

for (k = 0; k < x.length; k += 16) {
    AA = a;
    BB = b;
    CC = c;
    DD = d;
    a = FF(a, b, c, d, x, S11, 0xd76aa478);
    d = FF(d, a, b, c, x, S12, 0xe8c7b756);
    c = FF(c, d, a, b, x, S13, 0x242070db);
    b = FF(b, c, d, a, x, S14, 0xc1bdceee);
    a = FF(a, b, c, d, x, S11, 0xf57c0faf);
    d = FF(d, a, b, c, x, S12, 0x4787c62a);
    c = FF(c, d, a, b, x, S13, 0xa8304613);
    b = FF(b, c, d, a, x, S14, 0xfd469501);
    a = FF(a, b, c, d, x, S11, 0x698098d8);
    d = FF(d, a, b, c, x, S12, 0x8b44f7af);
    c = FF(c, d, a, b, x, S13, 0xffff5bb1);
    b = FF(b, c, d, a, x, S14, 0x895cd7be);
    a = FF(a, b, c, d, x, S11, 0x6b901122);
    d = FF(d, a, b, c, x, S12, 0xfd987193);
    c = FF(c, d, a, b, x, S13, 0xa679438e);
    b = FF(b, c, d, a, x, S14, 0x49b40821);
    a = GG(a, b, c, d, x, S21, 0xf61e2562);
    d = GG(d, a, b, c, x, S22, 0xc040b340);
    c = GG(c, d, a, b, x, S23, 0x265e5a51);
    b = GG(b, c, d, a, x, S24, 0xe9b6c7aa);
    a = GG(a, b, c, d, x, S21, 0xd62f105d);
    d = GG(d, a, b, c, x, S22, 0x2441453);
    c = GG(c, d, a, b, x, S23, 0xd8a1e681);
    b = GG(b, c, d, a, x, S24, 0xe7d3fbc8);
    a = GG(a, b, c, d, x, S21, 0x21e1cde6);
    d = GG(d, a, b, c, x, S22, 0xc33707d6);
    c = GG(c, d, a, b, x, S23, 0xf4d50d87);
    b = GG(b, c, d, a, x, S24, 0x455a14ed);
    a = GG(a, b, c, d, x, S21, 0xa9e3e905);
    d = GG(d, a, b, c, x, S22, 0xfcefa3f8);
    c = GG(c, d, a, b, x, S23, 0x676f02d9);
    b = GG(b, c, d, a, x, S24, 0x8d2a4c8a);
    a = HH(a, b, c, d, x, S31, 0xfffa3942);
    d = HH(d, a, b, c, x, S32, 0x8771f681);
    c = HH(c, d, a, b, x, S33, 0x6d9d6122);
    b = HH(b, c, d, a, x, S34, 0xfde5380c);
    a = HH(a, b, c, d, x, S31, 0xa4beea44);
    d = HH(d, a, b, c, x, S32, 0x4bdecfa9);
    c = HH(c, d, a, b, x, S33, 0xf6bb4b60);
    b = HH(b, c, d, a, x, S34, 0xbebfbc70);
    a = HH(a, b, c, d, x, S31, 0x289b7ec6);
    d = HH(d, a, b, c, x, S32, 0xeaa127fa);
    c = HH(c, d, a, b, x, S33, 0xd4ef3085);
    b = HH(b, c, d, a, x, S34, 0x4881d05);
    a = HH(a, b, c, d, x, S31, 0xd9d4d039);
    d = HH(d, a, b, c, x, S32, 0xe6db99e5);
    c = HH(c, d, a, b, x, S33, 0x1fa27cf8);
    b = HH(b, c, d, a, x, S34, 0xc4ac5665);
    a = II(a, b, c, d, x, S41, 0xf4292244);
    d = II(d, a, b, c, x, S42, 0x432aff97);
    c = II(c, d, a, b, x, S43, 0xab9423a7);
    b = II(b, c, d, a, x, S44, 0xfc93a039);
    a = II(a, b, c, d, x, S41, 0x655b59c3);
    d = II(d, a, b, c, x, S42, 0x8f0ccc92);
    c = II(c, d, a, b, x, S43, 0xffeff47d);
    b = II(b, c, d, a, x, S44, 0x85845dd1);
    a = II(a, b, c, d, x, S41, 0x6fa87e4f);
    d = II(d, a, b, c, x, S42, 0xfe2ce6e0);
    c = II(c, d, a, b, x, S43, 0xa3014314);
    b = II(b, c, d, a, x, S44, 0x4e0811a1);
    a = II(a, b, c, d, x, S41, 0xf7537e82);
    d = II(d, a, b, c, x, S42, 0xbd3af235);
    c = II(c, d, a, b, x, S43, 0x2ad7d2bb);
    b = II(b, c, d, a, x, S44, 0xeb86d391);
    a = AddUnsigned(a, AA);
    b = AddUnsigned(b, BB);
    c = AddUnsigned(c, CC);
    d = AddUnsigned(d, DD);
}

var temp = WordToHex(a) + WordToHex(b) + WordToHex(c) + WordToHex(d);

return temp.toLowerCase();
};

```


王一之 发表于 2023-2-24 21:47:45

我记得哥哥昨天还是什么时候说的,今天就好了,执行力真强

wwwwwllllk 发表于 2023-2-24 21:59:18

王一之 发表于 2023-2-24 21:47
我记得哥哥昨天还是什么时候说的,今天就好了,执行力真强

没哥哥厉害!!!!!
页: [1]
查看完整版本: 使用auto.js写一个翻译小工具