@@ -53,9 +53,20 @@ import { scriptToMenu, type TPopupPageLoadInfo } from "./popup_scriptmenu";
5353
5454const ORIGINAL_URLMATCH_SUFFIX = "{ORIGINAL}" ; // 用于标记原始URLPatterns的后缀
5555
56+ const RuntimeRegisterCode = {
57+ UNSET : 0 ,
58+ REGISTER_DONE : 1 ,
59+ UNREGISTER_DONE : 2 ,
60+ } as const ;
61+
62+ type RuntimeRegisterCode = ValueOf < typeof RuntimeRegisterCode > ;
63+
5664const runtimeGlobal = {
57- registered : false ,
65+ registerState : RuntimeRegisterCode . UNSET ,
5866 messageFlag : "PENDING" ,
67+ } as {
68+ registerState : RuntimeRegisterCode ;
69+ messageFlag : string ;
5970} ;
6071
6172export type TTabInfo = {
@@ -314,15 +325,15 @@ export class RuntimeService {
314325 await cacheInstance . set < boolean > ( "runtimeStartFlag" , true ) ;
315326 }
316327
317- let registered = false ;
328+ let count = 0 ;
318329 try {
319330 const res = await chrome . userScripts ?. getScripts ( { ids : [ "scriptcat-inject" ] } ) ;
320- registered = res ?. length === 1 ;
331+ count = res ?. length ;
321332 } catch {
322333 // 该错误为预期内情况,无需记录 debug 日志
323334 } finally {
324335 // 考虑 UserScripts API 不可使用等情况
325- runtimeGlobal . registered = registered ;
336+ runtimeGlobal . registerState = count === 1 ? RuntimeRegisterCode . REGISTER_DONE : RuntimeRegisterCode . UNSET ;
326337 }
327338 }
328339
@@ -669,8 +680,9 @@ export class RuntimeService {
669680 // 取消脚本注册
670681 async unregisterUserscripts ( ) {
671682 // 检查 registered 避免重复操作增加系统开支
672- if ( runtimeGlobal . registered ) {
673- runtimeGlobal . registered = false ;
683+ // 已成功注册(true)或是未知有无注册(null)的情况下执行
684+ if ( runtimeGlobal . registerState !== RuntimeRegisterCode . UNREGISTER_DONE ) {
685+ runtimeGlobal . registerState = RuntimeRegisterCode . UNREGISTER_DONE ;
674686 // 重置 flag 避免取消注册失败
675687 // 即使注册失败,通过重置 flag 可避免错误地呼叫已取消注册的Script
676688 await Promise . allSettled ( [ chrome . userScripts ?. unregister ( ) , chrome . scripting . unregisterContentScripts ( ) ] ) ;
@@ -893,7 +905,7 @@ export class RuntimeService {
893905 if ( ! this . isUserScriptsAvailable || ! this . isLoadScripts ) return ;
894906
895907 // 判断是否已经注册过
896- if ( runtimeGlobal . registered ) {
908+ if ( runtimeGlobal . registerState === RuntimeRegisterCode . REGISTER_DONE ) {
897909 // 异常情况
898910 // 检查scriptcat-content和scriptcat-inject是否存在
899911 const res = await chrome . userScripts . getScripts ( { ids : [ "scriptcat-inject" ] } ) ;
@@ -903,7 +915,7 @@ export class RuntimeService {
903915 // scriptcat-content/scriptcat-inject不存在的情况
904916 // 走一次重新注册的流程
905917 this . logger . warn ( "registered = true but scriptcat-content/scriptcat-inject not exists, re-register userscripts." ) ;
906- runtimeGlobal . registered = false ; // 异常时强制反注册
918+ runtimeGlobal . registerState = RuntimeRegisterCode . UNSET ; // 异常时强制反注册
907919 }
908920 // 删除旧注册
909921 await this . unregisterUserscripts ( ) ;
@@ -926,7 +938,7 @@ export class RuntimeService {
926938
927939 const list : chrome . userScripts . RegisteredUserScript [ ] = [ ...particularScriptList , ...injectScriptList ] ;
928940
929- runtimeGlobal . registered = true ;
941+ let failed = false ;
930942 try {
931943 await chrome . userScripts . register ( list ) ;
932944 } catch ( e : any ) {
@@ -941,6 +953,7 @@ export class RuntimeService {
941953 try {
942954 await chrome . userScripts . update ( [ script ] ) ;
943955 } catch ( e ) {
956+ failed = true ;
944957 this . logger . error ( "update error" , Logger . E ( e ) ) ;
945958 }
946959 } else {
@@ -953,9 +966,11 @@ export class RuntimeService {
953966 try {
954967 await chrome . scripting . registerContentScripts ( contentScriptList ) ;
955968 } catch ( e : any ) {
969+ failed = true ;
956970 this . logger . error ( "register content.js error" , Logger . E ( e ) ) ;
957971 }
958972 }
973+ runtimeGlobal . registerState = failed ? RuntimeRegisterCode . UNSET : RuntimeRegisterCode . REGISTER_DONE ;
959974 }
960975
961976 // 给指定tab发送消息
0 commit comments