@@ -56,7 +56,7 @@ export type ScriptMenu = {
5656// 后台脚本将会将代码注入到沙盒中
5757@IoC . Singleton ( MessageHander , MessageSandbox , ResourceManager , ValueManager )
5858export default class Runtime extends Manager {
59- messageSandbox : MessageSandbox ;
59+ messageSandbox ? : MessageSandbox ;
6060
6161 scriptDAO : ScriptDAO ;
6262
@@ -82,13 +82,11 @@ export default class Runtime extends Manager {
8282
8383 constructor (
8484 message : MessageHander ,
85- messageSandbox : MessageSandbox ,
8685 resourceManager : ResourceManager ,
8786 valueManager : ValueManager
8887 ) {
8988 super ( message , "runtime" ) ;
9089 this . scriptDAO = new ScriptDAO ( ) ;
91- this . messageSandbox = messageSandbox ;
9290 this . resourceManager = resourceManager ;
9391 this . valueManager = valueManager ;
9492 this . scriptFlag = randomString ( 8 ) ;
@@ -136,25 +134,27 @@ export default class Runtime extends Manager {
136134 // 监听脚本运行状态
137135 this . listenScriptRunStatus ( ) ;
138136
137+ // 启动普通脚本
139138 this . scriptDAO . table . toArray ( ( items ) => {
140139 items . forEach ( ( item ) => {
141140 // 容错处理
142141 if ( ! item ) {
143142 this . logger . error ( "script is null" ) ;
144143 return ;
145144 }
145+ if ( item . type !== SCRIPT_TYPE_NORMAL ) {
146+ return ;
147+ }
146148 // 加载所有的脚本
147149 if ( item . status === SCRIPT_STATUS_ENABLE ) {
148150 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 {
153152 // 只处理未开启的普通页面脚本
154153 this . disable ( item ) ;
155154 }
156155 } ) ;
157156 } ) ;
157+
158158 // 接受消息,注入脚本
159159 // 获取注入源码
160160 const { scriptFlag } = this ;
@@ -503,6 +503,28 @@ export default class Runtime extends Manager {
503503 ) ;
504504 }
505505
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+
506528 listenScriptRunStatus ( ) {
507529 // 监听沙盒发送的脚本运行状态消息
508530 this . message . setHandler (
@@ -644,7 +666,7 @@ export default class Runtime extends Manager {
644666 this . runBackScript . set ( script . id , script ) ;
645667 return new Promise ( ( resolve , reject ) => {
646668 this . messageSandbox
647- . syncSend ( "enable" , script )
669+ ? .syncSend ( "enable" , script )
648670 . then ( ( ) => {
649671 resolve ( true ) ;
650672 } )
@@ -660,7 +682,7 @@ export default class Runtime extends Manager {
660682 this . runBackScript . delete ( script . id ) ;
661683 return new Promise ( ( resolve , reject ) => {
662684 this . messageSandbox
663- . syncSend ( "disable" , script . id )
685+ ? .syncSend ( "disable" , script . id )
664686 . then ( ( ) => {
665687 resolve ( true ) ;
666688 } )
@@ -673,14 +695,14 @@ export default class Runtime extends Manager {
673695
674696 async startBackgroundScript ( script : Script ) {
675697 const scriptRes = await this . buildScriptRunResource ( script ) ;
676- this . messageSandbox . syncSend ( "start" , scriptRes ) ;
698+ this . messageSandbox ? .syncSend ( "start" , scriptRes ) ;
677699 return Promise . resolve ( true ) ;
678700 }
679701
680702 stopBackgroundScript ( scriptId : number ) {
681703 return new Promise ( ( resolve , reject ) => {
682704 this . messageSandbox
683- . syncSend ( "stop" , scriptId )
705+ ? .syncSend ( "stop" , scriptId )
684706 . then ( ( resp ) => {
685707 resolve ( resp ) ;
686708 } )
0 commit comments