@@ -203,12 +203,27 @@ const updateHeaderRulesInternal = async (language, retryCount = 0, isAutoSwitch
203203 language = language ? language . trim ( ) : DEFAULT_LANG_EN ;
204204
205205 // 优化的缓存检查:对所有调用(包括自动切换)都进行短路检查
206- if ( language === lastAppliedLanguage && rulesCache ) {
207- const logMessage = isAutoSwitch
208- ? backgroundI18n . t ( 'auto_switch_skip_duplicate' , { language } )
209- : backgroundI18n . t ( 'language_already_set' , { language } ) ;
210- sendBackgroundLog ( logMessage , 'info' ) ;
211- return { status : 'cached' , language } ;
206+ if ( language === lastAppliedLanguage && rulesCache && rulesCache . length > 0 ) {
207+ // 验证缓存中确实存在对应的规则
208+ const existingRule = rulesCache . find ( rule =>
209+ rule . id === RULE_ID &&
210+ rule . action . requestHeaders &&
211+ rule . action . requestHeaders . some ( header =>
212+ header . header === 'Accept-Language' &&
213+ header . value === language
214+ )
215+ ) ;
216+
217+ if ( existingRule ) {
218+ const logMessage = isAutoSwitch
219+ ? backgroundI18n . t ( 'auto_switch_skip_duplicate' , { language } )
220+ : backgroundI18n . t ( 'language_already_set' , { language } ) ;
221+ sendBackgroundLog ( logMessage , 'info' ) ;
222+ return { status : 'cached' , language } ;
223+ } else {
224+ // 语言相同但缓存中没有对应规则,记录警告并继续处理
225+ sendBackgroundLog ( backgroundI18n . t ( 'cache_empty_but_language_same' , { language } ) , 'warning' ) ;
226+ }
212227 }
213228
214229 sendBackgroundLog ( `${ backgroundI18n . t ( 'trying_update_rules' , { language } ) } ${ retryCount > 0 ? ` (${ backgroundI18n . t ( 'retry' ) } #${ retryCount } )` : '' } ` , 'info' ) ;
@@ -361,15 +376,7 @@ const performInitialization = async (reason) => {
361376 sendBackgroundLog ( backgroundI18n . t ( 'domain_rules_loaded' ) , 'info' ) ;
362377
363378 // 2. 从存储中获取设置
364- const result = await new Promise ( ( resolve , reject ) => {
365- chrome . storage . local . get ( [ 'currentLanguage' , 'autoSwitchEnabled' ] , ( result ) => {
366- if ( chrome . runtime . lastError ) {
367- reject ( new Error ( chrome . runtime . lastError . message ) ) ;
368- return ;
369- }
370- resolve ( result ) ;
371- } ) ;
372- } ) ;
379+ const result = await chrome . storage . local . get ( [ 'currentLanguage' , 'autoSwitchEnabled' ] ) ;
373380 autoSwitchEnabled = result . autoSwitchEnabled !== false ; // 默认为 true
374381 sendBackgroundLog ( `${ backgroundI18n . t ( 'loaded_auto_switch_status' ) } : ${ autoSwitchEnabled } ` , 'info' ) ;
375382
@@ -573,15 +580,7 @@ const handleGetCurrentLangRequest = async (sendResponse) => {
573580 const currentRule = rules . find ( rule => rule . id === RULE_ID ) ;
574581 const actualCurrentLang = currentRule ?. action ?. requestHeaders ?. find ( h => h . header === 'Accept-Language' ) ?. value ;
575582
576- const result = await new Promise ( ( resolve , reject ) => {
577- chrome . storage . local . get ( [ 'currentLanguage' , 'autoSwitchEnabled' ] , ( result ) => {
578- if ( chrome . runtime . lastError ) {
579- reject ( new Error ( chrome . runtime . lastError . message ) ) ;
580- return ;
581- }
582- resolve ( result ) ;
583- } ) ;
584- } ) ;
583+ const result = await chrome . storage . local . get ( [ 'currentLanguage' , 'autoSwitchEnabled' ] ) ;
585584 if ( typeof sendResponse === 'function' ) {
586585 sendResponse ( {
587586 currentLanguage : actualCurrentLang || result . currentLanguage || lastAppliedLanguage ,
@@ -1107,16 +1106,8 @@ const handleGetDynamicRulesRequest = async (sendResponse) => {
11071106 */
11081107const handleGetMatchedRulesRequest = async ( sendResponse ) => {
11091108 try {
1110- const matchedRules = await new Promise ( ( resolve , reject ) => {
1111- chrome . declarativeNetRequest . getMatchedRules ( { } , ( result ) => {
1112- if ( chrome . runtime . lastError ) {
1113- reject ( new Error ( chrome . runtime . lastError . message ) ) ;
1114- return ;
1115- }
1116- resolve ( result ) ;
1117- } ) ;
1118- } ) ;
1119-
1109+ const matchedRules = await chrome . declarativeNetRequest . getMatchedRules ( { } ) ;
1110+
11201111 if ( typeof sendResponse === 'function' ) {
11211112 sendResponse ( {
11221113 success : true ,
0 commit comments