Skip to content

Instantly share code, notes, and snippets.

@ewanlee
Created August 24, 2021 06:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ewanlee/3c98fc0718f785dbf2b567105117ce79 to your computer and use it in GitHub Desktop.
Save ewanlee/3c98fc0718f785dbf2b567105117ce79 to your computer and use it in GitHub Desktop.
Listary 6 Beta's Youdao Translate Extension
const axios = require("axios");
var CryptoJS = require("crypto-js");
async function search(query) {
var appKey = '';
var key = '';
var salt = (new Date).getTime();
var curtime = Math.round(new Date().getTime()/1000);
var from = 'en';
var to = 'zh-CHS';
var str1 = appKey + query + salt + curtime + key;
var sign = CryptoJS.SHA256(str1).toString(CryptoJS.enc.Hex);
const response = await axios({
url: 'http://openapi.youdao.com/api',
params: {
q: query,
appKey: appKey,
salt: salt,
from: from,
to: to,
sign: sign,
signType: "v3",
curtime: curtime
}
});
const json = response.data;
if (json.errorCode != "0") {
return [{
title: json.errorCode
}];
}
if (json.basic) {
return [{
title: json.basic.explains.toString()
}];
}
else if (json.translation) {
return [{
title: json.translation[0]
}];
}
else {
return [{
title: "No results."
}];
}
}
module.exports = {
search: search
};
@darkzbaron
Copy link

How do you install this ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment