11// splitChunks对injected可能会有问题
22
3- import { ExternalWhitelist } from " ./apps/config" ;
4- import { FrontendGrant , ScriptContext } from " ./apps/grant/frontend" ;
5- import { BrowserMsg } from " ./apps/msg-center/browser" ;
6- import { ExternalMessage , ScriptExec , ScriptValueChange } from " ./apps/msg-center/event" ;
7- import { ScriptCache } from " ./model/do/script" ;
8- import { Value } from " ./model/do/value" ;
9- import { addStyle } from " ./pkg/frontend" ;
10- import { createContext } from " ./pkg/sandbox/compile" ;
11- import { buildThis } from " ./pkg/sandbox/sandbox" ;
3+ import { ExternalWhitelist } from ' ./apps/config' ;
4+ import { FrontendGrant , ScriptContext } from ' ./apps/grant/frontend' ;
5+ import { FrontendMsg } from ' ./apps/msg-center/browser' ;
6+ import { ExternalMessage , ScriptExec , ScriptValueChange } from ' ./apps/msg-center/event' ;
7+ import { ScriptCache } from ' ./model/do/script' ;
8+ import { Value } from ' ./model/do/value' ;
9+ import { addStyle } from ' ./pkg/frontend' ;
10+ import { createContext } from ' ./pkg/sandbox/compile' ;
11+ import { buildThis } from ' ./pkg/sandbox/sandbox' ;
1212
1313// 参考了tm的实现
1414function waitBody ( callback : ( ) => void ) {
1515 if ( document . body ) {
1616 return callback ( ) ;
1717 }
18- let listen = function ( ) {
18+ const listen = function ( ) {
1919 document . removeEventListener ( 'load' , listen , false ) ;
2020 document . removeEventListener ( 'DOMNodeInserted' , listen , false ) ;
2121 document . removeEventListener ( 'DOMContentLoaded' , listen , false ) ;
@@ -26,9 +26,9 @@ function waitBody(callback: () => void) {
2626 document . addEventListener ( 'DOMContentLoaded' , listen , false ) ;
2727} ;
2828
29- let browserMsg = new BrowserMsg ( ScriptFlag , false ) ;
30- browserMsg . listen ( " scripts" , ( msg ) => {
31- let scripts : ScriptCache [ ] = msg ;
29+ const browserMsg = new FrontendMsg ( ScriptFlag , false ) ;
30+ browserMsg . listen ( ' scripts' , ( msg ) => {
31+ const scripts : ScriptCache [ ] = msg ;
3232 browserMsg . listen ( ScriptValueChange , ( msg : Value ) => {
3333 scripts . forEach ( val => {
3434 if ( ! val . value ) {
@@ -42,15 +42,15 @@ browserMsg.listen("scripts", (msg) => {
4242 browserMsg . listen ( ScriptExec , ( msg ) => {
4343 for ( let i = 0 ; i < scripts . length ; i ++ ) {
4444 if ( scripts [ i ] . uuid == msg ) {
45- ( < any > window ) [ scripts [ i ] . flag ! ] . apply ( scripts [ i ] . context , [ scripts [ i ] . context ] ) ;
45+ ( < { [ key : string ] : ( context : ScriptContext ) => void } > < unknown > window ) [ scripts [ i ] . flag ] . apply ( scripts [ i ] . context , [ scripts [ i ] . context ] ) ;
4646 break ;
4747 }
4848 }
4949 } ) ;
5050 scripts . forEach ( script => {
5151 // 构建沙盒
5252 let context : ScriptContext ;
53- if ( script . grantMap ! [ 'none' ] ) {
53+ if ( script . grantMap [ 'none' ] ) {
5454 context = < any > window ;
5555 } else {
5656 context = new FrontendGrant ( script , browserMsg ) ;
@@ -62,18 +62,18 @@ browserMsg.listen("scripts", (msg) => {
6262 if ( script . metadata [ 'run-at' ] && ( script . metadata [ 'run-at' ] [ 0 ] === 'document-menu' || script . metadata [ 'run-at' ] [ 0 ] === 'document-body' ) ) {
6363 if ( script . metadata [ 'run-at' ] [ 0 ] === 'document-body' ) {
6464 waitBody ( ( ) => {
65- if ( ( < any > window ) [ script . flag ! ] ) {
66- ( < any > window ) [ script . flag ! ] . apply ( context , [ context ] ) ;
65+ if ( ( < { [ key : string ] : ( ) => void } > < unknown > window ) [ script . flag ] ) {
66+ ( < { [ key : string ] : ( context : ScriptContext ) => void } > < unknown > window ) [ script . flag ] . apply ( context , [ context ] ) ;
6767 }
68- Object . defineProperty ( window , script . flag ! , {
68+ Object . defineProperty ( window , script . flag , {
6969 get : ( ) => { return undefined ; } ,
70- set : ( val ) => {
70+ set : ( val : ( context : ScriptContext ) => void ) => {
7171 val . apply ( context , [ context ] ) ;
7272 }
7373 } ) ;
7474 // 注入css
7575 script . metadata [ 'require-css' ] ?. forEach ( val => {
76- let res = script . resource ! [ val ] ;
76+ const res = script . resource [ val ] ;
7777 if ( res ) {
7878 addStyle ( res . content ) ;
7979 }
@@ -82,18 +82,18 @@ browserMsg.listen("scripts", (msg) => {
8282 }
8383 return ;
8484 }
85- if ( ( < any > window ) [ script . flag ! ] ) {
86- ( < any > window ) [ script . flag ! ] . apply ( context , [ context ] ) ;
85+ if ( ( < { [ key : string ] : ( ) => void } > < unknown > window ) [ script . flag ] ) {
86+ ( < { [ key : string ] : ( context : ScriptContext ) => void } > < unknown > window ) [ script . flag ] . apply ( context , [ context ] ) ;
8787 }
88- Object . defineProperty ( window , script . flag ! , {
88+ Object . defineProperty ( window , script . flag , {
8989 get : ( ) => { return undefined ; } ,
90- set : ( val ) => {
90+ set : ( val : ( context : ScriptContext ) => void ) => {
9191 val . apply ( context , [ context ] ) ;
9292 }
9393 } ) ;
9494 // 注入css
9595 script . metadata [ 'require-css' ] ?. forEach ( val => {
96- let res = script . resource ! [ val ] ;
96+ const res = script . resource [ val ] ;
9797 if ( res ) {
9898 addStyle ( res . content ) ;
9999 }
@@ -107,22 +107,23 @@ browserMsg.listen("scripts", (msg) => {
107107for ( let i = 0 ; i < ExternalWhitelist . length ; i ++ ) {
108108 if ( window . location . host . endsWith ( ExternalWhitelist [ i ] ) ) {
109109 // 注入
110- let isInstalledCallback : any ;
111- ( < any > window ) . external = window . external || { } ;
112- browserMsg . listen ( ExternalMessage , ( msg ) => {
113- switch ( msg . action ) {
114- case "isInstalled" :
115- isInstalledCallback ( msg . data ) ;
110+ let isInstalledCallback : ( data : any ) => void ;
111+ ( < { external : any } > < unknown > window ) . external = window . external || { } ;
112+ browserMsg . listen ( ExternalMessage , ( msg : any ) => {
113+ const m = < { action : string , data : any } > msg ;
114+ switch ( m . action ) {
115+ case 'isInstalled' :
116+ isInstalledCallback ( m . data ) ;
116117 break ;
117118 }
118119 } ) ;
119- ( < any > window . external ) . Scriptcat = {
120+ ( ( < { external : { Scriptcat : { isInstalled : ( name : string , namespace : string , callback : any ) => void } } } > < unknown > window ) . external ) . Scriptcat = {
120121 isInstalled ( name : string , namespace : string , callback : any ) {
121122 isInstalledCallback = callback ;
122123 browserMsg . send ( ExternalMessage , { 'action' : 'isInstalled' , 'params' : { name, namespace } } ) ;
123124 }
124125 } ;
125- ( < any > window . external ) . Tampermonkey = ( < any > window . external ) . Scriptcat ;
126+ ( ( < { external : { Tampermonkey : any } } > < unknown > window ) . external ) . Tampermonkey = ( ( < { external : { Scriptcat : any } } > < unknown > window ) . external ) . Scriptcat ;
126127 break ;
127128 }
128129
0 commit comments