-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
51 lines (47 loc) · 2.01 KB
/
Copy pathbackground.js
File metadata and controls
51 lines (47 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// 简单粗暴,每次后台都无脑尝试下载一次所有的语言包
// background.js
// 将 await 代码包装在异步函数中
(async function() {
const sourceLanguage = ['en', 'ja', 'ko', 'es'];
const targetLanguage = 'zh';
const code2lang = {
'zh':'中文',
'en': '英语',
'ja': '日语',
'ko': '韩语',
'es': '西班牙语'
};
for (const srcLang of sourceLanguage) {
try {
// 检查语言模型是否可用
const translatorCapabilities = await Translator.availability({
sourceLanguage: srcLang,
targetLanguage: targetLanguage,
});
// 只有当模型不可用时才创建(下载)模型
if (translatorCapabilities !== 'available') {
console.log(`模型 ${srcLang} -> ${targetLanguage} 不可用,准备下载...`);
/*chrome.notifications.create({
type: 'basic',
title: '下载中',
message: `AI模型 ${codecode2lang2str[srcLang]} 翻译 ${code2lang[targetLanguage]} 不可用,自动下载...`,
iconUrl: 'icon48.png'
});*/
console.log(`AI模型 ${code2lang[srcLang]} 翻译 ${code2lang[targetLanguage]} 不可用,自动下载...`);
await Translator.create({
sourceLanguage: srcLang,
targetLanguage: targetLanguage,
monitor(m) {
m.addEventListener('downloadprogress', (e) => {
console.log(`Downloading ${srcLang} to ${targetLanguage}, progress: ${e.loaded * 100}%`);
});
},
});
} else {
console.log(`模型 ${srcLang} -> ${targetLanguage} 已可用,跳过下载`);
}
} catch (error) {
console.error(`检查或下载语言模型失败 ${srcLang}:`, error);
}
}
})();