@@ -56,7 +56,7 @@ export type ScriptMenu = {
56
56
// 后台脚本将会将代码注入到沙盒中
57
57
@IoC . Singleton ( MessageHander , MessageSandbox , ResourceManager , ValueManager )
58
58
export default class Runtime extends Manager {
59
- messageSandbox : MessageSandbox ;
59
+ messageSandbox ? : MessageSandbox ;
60
60
61
61
scriptDAO : ScriptDAO ;
62
62
@@ -82,13 +82,11 @@ export default class Runtime extends Manager {
82
82
83
83
constructor (
84
84
message : MessageHander ,
85
- messageSandbox : MessageSandbox ,
86
85
resourceManager : ResourceManager ,
87
86
valueManager : ValueManager
88
87
) {
89
88
super ( message , "runtime" ) ;
90
89
this . scriptDAO = new ScriptDAO ( ) ;
91
- this . messageSandbox = messageSandbox ;
92
90
this . resourceManager = resourceManager ;
93
91
this . valueManager = valueManager ;
94
92
this . scriptFlag = randomString ( 8 ) ;
@@ -136,25 +134,27 @@ export default class Runtime extends Manager {
136
134
// 监听脚本运行状态
137
135
this . listenScriptRunStatus ( ) ;
138
136
137
+ // 启动普通脚本
139
138
this . scriptDAO . table . toArray ( ( items ) => {
140
139
items . forEach ( ( item ) => {
141
140
// 容错处理
142
141
if ( ! item ) {
143
142
this . logger . error ( "script is null" ) ;
144
143
return ;
145
144
}
145
+ if ( item . type !== SCRIPT_TYPE_NORMAL ) {
146
+ return ;
147
+ }
146
148
// 加载所有的脚本
147
149
if ( item . status === SCRIPT_STATUS_ENABLE ) {
148
150
this . enable ( item ) ;
149
- if ( item . type !== SCRIPT_TYPE_NORMAL ) {
150
- this . runBackScript . set ( item . id , item ) ;
151
- }
152
- } else if ( item . type === SCRIPT_TYPE_NORMAL ) {
151
+ } else {
153
152
// 只处理未开启的普通页面脚本
154
153
this . disable ( item ) ;
155
154
}
156
155
} ) ;
157
156
} ) ;
157
+
158
158
// 接受消息,注入脚本
159
159
// 获取注入源码
160
160
const { scriptFlag } = this ;
@@ -503,6 +503,28 @@ export default class Runtime extends Manager {
503
503
) ;
504
504
}
505
505
506
+ // 启动沙盒相关脚本
507
+ startSandbox ( messageSandbox : MessageSandbox ) {
508
+ this . messageSandbox = messageSandbox ;
509
+ this . scriptDAO . table . toArray ( ( items ) => {
510
+ items . forEach ( ( item ) => {
511
+ // 容错处理
512
+ if ( ! item ) {
513
+ this . logger . error ( "script is null" ) ;
514
+ return ;
515
+ }
516
+ if ( item . type === SCRIPT_TYPE_NORMAL ) {
517
+ return ;
518
+ }
519
+ // 加载所有的脚本
520
+ if ( item . status === SCRIPT_STATUS_ENABLE ) {
521
+ this . enable ( item ) ;
522
+ this . runBackScript . set ( item . id , item ) ;
523
+ }
524
+ } ) ;
525
+ } ) ;
526
+ }
527
+
506
528
listenScriptRunStatus ( ) {
507
529
// 监听沙盒发送的脚本运行状态消息
508
530
this . message . setHandler (
@@ -644,7 +666,7 @@ export default class Runtime extends Manager {
644
666
this . runBackScript . set ( script . id , script ) ;
645
667
return new Promise ( ( resolve , reject ) => {
646
668
this . messageSandbox
647
- . syncSend ( "enable" , script )
669
+ ? .syncSend ( "enable" , script )
648
670
. then ( ( ) => {
649
671
resolve ( true ) ;
650
672
} )
@@ -660,7 +682,7 @@ export default class Runtime extends Manager {
660
682
this . runBackScript . delete ( script . id ) ;
661
683
return new Promise ( ( resolve , reject ) => {
662
684
this . messageSandbox
663
- . syncSend ( "disable" , script . id )
685
+ ? .syncSend ( "disable" , script . id )
664
686
. then ( ( ) => {
665
687
resolve ( true ) ;
666
688
} )
@@ -673,14 +695,14 @@ export default class Runtime extends Manager {
673
695
674
696
async startBackgroundScript ( script : Script ) {
675
697
const scriptRes = await this . buildScriptRunResource ( script ) ;
676
- this . messageSandbox . syncSend ( "start" , scriptRes ) ;
698
+ this . messageSandbox ? .syncSend ( "start" , scriptRes ) ;
677
699
return Promise . resolve ( true ) ;
678
700
}
679
701
680
702
stopBackgroundScript ( scriptId : number ) {
681
703
return new Promise ( ( resolve , reject ) => {
682
704
this . messageSandbox
683
- . syncSend ( "stop" , scriptId )
705
+ ? .syncSend ( "stop" , scriptId )
684
706
. then ( ( resp ) => {
685
707
resolve ( resp ) ;
686
708
} )
0 commit comments