Skip to content

Commit b5c8678

Browse files
committed
dynamic server address
1 parent 511a3c8 commit b5c8678

File tree

11 files changed

+485
-4
lines changed

11 files changed

+485
-4
lines changed

public/_locales/en/messages.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1451,5 +1451,26 @@
14511451
},
14521452
"cacheManagementTitle": {
14531453
"message": "缓存管理"
1454+
},
1455+
"enableDynamicServerAddress": {
1456+
"message": "启用动态服务器地址"
1457+
},
1458+
"dynamicServerAddressDescription": {
1459+
"message": "自动从远程接口获取服务器地址。地址将被缓存以减少请求次数。"
1460+
},
1461+
"apiUrl": {
1462+
"message": "API 地址"
1463+
},
1464+
"refreshing": {
1465+
"message": "刷新中..."
1466+
},
1467+
"refreshDynamicServerAddress": {
1468+
"message": "刷新服务器地址"
1469+
},
1470+
"refreshServerAddressSuccess": {
1471+
"message": "服务器地址刷新成功!"
1472+
},
1473+
"refreshServerAddressFailed": {
1474+
"message": "服务器地址刷新失败!"
14541475
}
14551476
}

public/_locales/zh_CN/messages.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1451,5 +1451,26 @@
14511451
},
14521452
"cacheManagementTitle": {
14531453
"message": "缓存管理"
1454+
},
1455+
"enableDynamicServerAddress": {
1456+
"message": "启用动态服务器地址"
1457+
},
1458+
"dynamicServerAddressDescription": {
1459+
"message": "自动从远程接口获取服务器地址。地址将被缓存以减少请求次数。"
1460+
},
1461+
"apiUrl": {
1462+
"message": "API 地址"
1463+
},
1464+
"refreshing": {
1465+
"message": "刷新中..."
1466+
},
1467+
"refreshDynamicServerAddress": {
1468+
"message": "刷新服务器地址"
1469+
},
1470+
"refreshServerAddressSuccess": {
1471+
"message": "服务器地址刷新成功!"
1472+
},
1473+
"refreshServerAddressFailed": {
1474+
"message": "服务器地址刷新失败!"
14541475
}
14551476
}

public/_locales/zh_TW/messages.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1450,5 +1450,26 @@
14501450
},
14511451
"cacheManagementTitle": {
14521452
"message": "快取管理"
1453+
},
1454+
"enableDynamicServerAddress": {
1455+
"message": "啟用動態伺服器地址"
1456+
},
1457+
"dynamicServerAddressDescription": {
1458+
"message": "自動從遠端介面取得伺服器地址。地址將被快取以減少請求次數。"
1459+
},
1460+
"apiUrl": {
1461+
"message": "API 地址"
1462+
},
1463+
"refreshing": {
1464+
"message": "重整中..."
1465+
},
1466+
"refreshDynamicServerAddress": {
1467+
"message": "重整伺服器地址"
1468+
},
1469+
"refreshServerAddressSuccess": {
1470+
"message": "伺服器地址重整成功!"
1471+
},
1472+
"refreshServerAddressFailed": {
1473+
"message": "伺服器地址重整失敗!"
14531474
}
14541475
}

public/options/options.html

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,7 @@ <h2>__MSG_exportOtherData__</h2>
731731
<label class="optionLabel inline">
732732
<span class="optionLabel">__MSG_customServerAddress__:</span>
733733

734-
<input class="option-text-box" type="text" style="margin-right: 10px" />
734+
<input id="serverAddressInput" class="option-text-box" type="text" style="margin-right: 10px" />
735735
</label>
736736

737737
<div class="small-description">__MSG_customServerAddressDescription__</div>
@@ -743,6 +743,40 @@ <h2>__MSG_exportOtherData__</h2>
743743
</div>
744744
</div>
745745

