import "../common/init/commonContext" ; import { AddTasks } from "../common/apis/messages" ; import { onStoredStateChange } from "../common/state/listen" ; import { DOWNLOAD_ONLY_PROTOCOLS , startsWithAnyProtocol } from "../common/apis/protocols" ; const LEFT_MOUSE_BUTTON = 0 ; let enabled = true ; onStoredStateChange ( ( state ) => { enabled = state . settings . shouldHandleDownloadLinks ; } ) ; function recursivelyFindAnchorAncestor ( e , depth = 10 ) { if ( e == null ) { return undefined ; } else if ( e instanceof HTMLAnchorElement ) { return e ; } else if ( depth === 0 ) { return undefined ; } else { return recursivelyFindAnchorAncestor ( e . parentElement , depth - 1 ) ; } } document . addEventListener ( "click" , ( e ) => { if ( enabled && e . button === LEFT_MOUSE_BUTTON ) { const anchor = recursivelyFindAnchorAncestor ( e . target as HTMLElement ) ; if ( anchor != null && anchor . href && startsWithAnyProtocol ( anchor . href , DOWNLOAD_ONLY_PROTOCOLS ) ) { AddTasks . send ( [ anchor . href ] ) ; e . preventDefault ( ) ; } } } ) ;