import { getCurrentTab , getSelectionText , sendMessageToTab , copyToClipboard } from './utils' ; import { CreateLink } from './createlink' ; import fmt from './formats' ; export class ShortcutHandler { initialize ( ) { console . log ( 'initializing ShortcutHandler' ) ; chrome . commands . onCommand . addListener ( this . onKeyboardShortcut . bind ( this ) ) ; } async onKeyboardShortcut ( command ) { switch ( command ) { case 'current-tab-link' : await fmt . load ( ) ; const def = fmt . getDefaultFormat ( ) ; const tab = await getCurrentTab ( ) ; const response = await getSelectionText ( tab . id ) ; let selectionText ; if ( response instanceof Object ) { selectionText = response . text ; } const cl = new CreateLink ( ) ; const link = await cl . formatInTab ( def , { selectionText , pageUrl : tab . url , } , tab ) ; cl . copyToClipboard ( tab . id , link ) . then ( ( ) => { this . flashBadge ( 'success' , def . label ) ; } , ( ) => { this . flashBadge ( 'fail' , '' ) ; } ) ; break ; } } flashBadge ( type , text ) { var color ; switch ( type ) { case "success" : color = "#738a05" ; break ; case "fail" : color = "#d11b24" ; text = "!" ; break ; default : return ; } chrome . action . setBadgeText ( { "text" : text } ) ; chrome . action . setBadgeBackgroundColor ( { "color" : color } ) ; function clearBadge ( type , text ) { chrome . action . setBadgeText ( { text : "" } ) ; chrome . action . setBadgeBackgroundColor ( { color : [ 0 , 0 , 0 , 255 ] } ) ; } setTimeout ( clearBadge , 1500 ) ; } }