Skip to content

Commit

Permalink
fix: "microsoft" web page translating source might request authorizat…
Browse files Browse the repository at this point in the history
…ion for multiple times
  • Loading branch information
chunibyocola committed Aug 17, 2021
1 parent a8244b4 commit 8ede34b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 30 deletions.
52 changes: 30 additions & 22 deletions src/public/web-page-translate/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { GOOGLE_COM, MICROSOFT_COM } from '../../constants/translateSource';
import { bingSwitchLangCode } from '../switch-lang-code';
import { translate as googleWebTranslate } from './google/translate';
import { getAuthorization } from './microsoft/getAuthorization';
import { translate as microsoftWebTranslate } from './microsoft/translate';

type PageTranslateItemEnity = {
Expand Down Expand Up @@ -351,31 +352,38 @@ const microsoftWebTranslateProcess = (nextTranslateList: PageTranslateItemEnity[

const tempCloseFlag = closeFlag;

translateList.forEach((item) => {
const dealWithResult = (result: string[][]) => {
item.pageTranslateList.forEach((v, i) => {
v.result = result[i];
v.status = 'finished';
v.textNodes.forEach((textNode, i) => {
if (!textNode.parentElement || !v.result?.[i]) { return; }

const fonts = insertResultAndWrapOriginalTextNode(textNode, v.result[i]);
fonts && v.fontsNodes.push(fonts);
getAuthorization().then(() => {
translateList.forEach((item) => {
const dealWithResult = (result: string[][]) => {
item.pageTranslateList.forEach((v, i) => {
v.result = result[i];
v.status = 'finished';
v.textNodes.forEach((textNode, i) => {
if (!textNode.parentElement || !v.result?.[i]) { return; }

const fonts = insertResultAndWrapOriginalTextNode(textNode, v.result[i]);
fonts && v.fontsNodes.push(fonts);
});
});
};

microsoftWebTranslate(item.requestArray, targetLanguage).then((result) => {
item.textList.length === result.length && item.textList.forEach((v, i) => (resultCache[v] = result[i]));

// if not the same, means web page translate has been closed.
tempCloseFlag === closeFlag && dealWithResult(result);
}).catch((reason) => {
item.pageTranslateList.forEach(v => v.status = 'error');
errorCallback?.(reason.code);
});
};

microsoftWebTranslate(item.requestArray, targetLanguage).then((result) => {
item.textList.length === result.length && item.textList.forEach((v, i) => (resultCache[v] = result[i]));

// if not the same, means web page translate has been closed.
tempCloseFlag === closeFlag && dealWithResult(result);
}).catch((reason) => {
item.pageTranslateList.forEach(v => v.status = 'error');
errorCallback?.(reason.code);

item.pageTranslateList.forEach(v => v.status = 'loading');
});

item.pageTranslateList.forEach(v => v.status = 'loading');
}).catch(() => {
translateList.forEach((list) => {
list.pageTranslateList.forEach(v => v.status = 'error');
});
errorCallback?.('Microsoft: get authorization failed.');
});
};

Expand Down
15 changes: 13 additions & 2 deletions src/public/web-page-translate/microsoft/getAuthorization.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
import { fetchData } from '../../translate/utils';

export const getAuthorization = async () => {
let authorization = '';
let expiry = 0;

export const getAuthorization = async (force = false) => {
const timestamp = Number(new Date());

if (!force && expiry > timestamp && authorization) { return authorization; }

const url = 'https://edge.microsoft.com/translate/auth';

const res = await fetchData(url);
const authorization = await res.text();

authorization = await res.text();
expiry = timestamp + 500000;

return authorization;
};
8 changes: 2 additions & 6 deletions src/public/web-page-translate/microsoft/translate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import { getAuthorization } from './getAuthorization';
import { langCode } from '../../translate/bing/lang-code';
import { unescapeText } from '..';

let authorization = '';

export const translate = async (requestArray: { Text: string }[], targetLanguage: string): Promise<string[][]> => {
if (!(targetLanguage in langCode)) { throw getError(LANGUAGE_NOT_SOPPORTED); }

Expand All @@ -16,7 +14,7 @@ export const translate = async (requestArray: { Text: string }[], targetLanguage
return dealWithResult(data);
}
else if (data?.error?.code === 401000) {
authorization = await getAuthorization();
await getAuthorization(true);
data = await fetchFromMicrosoft(requestArray, targetLanguage);
if (Array.isArray(data)) {
return dealWithResult(data);
Expand All @@ -39,9 +37,7 @@ const dealWithResult = (result: { translations: [{ text: string }] }[]) => {
};

const fetchFromMicrosoft = async (requestArray: { Text: string }[], targetLanguage: string) => {
if (!authorization) {
authorization = await getAuthorization();
}
const authorization = await getAuthorization();

const url = `https://api.cognitive.microsofttranslator.com/translate?to=${targetLanguage}&api-version=3.0`;

Expand Down

0 comments on commit 8ede34b

Please sign in to comment.