@@ -3,18 +3,18 @@ import { getStorageName } from "@App/pkg/utils/utils";
33import type { EmitEventRequest , ScriptLoadInfo } from "../service_worker/types" ;
44import ExecScript from "./exec_script" ;
55import type { GMInfoEnv , ScriptFunc , PreScriptFunc , ValueUpdateDataEncoded } from "./types" ;
6- import { addStyle } from "./utils" ;
6+ import { addStyle , definePropertyListener } from "./utils" ;
77
88export type ExecScriptEntry = {
9- scriptLoadInfo : any ;
9+ scriptLoadInfo : ScriptLoadInfo ;
1010 scriptFlag : string ;
1111 envInfo : any ;
1212 scriptFunc : any ;
1313} ;
1414
1515// 脚本执行器
1616export class ScriptExecutor {
17- execList : ExecScript [ ] = [ ] ;
17+ execList : Map < string , ExecScript > = new Map ( ) ;
1818
1919 envInfo : GMInfoEnv | undefined ;
2020 earlyScriptFlag : string [ ] = [ ] ;
@@ -27,15 +27,15 @@ export class ScriptExecutor {
2727
2828 emitEvent ( data : EmitEventRequest ) {
2929 // 转发给脚本
30- const exec = this . execList . find ( ( val ) => val . scriptRes . uuid === data . uuid ) ;
30+ const exec = this . execList . get ( data . uuid ) ;
3131 if ( exec ) {
3232 exec . emitEvent ( data . event , data . eventId , data . data ) ;
3333 }
3434 }
3535
3636 valueUpdate ( data : ValueUpdateDataEncoded ) {
3737 const { uuid, storageName } = data ;
38- for ( const val of this . execList ) {
38+ for ( const val of this . execList . values ( ) ) {
3939 if ( val . scriptRes . uuid === uuid || getStorageName ( val . scriptRes ) === storageName ) {
4040 val . valueUpdate ( data ) ;
4141 }
@@ -55,7 +55,7 @@ export class ScriptExecutor {
5555 const flag = script . flag ;
5656 // 如果是EarlyScriptFlag,处理沙盒环境
5757 if ( this . earlyScriptFlag . includes ( flag ) ) {
58- for ( const val of this . execList ) {
58+ for ( const val of this . execList . values ( ) ) {
5959 if ( val . scriptRes . flag === flag ) {
6060 // 处理早期脚本的沙盒环境
6161 val . updateEarlyScriptGMInfo ( this . envInfo ! ) ;
@@ -64,23 +64,10 @@ export class ScriptExecutor {
6464 }
6565 return ;
6666 }
67- // @ts -ignore
68- const scriptFunc = window [ flag ] ;
69- if ( scriptFunc ) {
70- // @ts -ignore
71- window [ flag ] = null ; // 释放物件参考
72- loadExec ( script , scriptFunc ) ;
73- } else {
74- // 监听脚本加载,和屏蔽读取
75- Object . defineProperty ( window , flag , {
76- configurable : true ,
77- set : ( val : ScriptFunc ) => {
78- // @ts -ignore
79- delete window [ flag ] ; // 删除 property setter 避免重复呼叫
80- loadExec ( script , val ) ;
81- } ,
82- } ) ;
83- }
67+
68+ definePropertyListener ( window , flag , ( val : ScriptFunc ) => {
69+ loadExec ( script , val ) ;
70+ } ) ;
8471 } ) ;
8572 }
8673
@@ -95,33 +82,17 @@ export class ScriptExecutor {
9582 } ) ;
9683 } ;
9784 this . earlyScriptFlag . forEach ( ( flag ) => {
98- // @ts -ignore
99- const scriptFunc = window [ flag ] as PreScriptFunc ;
100- if ( scriptFunc ) {
101- // @ts -ignore
102- window [ flag ] = null ; // 释放物件参考
103- loadExec ( flag , scriptFunc ) ;
104- } else {
105- // 监听脚本加载,和屏蔽读取
106- Object . defineProperty ( window , flag , {
107- configurable : true ,
108- set : ( val : PreScriptFunc ) => {
109- // @ts -ignore
110- delete window [ flag ] ; // 取消 property setter 避免重复呼叫
111- loadExec ( flag , val ) ;
112- } ,
113- } ) ;
114- }
85+ definePropertyListener ( window , flag , ( val : PreScriptFunc ) => {
86+ loadExec ( flag , val ) ;
87+ } ) ;
11588 } ) ;
11689 }
11790
11891 execScriptEntry ( scriptEntry : ExecScriptEntry ) {
119- const { scriptFlag , scriptLoadInfo, scriptFunc, envInfo } = scriptEntry ;
92+ const { scriptLoadInfo, scriptFunc, envInfo } = scriptEntry ;
12093
121- // @ts -ignore
122- delete window [ scriptFlag ] ;
12394 const exec = new ExecScript ( scriptLoadInfo , "content" , this . msg , scriptFunc , envInfo ) ;
124- this . execList . push ( exec ) ;
95+ this . execList . set ( scriptLoadInfo . uuid , exec ) ;
12596 const metadata = scriptLoadInfo . metadata || { } ;
12697 const resource = scriptLoadInfo . resource ;
12798 // 注入css
0 commit comments