11'use strict'
22
3- let performFix = ( fn : ( saveUnAuthPicEnabled : boolean ) => Promise < boolean > ) : void => {
4- chrome . storage . sync . get ( 'ifSaveUnAuthPic' , items => {
5- fn ( items [ 'ifSaveUnAuthPic' ] ) . then ( saveUnAuthPicEnabled => {
3+ type EnableSaveUnAuthPic = ( saveUnAuthPicEnabled : boolean ) => Promise < boolean >
4+ type ContextEvent = ( e : Event ) => boolean
5+ type UrlCheck = ( url : string ) => boolean
6+
7+ const performFix = ( fn : EnableSaveUnAuthPic ) : void => {
8+ chrome . storage . sync . get ( 'ifSaveUnAuthPic' , ( items : { [ key : string ] : any } ) => {
9+ fn ( items [ 'ifSaveUnAuthPic' ] as boolean ) . then ( ( saveUnAuthPicEnabled : boolean ) => {
610 if ( saveUnAuthPicEnabled ) {
7- const allowCut = e => {
8- e . stopImmediatePropagation ( )
9- return true
10- }
11- const allowCopy = e => {
12- e . stopImmediatePropagation ( )
13- return true
14- }
15- const allowPaste = e => {
16- e . stopImmediatePropagation ( )
17- return true
18- }
19- const allowContextMenu = e => {
11+ const allowContextMenu : ContextEvent = ( e ) => {
2012 e . stopImmediatePropagation ( )
2113 return true
2214 }
23- const allowDragStart = e => {
15+ const allowMouseDown : ContextEvent = ( e ) => {
2416 e . stopImmediatePropagation ( )
2517 return true
2618 }
27- const allowDrag = e => {
19+ const allowSelectStart : ContextEvent = ( e ) => {
2820 e . stopImmediatePropagation ( )
2921 return true
3022 }
31- const allowDrop = e => {
23+ const allowCut : ContextEvent = ( e ) => {
3224 e . stopImmediatePropagation ( )
3325 return true
3426 }
35- const allowMouseDown = e => {
27+ const allowCopy : ContextEvent = ( e ) => {
3628 e . stopImmediatePropagation ( )
3729 return true
3830 }
39- const allowSelectStart = e => {
31+ const allowPaste : ContextEvent = ( e ) => {
4032 e . stopImmediatePropagation ( )
4133 return true
4234 }
@@ -45,24 +37,26 @@ let performFix = (fn: (saveUnAuthPicEnabled: boolean) => Promise<boolean>): void
4537 document . addEventListener ( 'mousedown' , allowMouseDown , true )
4638 document . addEventListener ( 'selectstart' , allowSelectStart , true )
4739
48- // document.addEventListener('cut', allowCut, true)
49- // document.addEventListener('copy', allowCopy, true)
50- // document.addEventListener('paste', allowPaste, true)
51- // document.addEventListener('dragstart', allowDragStart, true)
52- // document.addEventListener('drag', allowDrag, true)
53- // document.addEventListener('drop', allowDrop, true)
40+ document . addEventListener ( 'cut' , allowCut , true )
41+ document . addEventListener ( 'copy' , allowCopy , true )
42+ document . addEventListener ( 'paste' , allowPaste , true )
5443 }
5544 chrome . runtime . onMessage . addListener ( ( request , sender , sendResponse ) => {
56- switch ( request . type ) {
45+ const message = request as Message
46+ switch ( message . type ) {
5747 case 'C_SAVE' :
58- const selectInfo : chrome . contextMenus . OnClickData = request . info
59- const imgSrc : string = selectInfo . srcUrl !
60- console . log ( `info: ${ JSON . stringify ( request . info ) } ` )
61- console . log ( `tab: ${ JSON . stringify ( request . tab ) } ` )
62- const durl : string = imgSrc . substring ( 0 , imgSrc . lastIndexOf ( '/' ) )
63- console . log ( `durl: ${ durl } ` )
64- if ( isImg ( durl ) ) {
65- sendResponse ( { durl } )
48+ const imgmsg : SaveImageMessage = request as SaveImageMessage
49+ const selectInfo = imgmsg . info
50+ const imgSrc : string | undefined = selectInfo . srcUrl
51+ console . log ( `info: ${ JSON . stringify ( imgmsg . info ) } ` )
52+ console . log ( `tab: ${ JSON . stringify ( imgmsg . tab ) } ` )
53+ if ( typeof imgSrc === 'string' ) {
54+ const durl : string = imgSrc . substring ( 0 , imgSrc . lastIndexOf ( '/' ) )
55+ console . log ( `durl: ${ durl } ` )
56+ if ( isImg ( durl ) ) {
57+ const response : ImageResponse = { durl }
58+ sendResponse ( response )
59+ }
6660 }
6761 break
6862 default : break
@@ -72,9 +66,9 @@ let performFix = (fn: (saveUnAuthPicEnabled: boolean) => Promise<boolean>): void
7266 } )
7367}
7468
75- performFix ( async saveUnAuthPicEnabled => { return saveUnAuthPicEnabled } )
69+ performFix ( async ( saveUnAuthPicEnabled : boolean ) => { return saveUnAuthPicEnabled } )
7670
77- const isImg : ( string ) => boolean = ( imgurl : string ) => {
71+ const isImg : UrlCheck = ( imgurl : string ) => {
7872 const suffix = imgurl . substring ( imgurl . lastIndexOf ( '.' ) , imgurl . length )
7973 return / \. ( g i f | j p g | j p e g | p n g | a p n g | w e b p | b m p | t i f f | s v g | e x i f | w m f ) $ / . test ( suffix . toLowerCase ( ) )
8074}
0 commit comments