1- import { MessageQueue } from "@Packages/message/message_queue" ;
1+ import { MessageQueue , Unsubscribe } from "@Packages/message/message_queue" ;
22import { ExtMessageSender , GetSender , Group , MessageSend } from "@Packages/message/server" ;
33import {
44 Script ,
@@ -15,20 +15,17 @@ import { subscribeScriptDelete, subscribeScriptEnable, subscribeScriptInstall }
1515import { ScriptService } from "./script" ;
1616import { runScript , stopScript } from "../offscreen/client" ;
1717import { getRunAt } from "./utils" ;
18- import { InfoNotification , isUserScriptsAvailable , randomString } from "@App/pkg/utils/utils" ;
18+ import { isUserScriptsAvailable , randomString } from "@App/pkg/utils/utils" ;
1919import Cache from "@App/app/cache" ;
2020import { dealPatternMatches , UrlMatch } from "@App/pkg/utils/match" ;
2121import { ExtensionContentMessageSend } from "@Packages/message/extension_message" ;
2222import { sendMessage } from "@Packages/message/client" ;
2323import { compileInjectScript } from "../content/utils" ;
24- import { PopupService } from "./popup" ;
25- import Logger from "@App/app/logger/logger" ;
2624import LoggerCore from "@App/app/logger/core" ;
2725import PermissionVerify from "./permission_verify" ;
2826import { SystemConfig } from "@App/pkg/config/config" ;
2927import { ResourceService } from "./resource" ;
3028import { LocalStorageDAO } from "@App/app/repo/localStorage" ;
31- import i18n from "@App/locales/locales" ;
3229
3330// 为了优化性能,存储到缓存时删除了code、value与resource
3431export interface ScriptMatchInfo extends ScriptRunResouce {
@@ -52,6 +49,7 @@ export class RuntimeService {
5249 scriptMatchCache : Map < string , ScriptMatchInfo > | null | undefined ;
5350
5451 isEnableDeveloperMode = false ;
52+ isEnableUserscribe = true ;
5553
5654 constructor (
5755 private systemConfig : SystemConfig ,
@@ -102,8 +100,7 @@ export class RuntimeService {
102100 text : "!" ,
103101 } ) ;
104102 }
105- // 读取inject.js注入页面
106- this . registerInjectScript ( ) ;
103+
107104 // 监听脚本开启
108105 subscribeScriptEnable ( this . mq , async ( data ) => {
109106 const script = await this . scriptDAO . getAndCode ( data . uuid ) ;
@@ -114,6 +111,7 @@ export class RuntimeService {
114111 // 如果是后台脚本, 在offscreen中进行处理
115112 if ( script . type === SCRIPT_TYPE_NORMAL ) {
116113 // 加载页面脚本
114+ // 不管开没开启都要加载一次脚本信息
117115 await this . loadPageScript ( script ) ;
118116 if ( ! data . enable ) {
119117 await this . unregistryPageScript ( script . uuid ) ;
@@ -136,6 +134,32 @@ export class RuntimeService {
136134 this . deleteScriptMatch ( uuid ) ;
137135 } ) ;
138136
137+ this . systemConfig . addListener ( "enable_script" , ( enable ) => {
138+ this . isEnableUserscribe = enable ;
139+ if ( enable ) {
140+ this . registerUserscripts ( ) ;
141+ } else {
142+ this . unregisterUserscripts ( ) ;
143+ }
144+ } ) ;
145+ // 检查是否开启
146+ this . isEnableUserscribe = await this . systemConfig . getEnableScript ( ) ;
147+ if ( this . isEnableUserscribe ) {
148+ this . registerUserscripts ( ) ;
149+ }
150+ }
151+
152+ unsubscribe : Unsubscribe [ ] = [ ] ;
153+
154+ // 取消脚本注册
155+ unregisterUserscripts ( ) {
156+ chrome . userScripts . unregister ( ) ;
157+ this . deleteMessageFlag ( ) ;
158+ }
159+
160+ async registerUserscripts ( ) {
161+ // 读取inject.js注入页面
162+ this . registerInjectScript ( ) ;
139163 // 将开启的脚本发送一次enable消息
140164 const scriptDao = new ScriptDAO ( ) ;
141165 const list = await scriptDao . all ( ) ;
@@ -164,6 +188,10 @@ export class RuntimeService {
164188 } ) ;
165189 }
166190
191+ deleteMessageFlag ( ) {
192+ return Cache . getInstance ( ) . del ( "scriptInjectMessageFlag" ) ;
193+ }
194+
167195 getMessageFlag ( ) {
168196 return Cache . getInstance ( ) . get ( "scriptInjectMessageFlag" ) ;
169197 }
@@ -241,7 +269,7 @@ export class RuntimeService {
241269 return undefined ;
242270 }
243271 // 如果是iframe,判断是否允许在iframe里运行
244- if ( chromeSender . frameId !== undefined ) {
272+ if ( chromeSender . frameId ) {
245273 if ( scriptRes . metadata . noframes ) {
246274 return undefined ;
247275 }
@@ -420,8 +448,11 @@ export class RuntimeService {
420448 if ( ! this . scriptMatchCache ) {
421449 await this . loadScriptMatchInfo ( ) ;
422450 }
423- this . scriptMatchCache ! . get ( uuid ) ! . status = status ;
424- this . saveScriptMatchInfo ( ) ;
451+ const script = await this . scriptMatchCache ! . get ( uuid ) ;
452+ if ( script ) {
453+ script . status = status ;
454+ this . saveScriptMatchInfo ( ) ;
455+ }
425456 }
426457
427458 async deleteScriptMatch ( uuid : string ) {
@@ -487,8 +518,10 @@ export class RuntimeService {
487518 this . addScriptMatch ( scriptMatchInfo ) ;
488519
489520 // 如果脚本开启, 则注册脚本
490- if ( this . isEnableDeveloperMode && script . status === SCRIPT_STATUS_ENABLE ) {
491- if ( ! scriptRes . metadata [ "noframes" ] ) {
521+ if ( this . isEnableDeveloperMode && this . isEnableUserscribe && script . status === SCRIPT_STATUS_ENABLE ) {
522+ if ( scriptRes . metadata [ "noframes" ] ) {
523+ registerScript . allFrames = false ;
524+ } else {
492525 registerScript . allFrames = true ;
493526 }
494527 if ( scriptRes . metadata [ "run-at" ] ) {
@@ -524,19 +557,17 @@ export class RuntimeService {
524557 }
525558
526559 async unregistryPageScript ( uuid : string ) {
527- if ( ! this . isEnableDeveloperMode || ! ( await Cache . getInstance ( ) . get ( "registryScript:" + uuid ) ) ) {
560+ if (
561+ ! this . isEnableDeveloperMode ||
562+ ! this . isEnableUserscribe ||
563+ ! ( await Cache . getInstance ( ) . get ( "registryScript:" + uuid ) )
564+ ) {
528565 return ;
529566 }
530- chrome . userScripts . unregister (
531- {
532- ids : [ uuid ] ,
533- } ,
534- ( ) => {
535- // 删除缓存
536- Cache . getInstance ( ) . del ( "registryScript:" + uuid ) ;
537- // 修改脚本状态为disable
538- this . updateScriptStatus ( uuid , SCRIPT_STATUS_DISABLE ) ;
539- }
540- ) ;
567+ // 删除缓存
568+ Cache . getInstance ( ) . del ( "registryScript:" + uuid ) ;
569+ // 修改脚本状态为disable
570+ this . updateScriptStatus ( uuid , SCRIPT_STATUS_DISABLE ) ;
571+ chrome . userScripts . unregister ( { ids : [ uuid ] } ) ;
541572 }
542573}
0 commit comments