本帖最后由 梦汐 于 2024-2-8 18:37 编辑
[JavaScript] 纯文本查看 复制代码 class edge {
constructor() {
(new Promise(
complete => GM_xmlhttpRequest({
method: 'GET',
url: 'https://edge.microsoft.com/translate/auth',
onload: (response) => {
let data = response.responseText
this.token = data
complete(data)
}
})
)).then(
token => {
this.token = token
console.log(token)
}
)
}
transl(array) {
return new Promise(
async complete => {
await new Promise(
wait => {
let Interval = setInterval(((_this) => {
return function () {
console.log("token" in _this)
if ("token" in _this) {
wait(clearInterval(Interval))
}
}
})(this), 100)
}
)
let body = []
array.forEach(value => {
body.push({ "Text": value })
})
GM_xmlhttpRequest({
method: 'POST',
url: 'https://api.cognitive.microsofttranslator.com/translate?from=en&to=zh-CHS&api-version=3.0&includeSentenceLength=true',
headers: {
'Accept': '*/*',
'Accept-Encoding': 'gzip, deflate, br',
'Connection': 'keep-alive',
'Content-Type': 'application/json',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 Edg/107.0.1418.24',
'Authorization': 'Bearer ' + this.token
},
data: JSON.stringify(body),
onload: function (response) {
let data = JSON.parse(response.responseText);
let join = []
data.forEach(
(value) => {
join.push(value.translations[0].text)
}
)
complete(join)
}
})
}
)
}
}
let a = new edge()
console.log(await a.transl(['hi']));
|