document . addEventListener ( "DOMContentLoaded" , ( event ) => { let pageHref = null ; let filledThisHref = false ; let delayFillTimeout ; const enabledKey = "enableAutoFillOnPageLoad" ; chrome . storage . local . get ( enabledKey , ( obj ) => { if ( obj != null && obj [ enabledKey ] === true ) { setInterval ( ( ) => doFillIfNeeded ( ) , 500 ) ; } } ) ; chrome . runtime . onMessage . addListener ( ( msg , sender , sendResponse ) => { if ( msg . command === "fillForm" && pageHref === msg . url ) { filledThisHref = true ; } } ) ; function doFillIfNeeded ( force = false ) { if ( force || pageHref !== window . location . href ) { if ( ! force ) { filledThisHref = false ; if ( delayFillTimeout != null ) { window . clearTimeout ( delayFillTimeout ) ; } delayFillTimeout = window . setTimeout ( ( ) => { if ( ! filledThisHref ) { doFillIfNeeded ( true ) ; } } , 1500 ) ; } pageHref = window . location . href ; const msg = { command : "bgCollectPageDetails" , sender : "autofiller" , } ; chrome . runtime . sendMessage ( msg ) ; } } } ) ;