MCPcopy Create free account
hub / github.com/Bistutu/FluentRead / translateWithMicrosoftInBackground

Function translateWithMicrosoftInBackground

entrypoints/background.ts:11–36  ·  view source on GitHub ↗

* 在background脚本中调用微软翻译API(避免Firefox CORS问题)

(text: string, targetLang: string)

Source from the content-addressed store, hash-verified

9 * 在background脚本中调用微软翻译API(避免Firefox CORS问题)
10 */
11async function translateWithMicrosoftInBackground(text: string, targetLang: string): Promise<string> {
12 try {
13 // 获取微软翻译的JWT令牌
14 const jwtToken = await refreshMicrosoftTokenInBackground();
15
16 // 调用微软翻译API
17 const response = await fetch(`https://api-edge.cognitive.microsofttranslator.com/translate?from=&to=${targetLang}&api-version=3.0&includeSentenceLength=true&textType=html`, {
18 method: 'POST',
19 headers: {
20 'Content-Type': 'application/json',
21 'Authorization': 'Bearer ' + jwtToken
22 },
23 body: JSON.stringify([{ Text: text }])
24 });
25
26 if (response.ok) {
27 const result = await response.json();
28 return result[0].translations[0].text;
29 } else {
30 throw new Error(`微软翻译失败: ${response.status} ${response.statusText}`);
31 }
32 } catch (error) {
33 console.error('微软翻译请求失败:', error);
34 throw error;
35 }
36}
37
38/**
39 * 在background脚本中刷新微软翻译令牌

Callers 1

mainFunction · 0.85

Calls 1

Tested by

no test coverage detected