@@ -22,6 +22,7 @@ import {
2222 getMetadataStr ,
2323 getUserConfigStr ,
2424 obtainBlackList ,
25+ isFirefox ,
2526 sourceMapTo ,
2627} from "@App/pkg/utils/utils" ;
2728import { cacheInstance } from "@App/app/cache" ;
@@ -336,8 +337,8 @@ export class RuntimeService {
336337
337338 let registered = false ;
338339 try {
339- const res = await chrome . userScripts . getScripts ( { ids : [ "scriptcat-content" , "scriptcat- inject"] } ) ;
340- registered = res . length === 2 ;
340+ const res = await chrome . userScripts . getScripts ( { ids : [ "scriptcat-inject" ] } ) ;
341+ registered = res . length === 1 ;
341342 } finally {
342343 // 考虑 UserScripts API 不可使用等情况
343344 runtimeGlobal . registered = registered ;
@@ -663,6 +664,7 @@ export class RuntimeService {
663664 runtimeGlobal . messageFlag = this . generateMessageFlag ( ) ;
664665 await Promise . allSettled ( [
665666 chrome . userScripts . unregister ( ) ,
667+ chrome . scripting . unregisterContentScripts ( ) ,
666668 this . localStorageDAO . save ( { key : "scriptInjectMessageFlag" , value : runtimeGlobal . messageFlag } ) ,
667669 ] ) ;
668670 }
@@ -830,34 +832,51 @@ export class RuntimeService {
830832 // do nothing
831833 }
832834 }
833- const retScript : chrome . userScripts . RegisteredUserScript [ ] = [ ] ;
834- const contentJs = await this . getContentJsCode ( ) ;
835- if ( contentJs ) {
836- const codeBody = `(function (MessageFlag) {\n${ contentJs } \n})('${ messageFlag } ')` ;
837- const code = `${ codeBody } ${ sourceMapTo ( "scriptcat-content.js" ) } \n` ;
838- retScript . push ( {
839- id : "scriptcat-content" ,
840- js : [ { code } ] ,
841- matches : [ "<all_urls>" ] ,
842- allFrames : true ,
843- runAt : "document_start" ,
844- world : "USER_SCRIPT" ,
845- excludeMatches,
846- excludeGlobs,
847- } ) ;
848- }
849835
836+ let retContent : chrome . scripting . RegisteredContentScript [ ] = [ ] ;
837+ let retInject : chrome . userScripts . RegisteredUserScript [ ] = [ ] ;
850838 // inject.js
851839 const injectJs = await this . getInjectJsCode ( ) ;
852840 if ( injectJs ) {
853- const apiScripts = this . compileInjectUserScript ( injectJs , messageFlag , {
841+ // 构建inject.js的脚本注册信息
842+ retInject = this . compileInjectUserScript ( injectJs , messageFlag , {
854843 excludeMatches,
855844 excludeGlobs,
856845 } ) ;
857- retScript . push ( ...apiScripts ) ;
846+ }
847+ // Note: Chrome does not support file.js?query
848+ // 注意:Chrome 不支持 file.js?query
849+ if ( isFirefox ( ) ) {
850+ // 使用 URLSearchParams 避免字符编码问题
851+ retContent = [
852+ {
853+ id : "scriptcat-content" ,
854+ js : [ `/src/content.js?${ new URLSearchParams ( { usp_flag : messageFlag } ) } &usp_end` ] ,
855+ matches : [ "<all_urls>" ] ,
856+ allFrames : true ,
857+ runAt : "document_start" ,
858+ excludeMatches,
859+ } satisfies chrome . scripting . RegisteredContentScript ,
860+ ] ;
861+ } else {
862+ const contentJs = await this . getContentJsCode ( ) ;
863+ if ( contentJs ) {
864+ const codeBody = `(function (MessageFlag) {\n${ contentJs } \n})('${ messageFlag } ')` ;
865+ const code = `${ codeBody } ${ sourceMapTo ( "scriptcat-content.js" ) } \n` ;
866+ retInject . push ( {
867+ id : "scriptcat-content" ,
868+ js : [ { code } ] ,
869+ matches : [ "<all_urls>" ] ,
870+ allFrames : true ,
871+ runAt : "document_start" ,
872+ world : "USER_SCRIPT" ,
873+ excludeMatches,
874+ excludeGlobs,
875+ } satisfies chrome . userScripts . RegisteredUserScript ) ;
876+ }
858877 }
859878
860- return retScript ;
879+ return { content : retContent , inject : retInject } ;
861880 }
862881
863882 // 如果是重复注册,需要先调用 unregisterUserscripts
@@ -869,8 +888,8 @@ export class RuntimeService {
869888 if ( runtimeGlobal . registered ) {
870889 // 异常情况
871890 // 检查scriptcat-content和scriptcat-inject是否存在
872- const res = await chrome . userScripts . getScripts ( { ids : [ "scriptcat-content" , "scriptcat- inject"] } ) ;
873- if ( res . length === 2 ) {
891+ const res = await chrome . userScripts . getScripts ( { ids : [ "scriptcat-inject" ] } ) ;
892+ if ( res . length === 1 ) {
874893 return ;
875894 }
876895 // scriptcat-content/scriptcat-inject不存在的情况
@@ -894,9 +913,9 @@ export class RuntimeService {
894913 const particularScriptList = await this . getParticularScriptList ( options ) ;
895914 // getContentAndInjectScript依赖loadScriptMatchInfo
896915 // 需要等getParticularScriptList完成后再执行
897- const generalScriptList = await this . getContentAndInjectScript ( options ) ;
916+ const { inject : injectScriptList , content : contentScriptList } = await this . getContentAndInjectScript ( options ) ;
898917
899- const list : chrome . userScripts . RegisteredUserScript [ ] = [ ...particularScriptList , ...generalScriptList ] ;
918+ const list : chrome . userScripts . RegisteredUserScript [ ] = [ ...particularScriptList , ...injectScriptList ] ;
900919
901920 runtimeGlobal . registered = true ;
902921 try {
@@ -921,6 +940,13 @@ export class RuntimeService {
921940 }
922941 }
923942 }
943+ if ( contentScriptList . length > 0 ) {
944+ try {
945+ await chrome . scripting . registerContentScripts ( contentScriptList ) ;
946+ } catch ( e : any ) {
947+ this . logger . error ( "register content.js error" , Logger . E ( e ) ) ;
948+ }
949+ }
924950 }
925951
926952 // 给指定tab发送消息
0 commit comments