@@ -21,6 +21,7 @@ import {
2121 getMetadataStr ,
2222 getUserConfigStr ,
2323 obtainBlackList ,
24+ isFirefox ,
2425} from "@App/pkg/utils/utils" ;
2526import { cacheInstance } from "@App/app/cache" ;
2627import { UrlMatch } from "@App/pkg/utils/match" ;
@@ -53,7 +54,7 @@ const runtimeGlobal = {
5354 messageFlag : "PENDING" ,
5455 scriptLoadComplete : "PENDING" ,
5556 envLoadComplete : "PENDING" ,
56- } as MessageFlags ,
57+ } satisfies MessageFlags & Record < string , string > ,
5758} ;
5859
5960export class RuntimeService {
@@ -305,8 +306,8 @@ export class RuntimeService {
305306
306307 let registered = false ;
307308 try {
308- const res = await chrome . userScripts . getScripts ( { ids : [ "scriptcat-content" , "scriptcat- inject"] } ) ;
309- registered = res . length === 2 ;
309+ const res = await chrome . userScripts . getScripts ( { ids : [ "scriptcat-inject" ] } ) ;
310+ registered = res . length === 1 ;
310311 } finally {
311312 // 考虑 UserScripts API 不可使用等情况
312313 runtimeGlobal . registered = registered ;
@@ -608,6 +609,7 @@ export class RuntimeService {
608609 runtimeGlobal . messageFlags = this . generateMessageFlags ( ) ;
609610 await Promise . allSettled ( [
610611 chrome . userScripts . unregister ( ) ,
612+ chrome . scripting . unregisterContentScripts ( ) ,
611613 this . localStorageDAO . save ( { key : "scriptInjectMessageFlags" , value : runtimeGlobal . messageFlags } ) ,
612614 ] ) ;
613615 }
@@ -781,32 +783,56 @@ export class RuntimeService {
781783 // do nothing
782784 }
783785 }
784- const retScript : chrome . userScripts . RegisteredUserScript [ ] = [ ] ;
785- const contentJs = await this . getContentJsCode ( ) ;
786- if ( contentJs ) {
787- retScript . push ( {
788- id : "scriptcat-content" ,
789- js : [ { code : `(function (MessageFlags) {\n${ contentJs } \n})(${ JSON . stringify ( messageFlags ) } )` } ] ,
790- matches : [ "<all_urls>" ] ,
791- allFrames : true ,
792- runAt : "document_start" ,
793- world : "USER_SCRIPT" ,
794- excludeMatches,
795- excludeGlobs,
796- } ) ;
797- }
798-
786+ let retContent : chrome . scripting . RegisteredContentScript [ ] = [ ] ;
787+ let retInject : chrome . userScripts . RegisteredUserScript [ ] = [ ] ;
799788 // inject.js
800789 const injectJs = await this . getInjectJsCode ( ) ;
801790 if ( injectJs ) {
802- const apiScripts = this . compileInjectUserScript ( injectJs , messageFlags , {
803- excludeMatches,
804- excludeGlobs,
805- } ) ;
806- retScript . push ( ...apiScripts ) ;
791+ // 构建inject.js的脚本注册信息
792+ const code = `(function (MessageFlags) {\n${ injectJs } \n})(${ JSON . stringify ( messageFlags ) } )` ;
793+ retInject = [
794+ {
795+ id : "scriptcat-inject" ,
796+ js : [ { code } ] ,
797+ matches : [ "<all_urls>" ] ,
798+ allFrames : true ,
799+ world : "MAIN" ,
800+ runAt : "document_start" ,
801+ excludeMatches : excludeMatches ,
802+ excludeGlobs : excludeGlobs ,
803+ } satisfies chrome . userScripts . RegisteredUserScript ,
804+ ] ;
805+ }
806+ // Note: Chrome does not support file.js?query
807+ // 注意:Chrome 不支持 file.js?query
808+ if ( isFirefox ( ) ) {
809+ retContent = [
810+ {
811+ id : "scriptcat-content" ,
812+ js : [ `/src/content.js?FlagsStart&${ `${ new URLSearchParams ( messageFlags ) } ` } &FlagsEnd` ] ,
813+ matches : [ "<all_urls>" ] ,
814+ allFrames : true ,
815+ runAt : "document_start" ,
816+ excludeMatches,
817+ } satisfies chrome . scripting . RegisteredContentScript ,
818+ ] ;
819+ } else {
820+ const contentJs = await this . getContentJsCode ( ) ;
821+ if ( contentJs ) {
822+ retInject . push ( {
823+ id : "scriptcat-content" ,
824+ js : [ { code : `(function () {\n${ contentJs } \n})(${ JSON . stringify ( messageFlags ) } )` } ] ,
825+ matches : [ "<all_urls>" ] ,
826+ allFrames : true ,
827+ runAt : "document_start" ,
828+ world : "USER_SCRIPT" ,
829+ excludeMatches,
830+ excludeGlobs,
831+ } satisfies chrome . userScripts . RegisteredUserScript ) ;
832+ }
807833 }
808834
809- return retScript ;
835+ return { content : retContent , inject : retInject } ;
810836 }
811837
812838 // 如果是重复注册,需要先调用 unregisterUserscripts
@@ -818,8 +844,8 @@ export class RuntimeService {
818844 if ( runtimeGlobal . registered ) {
819845 // 异常情况
820846 // 检查scriptcat-content和scriptcat-inject是否存在
821- const res = await chrome . userScripts . getScripts ( { ids : [ "scriptcat-content" , "scriptcat- inject"] } ) ;
822- if ( res . length === 2 ) {
847+ const res = await chrome . userScripts . getScripts ( { ids : [ "scriptcat-inject" ] } ) ;
848+ if ( res . length === 1 ) {
823849 return ;
824850 }
825851 // scriptcat-content/scriptcat-inject不存在的情况
@@ -843,9 +869,9 @@ export class RuntimeService {
843869 const particularScriptList = await this . getParticularScriptList ( options ) ;
844870 // getContentAndInjectScript依赖loadScriptMatchInfo
845871 // 需要等getParticularScriptList完成后再执行
846- const generalScriptList = await this . getContentAndInjectScript ( options ) ;
872+ const { inject : injectScripList , content : contentScriptList } = await this . getContentAndInjectScript ( options ) ;
847873
848- const list : chrome . userScripts . RegisteredUserScript [ ] = [ ...particularScriptList , ...generalScriptList ] ;
874+ const list : chrome . userScripts . RegisteredUserScript [ ] = [ ...particularScriptList , ...injectScripList ] ;
849875
850876 runtimeGlobal . registered = true ;
851877 try {
@@ -870,6 +896,11 @@ export class RuntimeService {
870896 }
871897 }
872898 }
899+ try {
900+ await chrome . scripting . registerContentScripts ( contentScriptList ) ;
901+ } catch ( e : any ) {
902+ this . logger . error ( "register content.js error" , Logger . E ( e ) ) ;
903+ }
873904 }
874905
875906 // 给指定tab发送消息
@@ -1200,27 +1231,6 @@ export class RuntimeService {
12001231 return await runScript ( this . msgSender , res ) ;
12011232 }
12021233
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-
12241234 scriptMatchEntry (
12251235 scriptRes : ScriptRunResource ,
12261236 o : {
0 commit comments