import { createPluginElement , getSpellCheckTextElement , removePluginElement } from "../dom-helper" ; import { SpellcheckController } from "./spellcheck-controller" ; import { Filter } from "../Popup/filter" ; import { browser } from 'webextension-polyfill-ts' ; const controller = SpellcheckController . getInstance ( ) ; let userFilters = [ ] ; let active = false ; let syncScroll = false ; const s = document . createElement ( 'script' ) ; s . src = browser . runtime . getURL ( 'js/page-script.bundle.js' ) ; document . head . appendChild ( s ) ; s . onload = ( ) => { s . remove ( ) ; } ; browser . storage . sync . get ( [ 'active' ] ) . then ( result => { active = result . active === undefined ? true : result . active ; if ( active ) { createPluginElement ( ) ; getSpellCheckTextElement ( ) ?. addEventListener ( 'input' , ( ) => { controller . textAreaInputChangeEvent ( ) ; } ) ; setTextareaScrollListener ( ) ; setTimeout ( ( ) => { const textarea = getSpellCheckTextElement ( ) ; if ( textarea ) { const current = document . activeElement ; getSpellCheckTextElement ( ) ! . selectionEnd = 1 ; getSpellCheckTextElement ( ) ?. focus ( ) ; ( current as HTMLElement ) ?. focus ( ) ; } } , 2000 ) ; } } ) ; browser . storage . onChanged . addListener ( changes => { if ( changes [ 'active' ] ) { active = changes [ 'active' ] . newValue ; if ( active ) { createPluginElement ( ) ; getSpellCheckTextElement ( ) ?. addEventListener ( 'input' , ( ) => { controller . textAreaInputChangeEvent ( ) ; } ) ; setTextareaScrollListener ( ) ; getSpellCheckTextElement ( ) ?. focus ( ) ; } else { removePluginElement ( ) ; } } } ) ; browser . storage . sync . get ( [ 'syncScroll' ] ) . then ( result => { syncScroll = result . syncScroll === undefined ? true : result . syncScroll ; } ) ; browser . storage . onChanged . addListener ( changes => { if ( changes [ 'syncScroll' ] ) { syncScroll = changes [ 'syncScroll' ] . newValue ; } } ) ; browser . storage . sync . get ( [ 'customRegex' ] ) . then ( result => { userFilters = result . customRegex || [ ] ; } ) ; browser . storage . onChanged . addListener ( changes => { if ( changes [ 'customRegex' ] ) { userFilters = changes [ 'customRegex' ] . newValue ; } } ) ; let aside = document . querySelector ( 'aside.editor-sidebar' ) ; aside ?. addEventListener ( "click" , ( ) => { if ( active ) { browser . storage . sync . set ( { active : false } ) ; setTimeout ( ( ) => { browser . storage . sync . set ( { active : true } ) ; } , 1000 ) ; } } ) ; document . addEventListener ( 'overleaf_scroll' , e => { const percentage = e . detail ; const textarea = getSpellCheckTextElement ( ) ; if ( textarea && syncScroll ) { textarea . scrollTop = textarea . scrollHeight * ( percentage / 100 ) ; } } ) ; setInterval ( ( ) => { const message = JSON . stringify ( { method : 'getValue' , args : [ ] } ) ; document . dispatchEvent ( new CustomEvent ( 'call_command' , { detail : message } ) ) ; } , 2000 ) ; document . addEventListener ( 'return_command' , function ( e ) { const message = JSON . parse ( e . detail ) ; controller . handleOverleafReturnCommand ( message , userFilters ) ; } ) ; function setTextareaScrollListener ( ) { const textarea = getSpellCheckTextElement ( ) ; if ( textarea ) { textarea . addEventListener ( 'scroll' , function ( ) { if ( syncScroll ) { const percentage = textarea . scrollTop / textarea . scrollHeight * 100 ; if ( textarea . scrollTop != 0 ) { document . dispatchEvent ( new CustomEvent ( 'textarea_scroll' , { detail : percentage } ) ) ; } } } ) ; } } export { } ;