Skip to content

Instantly share code, notes, and snippets.

@yfdyh000
Created June 2, 2023 20:30
Show Gist options
  • Save yfdyh000/1f8dd321b641623eaf48f259f1015ce7 to your computer and use it in GitHub Desktop.
Save yfdyh000/1f8dd321b641623eaf48f259f1015ce7 to your computer and use it in GitHub Desktop.
QTranslate - Microsoft Translator
var BingKey = '168.....4';
var BingToken = 'VOf0k-....-mKd';
var IID = "&IID=translator.5026";
function serviceHeader() {
return new ServiceHeader(5, "Microsoft", "Microsoft Translator is a cloud service that translates between 60+ languages." + Const.NL2 + serviceHost() + "/translator/" + Const.NL2 + "\u00a9 2021 Microsoft", Capability.TRANSLATE | Capability.DETECT_LANGUAGE)
}
function serviceHost(a, b, c) {
return "https://" + ("cn" === Options.PreferredDomain || "cn" === Options.GoogleDomain ? "cn.bing.com": "www.bing.com")
}
function serviceLink(a, b, c) {
var d = serviceHost() + "/translator";
a && (b = isLanguage(b) ? codeFromLanguage(b) : "-", c = isLanguage(c) ? codeFromLanguage(c) : "-", d += format("?text={0}&from={1}&to={2}", encodeGetParam(a), b, c));
return d
}
SupportedLanguages = [ - 1, "auto-detect", "af", -1, -1, "ar", -1, -1, -1, "bg", "ca", "zh-Hans", "zh-Hant", "hr", "cs", "da", "nl", "en", "et", "fi", "fil", "fr", -1, "de", "el", "ht", "he", "hi", "hu", "is", "id", "it", -1, "ja", -1, "ko", "lv", "lt", -1, "ms", "mt", "no", "fa", "pl", "pt", "ro", "ru", "sr-Cyrl", "sk", "sl", "es", -1, "sv", "th", "tr", "uk", "ur", "vi", "cy", -1, -1, "mww", -1, -1, -1, -1, -1, -1, "te", -1, -1, -1, "ta", -1, "bn-BD", -1];
function serviceRequest(a, b, c) {
a = limitSource(a, 1E3);
var d = "/ttranslatev3?isVertical=1&IG=" + Options.IG + IID;
a = "text=" + encodePostParam(a) + "&fromLang=" + codeFromLanguage(b || AUTO_DETECT_LANGUAGE) + "&to=" + codeFromLanguage(c || ENGLISH_LANGUAGE) + "&token=" + BingToken + "&key=" + BingKey;
b = postHeader() + Const.NL + "Cookie: " + Options.BingCookie + Const.NL + "Origin: https://www.bing.com" + Const.NL + "Referer: https://www.bing.com/translator";
return new RequestData(HttpMethod.POST, d, a, b)
}
function getResponseObj(a) {
if ((a = parseJSON(a)) && 0 < a.length) return a[0]
}
function getSourceLanguage(a) {
return a && a.detectedLanguage ? languageFromCode(a.detectedLanguage.language) : UNKNOWN_LANGUAGE
}
function serviceDetectLanguageRequest(a) {
return serviceRequest(a)
}
function serviceDetectLanguageResponse(a) {
a = getResponseObj(a);
return getSourceLanguage(a)
}
function serviceTranslateRequest(a, b, c) {
return serviceRequest(a, b, c)
}
function serviceTranslateResponse(a, b, c, d) {
var e = getResponseObj(b);
b = "";
e && e.translations && (b = e.translations[0].text);
isLanguage(c) || (c = getSourceLanguage(e));
a = a && !/[\n\r]/.test(a) && 3 >= a.split(" ").length ? "dictionaryRequest": null;
return new ResponseData(b, c, d, null, a)
}
function dictionaryRequest(a, b, c) {
a = limitSource(a, 1E3);
var d = "/tlookupv3?isVertical=1&IG=" + Options.IG + IID;
a = "text=" + encodePostParam(a) + "&from=" + codeFromLanguage(b) + "&to=" + codeFromLanguage(c) + "&token=" + BingToken + "&key=" + BingKey;
b = getHeader() + Const.NL + "Cookie: " + Options.BingCookie + Const.NL + "Origin: https://www.bing.com" + Const.NL + "Referer: https://www.bing.com/translator";
return new RequestData(HttpMethod.POST, d, a, b, null, "dictionaryResponse")
}
function dictionaryResponse(a, b, c, d) {
a = getResponseObj(b);
b = "";
var e, l = {
ADJ: "Adjectives",
ADV: "Adverbs",
CONJ: "Conjunctions",
DET: "Determiners",
MODAL: "Verbs",
NOUN: "Nouns",
PREP: "Prepositions",
PRON: "Pronouns",
VERB: "Verbs"
};
if (a && a.translations) {
var g = {};
for (e = 0; e < a.translations.length; e++) {
var f = a.translations[e],
h = " " + f.displayTarget;
f.backTranslations && (h += " (" + f.backTranslations.map(function(a) {
return a.displayText
}).join(", ") + ")");
g[f.posTag] ? g[f.posTag].push(h) : g[f.posTag] = [h]
}
for (var k in g) b += (l[k] || k) + ":" + Const.NL,
b += g[k].join(Const.NL),
b += Const.NL2
}
b && (b = Const.NL2 + b);
return new ResponseData(b, c, d)
};
// cmd 运行 node updatekey.js
const fs = require('fs');
const https = require('https');
const path = __dirname + '/' + 'Service.js';
// 读取文件内容
const content = fs.readFileSync(path, 'utf8');
function httpsGet(url, callback) {
https.get(url, (res) => {
if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
// 如果响应状态码是 3xx,并且有重定向地址
httpsGet(res.headers.location, callback); // 递归调用 httpsGet 方法
} else {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
callback(null, data);
});
}
}).on('error', (err) => {
callback(err);
});
}
httpsGet('https://www.bing.com/translator', (err, data) => {
if (err) {
console.error(err);
} else {
// 使用正则匹配
const regex = /var params_AbusePreventionHelper\s*=\s*\[(\d+)\s*,\s*"(.+?)"\s*,/i;
const match = data.match(regex);
// 替换文件内容
if (match) {
const newContent = content.replace(/var BingKey = '\d*';/, `var BingKey = '${match[1]}';`)
.replace(/var BingToken = '.*';/, `var BingToken = '${match[2]}';`);
// 写入文件
fs.writeFileSync(path, newContent);
console.log('OK');
} else {
console.log('运行失败');
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment