Skip to content

Commit d90bd9f

Browse files
committed
fix: 优化转换器下载源的默认设置,使用最快的下载源。并在设置界面添加一个选择最快源的按钮
1 parent 9730288 commit d90bd9f

File tree

10 files changed

+70
-9
lines changed

10 files changed

+70
-9
lines changed

addon/chrome/content/preferences-main.xhtml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@
132132
/>
133133
</menupopup>
134134
</menulist>
135+
<button
136+
id="zotero-prefpane-__addonRef__-best-speed-button"
137+
data-l10n-id="label-best-speed"
138+
></button>
135139
<image
136140
class="help-icon"
137141
src="chrome://jasminum/content/icons/help.svg"

addon/locale/en-US/addon.ftl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ tabpanel-reader-tab-label = Reader Tab
88
select-download-folder = Select download folder
99
get-Chinese-styles = Get Chinese community styles…
1010
info-translators-cn-updaing = Chinese translators are under updating.
11+
info-best-speed-source-updated = Updated to fastest source: { $source }
12+
info-best-speed-source-failed = Failed to select fastest source, please check network connection
1113
1214
1315
# Preference translator table

addon/locale/en-US/preferences-main.ftl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ namepattern-desc =
3939
# Transator Settings
4040
pref-group-translators = Chinese Translator Settings
4141
label-translator-source = Translator Download Source
42+
label-best-speed = Choose Fastest Source
4243
translatorSource-desc =
43-
.tooltiptext = Select the translator download source. Generally, there is no need to switch. If you cannot download the Chinese translator, you can try other sources.
44+
.tooltiptext = Select the translator download source. Generally, there is no need to switch. If you cannot download the Chinese translator, you can try other sources or click the "Choose Fastest Source" button.
4445
label-auto-update-translators =
4546
.label = Automatically Update Translators
4647
label-translators-force-update =

addon/locale/zh-CN/addon.ftl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ tabpanel-reader-tab-label = 阅读器标签
99
select-download-folder = 选择下载文件保存目录
1010
get-Chinese-styles = 获取中文社区样式…
1111
info-translators-cn-updaing = 中文转换器正在更新中
12+
info-best-speed-source-updated = 已更新为最快源:{ $source }
13+
info-best-speed-source-failed = 选择最快源失败,请检查网络连接
1214
1315
# Preference translator table
1416
th-filename = 文件名

addon/locale/zh-CN/preferences-main.ftl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,9 @@ action-after-import-desc =
6060
# 转换器设置
6161
pref-group-translators = 中文转换器设置
6262
label-translator-source = 转换器下载源
63+
label-best-speed = 选择最快源
6364
translatorSource-desc =
64-
.tooltiptext = 选择转换器下载源,一般情况下不用切换。如果您无法下载中文转换器,可选择尝试其他源
65+
.tooltiptext = 选择转换器下载源,一般情况下不用切换。如果您无法下载中文转换器,可选择尝试其他源或点击 选择最快源 按钮
6566
label-auto-update-translators =
6667
.label = 自动更新转换器
6768
label-translators-force-update =

addon/locale/zh-TW/addon.ftl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ tabpanel-reader-tab-label = 閱讀器標籤
99
select-download-folder = 選擇下載檔案儲存目錄
1010
get-Chinese-styles = 取得中文社群樣式…
1111
info-translators-cn-updaing = 中文轉換器正在更新中
12+
info-best-speed-source-updated = 已更新為最快源:{ $source }
13+
info-best-speed-source-failed = 選擇最快源失敗,請檢查網路連接
1214
1315
# Preference translator table
1416
th-filename = 檔案名稱

addon/locale/zh-TW/preferences-main.ftl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ namepattern-desc =
3939
# 轉換器設定
4040
pref-group-translators = 中文轉換器設定
4141
label-translator-source = 轉換器下載源
42+
label-best-speed = 選擇最快源
4243
translatorSource-desc =
43-
.tooltiptext = 選擇轉換器下載源,一般情況下不用切換。如果您無法下載中文轉換器,可選擇嘗試其他源
44+
.tooltiptext = 選擇轉換器下載源,一般情況下不用切換。如果您無法下載中文轉換器,可選擇嘗試其他源或點擊「選擇最快源」按鈕
4445
label-auto-update-translators =
4546
.label = 自動更新轉換器
4647
label-translators-force-update =

