@@ -2,29 +2,26 @@ import { Client, sendMessage } from "@Packages/message/client";
22import { type CustomEventMessage } from "@Packages/message/custom_event_message" ;
33import { forwardMessage , type Server } from "@Packages/message/server" ;
44import type { MessageSend } from "@Packages/message/types" ;
5- import type { GMInfoEnv } from "./types" ;
6- import type { ScriptLoadInfo } from "../service_worker/types" ;
75import type { ScriptExecutor } from "./script_executor" ;
8- import { isInjectIntoContent } from "./utils" ;
96import { RuntimeClient } from "../service_worker/client" ;
107
118// content页的处理
129export default class ContentRuntime {
1310 // 运行在content页面的脚本
14- contentScript : Map < string , ScriptLoadInfo > = new Map ( ) ;
11+ private readonly contentScriptSet : Set < string > = new Set ( ) ;
1512
1613 constructor (
1714 // 监听来自service_worker的消息
18- private extServer : Server ,
15+ private readonly extServer : Server ,
1916 // 监听来自inject的消息
20- private server : Server ,
17+ private readonly server : Server ,
2118 // 发送给扩展service_worker的通信接口
22- private senderToExt : MessageSend ,
19+ private readonly senderToExt : MessageSend ,
2320 // 发送给inject的消息接口
24- private senderToInject : CustomEventMessage ,
21+ private readonly senderToInject : CustomEventMessage ,
2522 // 脚本执行器消息接口
26- private scriptExecutorMsg : CustomEventMessage ,
27- private scriptExecutor : ScriptExecutor
23+ private readonly scriptExecutorMsg : CustomEventMessage ,
24+ private readonly scriptExecutor : ScriptExecutor
2825 ) { }
2926
3027 init ( ) {
@@ -76,7 +73,7 @@ export default class ContentRuntime {
7673 let parentNode : EventTarget | undefined ;
7774 // 判断是不是content脚本发过来的
7875 let msg : CustomEventMessage ;
79- if ( this . contentScript . has ( data . uuid ) || this . scriptExecutor . execMap . has ( data . uuid ) ) {
76+ if ( this . contentScriptSet . has ( data . uuid ) || this . scriptExecutor . execMap . has ( data . uuid ) ) {
8077 msg = this . scriptExecutorMsg ;
8178 } else {
8279 msg = this . senderToInject ;
@@ -126,38 +123,25 @@ export default class ContentRuntime {
126123 ) ;
127124 }
128125
129- pageLoad ( messageFlags : MessageFlags ) {
130- this . scriptExecutor . checkEarlyStartScript ( "content" , messageFlags ) ;
131-
126+ pageLoad ( messageFlag : string ) {
127+ this . scriptExecutor . checkEarlyStartScript ( "content" , messageFlag ) ;
132128 const client = new RuntimeClient ( this . senderToExt ) ;
133- // 向service_worker请求脚本列表
134- client . pageLoad ( ) . then ( ( data ) => {
135- this . start ( data . scripts , data . envInfo ) ;
136- } ) ;
137- }
138-
139- start ( scripts : ScriptLoadInfo [ ] , envInfo : GMInfoEnv ) {
140- // 启动脚本
141- const client = new Client ( this . senderToInject , "inject" ) ;
142- // 根据@inject-into content过滤脚本
143- const injectScript : ScriptLoadInfo [ ] = [ ] ;
144- const contentScript : ScriptLoadInfo [ ] = [ ] ;
145- for ( const script of scripts ) {
146- if ( isInjectIntoContent ( script . metadata ) ) {
147- contentScript . push ( script ) ;
148- continue ;
129+ // 向service_worker请求脚本列表及环境信息
130+ client . pageLoad ( ) . then ( ( o ) => {
131+ if ( ! o . ok ) return ;
132+ const { injectScriptList, contentScriptList, envInfo } = o ;
133+ // 启动脚本:向 inject页面 发送脚本列表及环境信息
134+ const client = new Client ( this . senderToInject , "inject" ) ;
135+ // 根据@inject-into content过滤脚本
136+ client . do ( "pageLoad" , { injectScriptList, envInfo } ) ;
137+ // 处理注入到content环境的脚本
138+ for ( const script of contentScriptList ) {
139+ this . contentScriptSet . add ( script . uuid ) ;
149140 }
150- injectScript . push ( script ) ;
151- }
152- client . do ( "pageLoad" , { scripts : injectScript , envInfo } ) ;
153-
154- // 处理注入到content环境的脚本
155- for ( const script of contentScript ) {
156- this . contentScript . set ( script . uuid , script ) ;
157- }
158- // 监听事件
159- this . scriptExecutor . init ( envInfo ) ;
160- // 启动脚本
161- this . scriptExecutor . start ( contentScript ) ;
141+ // 监听事件
142+ this . scriptExecutor . setEnvInfo ( envInfo ) ;
143+ // 启动脚本
144+ this . scriptExecutor . startScripts ( contentScriptList ) ;
145+ } ) ;
162146 }
163147}
0 commit comments