@@ -208,6 +208,7 @@ export class RuntimeService {
208208 // 过滤掉undefined和未开启的
209209 return res . filter ( ( item ) => item ) as chrome . userScripts . RegisteredUserScript [ ] ;
210210 } ) ;
211+
211212 // 如果脚本开启, 则注册脚本
212213 if ( this . isEnableDeveloperMode && this . isEnableUserscribe ) {
213214 // 批量注册
@@ -313,23 +314,30 @@ export class RuntimeService {
313314 // 匹配当前页面的脚本
314315 const matchScriptUuid = await this . getPageScriptUuidByUrl ( chromeSender . url ! ) ;
315316
316- const scripts = matchScriptUuid . map ( ( uuid ) => {
317+ const enableScript = matchScriptUuid . reduce ( ( arr , uuid ) => {
317318 const scriptRes = Object . assign ( { } , this . scriptMatchCache ?. get ( uuid ) ) ;
318319 // 判断脚本是否开启
319320 if ( scriptRes . status === SCRIPT_STATUS_DISABLE ) {
320- return undefined ;
321+ return arr ;
322+ }
323+ // 判断注入页面类型
324+ if ( scriptRes . metadata [ "run-in" ] ) {
325+ // 判断插件运行环境
326+ const contextType = chrome . extension . inIncognitoContext ? "incognito-tabs" : "normal-tabs" ;
327+ if ( ! scriptRes . metadata [ "run-in" ] . includes ( contextType ) ) {
328+ return arr ;
329+ }
321330 }
322331 // 如果是iframe,判断是否允许在iframe里运行
323332 if ( chromeSender . frameId ) {
324333 if ( scriptRes . metadata . noframes ) {
325- return undefined ;
334+ return arr ;
326335 }
327336 }
328337 // 获取value
329- return scriptRes ;
330- } ) ;
331-
332- const enableScript = scripts . filter ( ( item ) => item ) as ScriptMatchInfo [ ] ;
338+ arr . push ( scriptRes ) ;
339+ return arr ;
340+ } , [ ] as ScriptMatchInfo [ ] ) ;
333341
334342 await Promise . all ( [
335343 // 加载value
@@ -533,6 +541,7 @@ export class RuntimeService {
533541 id : scriptRes . uuid ,
534542 js : [ { code : scriptRes . code } ] ,
535543 matches : patternMatches . patternResult ,
544+ allFrames : ! scriptRes . metadata [ "noframes" ] ,
536545 world : "MAIN" ,
537546 } ;
538547
@@ -563,12 +572,6 @@ export class RuntimeService {
563572 // 将脚本match信息放入缓存中
564573 this . addScriptMatch ( scriptMatchInfo ) ;
565574
566- // 注册脚本信息
567- if ( scriptRes . metadata [ "noframes" ] ) {
568- registerScript . allFrames = false ;
569- } else {
570- registerScript . allFrames = true ;
571- }
572575 if ( scriptRes . metadata [ "run-at" ] ) {
573576 registerScript . runAt = getRunAt ( scriptRes . metadata [ "run-at" ] ) ;
574577 }
0 commit comments