746+
<div data-dependent-on="testingServer" data-dependent-on-inverted="true">
747+
<div data-type="toggle" data-sync="useDynamicServerAddress">
748+
<div class="switch-container">
749+
<label class="switch">
750+
<input id="useDynamicServerAddress" type="checkbox" />
751+
<span class="slider round"></span>
752+
</label>
753+
<label class="switch-label" for="useDynamicServerAddress">
754+
__MSG_enableDynamicServerAddress__
755+
</label>
756+
</div>
757+
758+
<div class="small-description">__MSG_dynamicServerAddressDescription__</div>
759+
760+
<div data-dependent-on="useDynamicServerAddress">
761+
<br />
762+
<div data-type="text-change" data-sync="dynamicServerAddressUrl">
763+
<label class="optionLabel inline">
764+
<span class="optionLabel">__MSG_apiUrl__:</span>
765+
<input class="option-text-box" type="text" style="margin-right: 10px" />
766+
</label>
767+
768+
<div class="next-line" style=" display: flex;gap: 10px;">
769+
<div class="option-button text-change-set inline">__MSG_save__</div>
770+
<div class="option-button text-change-reset inline">__MSG_reset__</div>
771+
<div data-type="react-DynamicServerAddressComponent"></div>
772+
773+
</div>
774+
</div>
775+
776+
</div>
777+
</div>
778+
</div>
779+
746780
</div>
747781

748782
<div id="experiment" class="option-group hidden">

src/background.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { chromeP } from "./utils/browserApi";
1010
import { getHash } from "./utils/hash";
1111
import { generateUserID } from "./utils/setup";
1212
import { setupTabUpdates } from "./utils/tab-updates";
13+
import { updateServerAddressCache, forceRefreshServerAddress, clearServerAddressCache } from "./utils/dynamicServerAddress";
1314

1415
const popupPort: Record<string, chrome.runtime.Port> = {};
1516

@@ -19,6 +20,9 @@ const contentScriptRegistrations = {};
1920
setupBackgroundRequestProxy();
2021
setupTabUpdates(Config);
2122

23+
// 初始化动态服务器地址
24+
void initDynamicServerAddress();
25+
2226
chrome.runtime.onMessage.addListener(function (request, sender, callback) {
2327
switch (request.message) {
2428
case "openConfig":
@@ -103,6 +107,9 @@ chrome.runtime.onInstalled.addListener(function () {
103107
// Don't show update notification
104108
Config.config.categoryPillUpdate = true;
105109
}
110+
111+
// 在安装或更新时更新动态服务器地址
112+
void initDynamicServerAddress();
106113
}, 1500);
107114
});
108115

@@ -236,10 +243,55 @@ function setupBackgroundRequestProxy() {
236243
return true;
237244
}
238245

246+
// ============ Dynamic Server Address Handlers ============
247+
if (request.message === "updateServerAddress") {
248+
updateServerAddressCache()
249+
.then(() => callback({ ok: true }))
250+
.catch(() => callback({ ok: false }));
251+
return true;
252+
}
253+
254+
if (request.message === "forceRefreshServerAddress") {
255+
forceRefreshServerAddress()
256+
.then((success) => callback({ ok: success }))
257+
.catch(() => callback({ ok: false }));
258+
return true;
259+
}
260+
261+
if (request.message === "clearServerAddressCache") {
262+
try {
263+
clearServerAddressCache();
264+
callback({ ok: true });
265+
} catch {
266+
callback({ ok: false });
267+
}
268+
return true;
269+
}
270+
239271
return false;
240272
});
241273
}
242274