src/modules/preferences/main.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { config } from "../../../package.json";
22
import { isMainlandChina } from "../../utils/http";
33
import { getString } from "../../utils/locale";
44
import { getPref, setPref } from "../../utils/prefs";
5-
import { updateTranslators, randomBaseUrl } from ".././translators";
5+
import { updateTranslators, bestSpeedBaseUrl } from ".././translators";
66
import type { PluginPrefsMap } from "../../utils/prefs";
77
import { onShowTable } from "./translators";
88

@@ -62,7 +62,7 @@ export async function initPrefs() {
6262
}
6363

6464
if (!getPref("translatorSource")) {
65-
setPref("translatorSource", randomBaseUrl());
65+
setPref("translatorSource", await bestSpeedBaseUrl());
6666
}
6767

6868
const translatortUpdateTime = getPref("translatorUpdateTime");
@@ -186,6 +186,29 @@ function bindPrefEvents(doc: Document) {
186186
onShowTable();
187187
});
188188

189+
doc
190+
.getElementById(`zotero-prefpane-${config.addonRef}-best-speed-button`)
191+
?.addEventListener("click", async (event) => {
192+
const button = event.target as HTMLButtonElement;
193+
button.disabled = true;
194+
try {
195+
const bestUrl = await bestSpeedBaseUrl();
196+
setPref("translatorSource", bestUrl);
197+
addon.data.prefs?.window.alert(
198+
getString("info-best-speed-source-updated", {
199+
args: { source: bestUrl },
200+
}),
201+
);
202+
} catch (error) {
203+
ztoolkit.log(`select best speed source failed: ${error}`);
204+
addon.data.prefs?.window.alert(
205+
getString("info-best-speed-source-failed"),
206+
);
207+
} finally {
208+
button.disabled = false;
209+
}
210+
});
211+
189212
// metadata source dropdown
190213
// doc
191214
// .querySelector(`#zotero-prefpane-${config.addonRef}-metadata-source-button`)

src/modules/translators.ts

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,37 @@
11
import { getString } from "../utils/locale";
22
import { getPref, setPref } from "../utils/prefs";
33

4-
export function randomBaseUrl() {
4+
export async function bestSpeedBaseUrl() {
55
const baseUrls = [
66
"https://ftp.linxingzhong.top/translators_CN",
77
"https://oss.wwang.de/translators_CN",
88
"https://www.wieke.cn/translators_CN",
99
];
10-
const baseUrl = baseUrls[Math.floor(Math.random() * baseUrls.length)];
11-
ztoolkit.log(`use base url: ${baseUrl}`);
12-
return baseUrl;
10+
11+
const testUrl = async (
12+
url: string,
13+
): Promise<{ url: string; time: number }> => {
14+
const startTime = Date.now();
15+
try {
16+
await Zotero.HTTP.request("HEAD", `${url}/data/translators.json`, {
17+
timeout: 5000,
18+
});
19+
const time = Date.now() - startTime;
20+
ztoolkit.log(`${url} response time: ${time}ms`);
21+
return { url, time };
22+
} catch (error) {
23+
ztoolkit.log(`${url} request failed: ${error}`);
24+
return { url, time: Infinity };
25+
}
26+
};
27+
28+
const results = await Promise.all(baseUrls.map(testUrl));
29+
const fastest = results.reduce((prev, curr) =>
30+
curr.time < prev.time ? curr : prev,
31+
);
32+
33+
ztoolkit.log(`use fastest base url: ${fastest.url} (${fastest.time}ms)`);
34+
return fastest.url;
1335
}
1436

1537
/**

typings/i10n.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ export type FluentMessageId =
2424
| 'how-to-update-translators'
2525
| 'import-attachments-success'
2626
| 'importing-attachments-is-running'
27+
| 'info-best-speed-source-failed'
28+
| 'info-best-speed-source-updated'
2729
| 'info-translators-cn-updaing'
2830
| 'item-section-example1-head-text'
2931
| 'item-section-example1-sidenav-tooltip'
@@ -33,6 +35,7 @@ export type FluentMessageId =
3335
| 'label-auto-split-name'
3436
| 'label-auto-update-translators'
3537
| 'label-autoupdate-metadata'
38+
| 'label-best-speed'
3639
| 'label-choose-folder'
3740
| 'label-choose-namepattern'
3841
| 'label-choose-source'

0 commit comments

Comments
 (0)