feat: add new google translate api (issue: 225, by: Bush2021)
This commit is contained in:
@@ -92,6 +92,7 @@ const userscriptWebpack = (config, env) => {
|
||||
// @grant GM.info
|
||||
// @grant unsafeWindow
|
||||
// @connect translate.googleapis.com
|
||||
// @connect translate-pa.googleapis.com
|
||||
// @connect api-edge.cognitive.microsofttranslator.com
|
||||
// @connect edge.microsoft.com
|
||||
// @connect api-free.deepl.com
|
||||
@@ -108,6 +109,7 @@ const userscriptWebpack = (config, env) => {
|
||||
// @connect dav.jianguoyun.com
|
||||
// @connect fanyi.baidu.com
|
||||
// @connect transmart.qq.com
|
||||
// @connect niutrans.com
|
||||
// @connect localhost:3000
|
||||
// @connect 127.0.0.1:3000
|
||||
// @connect localhost:1188
|
||||
|
||||
@@ -2,6 +2,7 @@ import queryString from "query-string";
|
||||
import { fetchData } from "../libs/fetch";
|
||||
import {
|
||||
OPT_TRANS_GOOGLE,
|
||||
OPT_TRANS_GOOGLE2,
|
||||
OPT_TRANS_MICROSOFT,
|
||||
OPT_TRANS_DEEPL,
|
||||
OPT_TRANS_DEEPLFREE,
|
||||
@@ -259,6 +260,10 @@ export const apiTranslate = async ({
|
||||
trText = res.sentences.map((item) => item.trans).join(" ");
|
||||
isSame = to === res.src;
|
||||
break;
|
||||
case OPT_TRANS_GOOGLE2:
|
||||
trText = res?.[0]?.[0]||"";
|
||||
isSame = to === res.src;
|
||||
break;
|
||||
case OPT_TRANS_MICROSOFT:
|
||||
trText = res
|
||||
.map((item) => item.translations.map((item) => item.text).join(" "))
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import queryString from "query-string";
|
||||
import {
|
||||
OPT_TRANS_GOOGLE,
|
||||
OPT_TRANS_GOOGLE2,
|
||||
OPT_TRANS_MICROSOFT,
|
||||
OPT_TRANS_DEEPL,
|
||||
OPT_TRANS_DEEPLFREE,
|
||||
@@ -80,6 +81,20 @@ const genGoogle = ({ text, from, to, url, key }) => {
|
||||
return [input, init];
|
||||
};
|
||||
|
||||
const genGoogle2 = ({ text, from, to, url, key }) => {
|
||||
const body = JSON.stringify([[ [text], from, to ], "wt_lib"]);
|
||||
const init = {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json+protobuf",
|
||||
"X-Goog-API-Key": key,
|
||||
},
|
||||
body,
|
||||
};
|
||||
|
||||
return [url, init];
|
||||
};
|
||||
|
||||
const genMicrosoft = async ({ text, from, to }) => {
|
||||
const [token] = await msAuth();
|
||||
const params = {
|
||||
@@ -440,6 +455,8 @@ export const genTransReq = ({ translator, text, from, to }, apiSetting) => {
|
||||
switch (translator) {
|
||||
case OPT_TRANS_GOOGLE:
|
||||
return genGoogle(args);
|
||||
case OPT_TRANS_GOOGLE2:
|
||||
return genGoogle2(args);
|
||||
case OPT_TRANS_MICROSOFT:
|
||||
return genMicrosoft(args);
|
||||
case OPT_TRANS_DEEPL:
|
||||
|
||||
@@ -89,7 +89,8 @@ export const URL_MICROSOFT_LANGDETECT =
|
||||
|
||||
export const URL_GOOGLE_TRAN =
|
||||
"https://translate.googleapis.com/translate_a/single";
|
||||
|
||||
export const URL_GOOGLE_TRAN2 =
|
||||
"https://translate-pa.googleapis.com/v1/translateHtml";
|
||||
export const DEFAULT_GOOGLE_API_KEY = "AIzaSyATBXajvzQLTDHEQbcpq0Ihe0vWDHmO520";
|
||||
|
||||
export const URL_BAIDU_LANGDETECT = "https://fanyi.baidu.com/langdetect";
|
||||
@@ -109,6 +110,7 @@ export const DEFAULT_USER_AGENT =
|
||||
export const OPT_DICT_BAIDU = "Baidu";
|
||||
|
||||
export const OPT_TRANS_GOOGLE = "Google";
|
||||
export const OPT_TRANS_GOOGLE2 = "Google2";
|
||||
export const OPT_TRANS_MICROSOFT = "Microsoft";
|
||||
export const OPT_TRANS_DEEPL = "DeepL";
|
||||
export const OPT_TRANS_DEEPLX = "DeepLX";
|
||||
@@ -132,6 +134,7 @@ export const OPT_TRANS_CUSTOMIZE_4 = "Custom4";
|
||||
export const OPT_TRANS_CUSTOMIZE_5 = "Custom5";
|
||||
export const OPT_TRANS_ALL = [
|
||||
OPT_TRANS_GOOGLE,
|
||||
OPT_TRANS_GOOGLE2,
|
||||
OPT_TRANS_MICROSOFT,
|
||||
OPT_TRANS_BAIDU,
|
||||
OPT_TRANS_TENCENT,
|
||||
@@ -204,6 +207,7 @@ export const OPT_LANGS_TO = [
|
||||
export const OPT_LANGS_FROM = [["auto", "Auto-detect"], ...OPT_LANGS_TO];
|
||||
export const OPT_LANGS_SPECIAL = {
|
||||
[OPT_TRANS_GOOGLE]: new Map(OPT_LANGS_FROM.map(([key]) => [key, key])),
|
||||
[OPT_TRANS_GOOGLE2]: new Map(OPT_LANGS_FROM.map(([key]) => [key, key])),
|
||||
[OPT_TRANS_MICROSOFT]: new Map([
|
||||
...OPT_LANGS_FROM.map(([key]) => [key, key]),
|
||||
["auto", ""],
|
||||
@@ -553,10 +557,15 @@ export const DEFAULT_TRANS_APIS = {
|
||||
[OPT_TRANS_GOOGLE]: {
|
||||
url: URL_GOOGLE_TRAN,
|
||||
key: "",
|
||||
// key: DEFAULT_GOOGLE_API_KEY,
|
||||
fetchLimit: DEFAULT_FETCH_LIMIT, // 最大任务数量
|
||||
fetchInterval: DEFAULT_FETCH_INTERVAL, // 任务间隔时间
|
||||
},
|
||||
[OPT_TRANS_GOOGLE2]: {
|
||||
url: URL_GOOGLE_TRAN2,
|
||||
key: DEFAULT_GOOGLE_API_KEY,
|
||||
fetchLimit: DEFAULT_FETCH_LIMIT,
|
||||
fetchInterval: DEFAULT_FETCH_INTERVAL,
|
||||
},
|
||||
[OPT_TRANS_MICROSOFT]: {
|
||||
fetchLimit: DEFAULT_FETCH_LIMIT,
|
||||
fetchInterval: DEFAULT_FETCH_INTERVAL,
|
||||
|
||||
Reference in New Issue
Block a user