@@ -10,6 +10,7 @@ import { chromeP } from "./utils/browserApi";
1010import { getHash } from "./utils/hash" ;
1111import { generateUserID } from "./utils/setup" ;
1212import { setupTabUpdates } from "./utils/tab-updates" ;
13+ import { updateServerAddressCache , forceRefreshServerAddress , clearServerAddressCache } from "./utils/dynamicServerAddress" ;
1314
1415const popupPort : Record < string , chrome . runtime . Port > = { } ;
1516
@@ -19,6 +20,9 @@ const contentScriptRegistrations = {};
1920setupBackgroundRequestProxy ( ) ;
2021setupTabUpdates ( Config ) ;
2122
23+ // 初始化动态服务器地址
24+ void initDynamicServerAddress ( ) ;
25+
2226chrome . 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 */
0 commit comments