Created
August 24, 2021 06:51
-
-
Save ewanlee/3c98fc0718f785dbf2b567105117ce79 to your computer and use it in GitHub Desktop.
Listary 6 Beta's Youdao Translate Extension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How do you install this ?