@@ -53,7 +53,7 @@ const runtimeGlobal = {
5353 messageFlag : "PENDING" ,
5454 scriptLoadComplete : "PENDING" ,
5555 envLoadComplete : "PENDING" ,
56- } as MessageFlags ,
56+ } satisfies MessageFlags & Record < string , string > ,
5757} ;
5858
5959export class RuntimeService {
@@ -305,8 +305,8 @@ export class RuntimeService {
305305
306306 let registered = false ;
307307 try {
308- const res = await chrome . userScripts . getScripts ( { ids : [ "scriptcat-content" , "scriptcat- inject"] } ) ;
309- registered = res . length === 2 ;
308+ const res = await chrome . userScripts . getScripts ( { ids : [ "scriptcat-inject" ] } ) ;
309+ registered = res . length === 1 ;
310310 } finally {
311311 // 考虑 UserScripts API 不可使用等情况
312312 runtimeGlobal . registered = registered ;
@@ -608,6 +608,7 @@ export class RuntimeService {
608608 runtimeGlobal . messageFlags = this . generateMessageFlags ( ) ;
609609 await Promise . allSettled ( [
610610 chrome . userScripts . unregister ( ) ,
611+ chrome . scripting . unregisterContentScripts ( ) ,
611612 this . localStorageDAO . save ( { key : "scriptInjectMessageFlags" , value : runtimeGlobal . messageFlags } ) ,
612613 ] ) ;
613614 }
@@ -781,32 +782,39 @@ export class RuntimeService {
781782 // do nothing
782783 }
783784 }
784- const retScript : chrome . userScripts . RegisteredUserScript [ ] = [ ] ;
785- const contentJs = await this . getContentJsCode ( ) ;
786- if ( contentJs ) {
787- retScript . push ( {
785+ let retContent : chrome . scripting . RegisteredContentScript [ ] = [ ] ;
786+ let retInject : chrome . userScripts . RegisteredUserScript [ ] = [ ] ;
787+ retContent = [
788+ {
788789 id : "scriptcat-content" ,
789- js : [ { code : `(function (MessageFlags) {\n ${ contentJs } \n})( ${ JSON . stringify ( messageFlags ) } )` } ] ,
790+ js : [ `/src/content.js?FlagsStart& ${ ` ${ new URLSearchParams ( messageFlags ) } ` } &FlagsEnd` ] ,
790791 matches : [ "<all_urls>" ] ,
791792 allFrames : true ,
792793 runAt : "document_start" ,
793- world : "USER_SCRIPT" ,
794794 excludeMatches,
795- excludeGlobs,
796- } ) ;
797- }
795+ } satisfies chrome . scripting . RegisteredContentScript ,
796+ ] ;
798797
799798 // inject.js
800799 const injectJs = await this . getInjectJsCode ( ) ;
801800 if ( injectJs ) {
802- const apiScripts = this . compileInjectUserScript ( injectJs , messageFlags , {
803- excludeMatches,
804- excludeGlobs,
805- } ) ;
806- retScript . push ( ...apiScripts ) ;
801+ // 构建inject.js的脚本注册信息
802+ const code = `(function (MessageFlags) {\n${ injectJs } \n})(${ JSON . stringify ( messageFlags ) } )` ;
803+ retInject = [
804+ {
805+ id : "scriptcat-inject" ,
806+ js : [ { code } ] ,
807+ matches : [ "<all_urls>" ] ,
808+ allFrames : true ,
809+ world : "MAIN" ,
810+ runAt : "document_start" ,
811+ excludeMatches : excludeMatches ,
812+ excludeGlobs : excludeGlobs ,
813+ } satisfies chrome . userScripts . RegisteredUserScript ,
814+ ] ;
807815 }
808816
809- return retScript ;
817+ return { content : retContent , inject : retInject } ;
810818 }
811819
812820 // 如果是重复注册,需要先调用 unregisterUserscripts
@@ -818,8 +826,8 @@ export class RuntimeService {
818826 if ( runtimeGlobal . registered ) {
819827 // 异常情况
820828 // 检查scriptcat-content和scriptcat-inject是否存在
821- const res = await chrome . userScripts . getScripts ( { ids : [ "scriptcat-content" , "scriptcat- inject"] } ) ;
822- if ( res . length === 2 ) {
829+ const res = await chrome . userScripts . getScripts ( { ids : [ "scriptcat-inject" ] } ) ;
830+ if ( res . length === 1 ) {
823831 return ;
824832 }
825833 // scriptcat-content/scriptcat-inject不存在的情况
@@ -843,9 +851,9 @@ export class RuntimeService {
843851 const particularScriptList = await this . getParticularScriptList ( options ) ;
844852 // getContentAndInjectScript依赖loadScriptMatchInfo
845853 // 需要等getParticularScriptList完成后再执行
846- const generalScriptList = await this . getContentAndInjectScript ( options ) ;
854+ const { inject : injectScripList , content : contentScriptList } = await this . getContentAndInjectScript ( options ) ;
847855
848- const list : chrome . userScripts . RegisteredUserScript [ ] = [ ...particularScriptList , ...generalScriptList ] ;
856+ const list : chrome . userScripts . RegisteredUserScript [ ] = [ ...particularScriptList , ...injectScripList ] ;
849857
850858 runtimeGlobal . registered = true ;
851859 try {
@@ -870,6 +878,11 @@ export class RuntimeService {
870878 }
871879 }
872880 }
881+ try {
882+ await chrome . scripting . registerContentScripts ( contentScriptList ) ;
883+ } catch ( e : any ) {
884+ this . logger . error ( "register content.js error" , Logger . E ( e ) ) ;
885+ }
873886 }
874887
875888 // 给指定tab发送消息
@@ -1200,27 +1213,6 @@ export class RuntimeService {
12001213 return await runScript ( this . msgSender , res ) ;
12011214 }
12021215
1203- compileInjectUserScript (
1204- injectJs : string ,
1205- messageFlags : MessageFlags ,
1206- { excludeMatches, excludeGlobs } : { excludeMatches : string [ ] | undefined ; excludeGlobs : string [ ] | undefined }
1207- ) {
1208- // 构建inject.js的脚本注册信息
1209- const code = `(function (MessageFlags) {\n${ injectJs } \n})(${ JSON . stringify ( messageFlags ) } )` ;
1210- const script : chrome . userScripts . RegisteredUserScript = {
1211- id : "scriptcat-inject" ,
1212- js : [ { code } ] ,
1213- matches : [ "<all_urls>" ] ,
1214- allFrames : true ,
1215- world : "MAIN" ,
1216- runAt : "document_start" ,
1217- excludeMatches : excludeMatches ,
1218- excludeGlobs : excludeGlobs ,
1219- } ;
1220-
1221- return [ script ] as chrome . userScripts . RegisteredUserScript [ ] ;
1222- }
1223-
12241216 scriptMatchEntry (
12251217 scriptRes : ScriptRunResource ,
12261218 o : {
0 commit comments