275+
/**
276+
* 初始化动态服务器地址
277+
* 在扩展启动时调用,检查并更新服务器地址缓存
278+
*/
279+
async function initDynamicServerAddress(): Promise<void> {
280+
// 等待配置加载完成
281+
while (!Config.isReady()) {
282+
await new Promise((resolve) => setTimeout(resolve, 100));
283+
}
284+
285+
// 启动时更新服务器地址
286+
await updateServerAddressCache();
287+
288+
// 设置定期更新(每隔TTL的一半检查一次)
289+
const checkInterval = Math.max(Config.config.dynamicServerAddressTTL / 2, 60000); // 至少1分钟
290+
setInterval(() => {
291+
void updateServerAddressCache();
292+
}, checkInterval);
293+
}
294+
243295
/**
244296
* Get cache statistics for segments and video labels caches
245297
*/
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import * as React from "react";
2+
3+
export interface DynamicServerAddressProps {}
4+
5+
export interface DynamicServerAddressState {
6+
isRefreshing: boolean;
7+
}
8+
9+
class DynamicServerAddressComponent extends React.Component<
10+
DynamicServerAddressProps,
11+
DynamicServerAddressState
12+
> {
13+
constructor(props: DynamicServerAddressProps) {
14+
super(props);
15+
16+
this.state = {
17+
isRefreshing: false,
18+
};
19+
}
20+
21+
async handleRefresh(): Promise<void> {
22+
this.setState({ isRefreshing: true });
23+
24+
try {
25+
const response = await new Promise<{ ok: boolean }>((resolve) => {
26+
chrome.runtime.sendMessage({ message: "forceRefreshServerAddress" }, resolve);
27+
});
28+
29+
if (response.ok) {
30+
alert(
31+
chrome.i18n.getMessage("refreshServerAddressSuccess") || "Server address refreshed successfully!"
32+
);
33+
} else {
34+
alert(chrome.i18n.getMessage("refreshServerAddressFailed") || "Failed to refresh server address!");
35+
}
36+
} catch (error) {
37+
alert(chrome.i18n.getMessage("refreshServerAddressFailed") || "Failed to refresh server address!");
38+
} finally {
39+
this.setState({ isRefreshing: false });
40+
}
41+
}
42+
43+
render(): React.ReactElement {
44+
return (
45+
<div>
46+
<div
47+
className={`option-button inline ${this.state.isRefreshing ? "disabled" : ""}`}
48+
onClick={() => !this.state.isRefreshing && this.handleRefresh()}
49+
>
50+
{this.state.isRefreshing
51+
? chrome.i18n.getMessage("refreshing") || "Refreshing..."
52+
: chrome.i18n.getMessage("refreshDynamicServerAddress") || "Refresh Server Address"}
53+
</div>
54+
</div>
55+
);
56+
}
57+
}
58+
59+
export default DynamicServerAddressComponent;
60+

src/config.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ interface SBConfig {
105105

106106
showNewIcon: boolean;
107107

108+
// 动态服务器地址配置
109+
useDynamicServerAddress: boolean; // 是否启用动态服务器地址
110+
dynamicServerAddressUrl: string; // 获取服务器地址的接口URL
111+
dynamicServerAddressTTL: number; // 缓存时间(毫秒)
112+
108113
// Used to cache calculated text color info
109114
categoryPillColors: {
110115
[key in Category]: {
@@ -180,6 +185,12 @@ interface SBStorage {
180185

181186
/* Contains unsubmitted segments that the user has created. */
182187
unsubmittedSegments: Record<string, SponsorTime[]>;
188+
189+
// 动态服务器地址缓存
190+
dynamicServerAddressCache?: {
191+
address: string;
192+
fetchedAt: number; // timestamp
193+
};
183194
}
184195

185196
class ConfigClass extends ProtoConfig<SBConfig, SBStorage> {
@@ -373,6 +384,11 @@ const syncDefaults = {
373384

374385
showNewIcon: true,
375386

387+
// 动态服务器地址配置
388+
useDynamicServerAddress: true,
389+
dynamicServerAddressUrl: "https://bsbsb.top/api/serverAddress", // 默认接口地址
390+
dynamicServerAddressTTL: 5 * 3600 * 1000, // 5小时
391+
376392
categoryPillColors: {},
377393

378394
/**

0 commit comments

Comments
 (0)