import { showToastError } from '../../common/showToast' ; import { saveNewSettings } from './settings' ; import { defaultSiteSpecificShortReasons } from './defaultSiteSpecificShortReasons' ; /**
 * Make a network request to get the site-specific interface to close the question,
 * save the results in localStorage
 */ export const populateCloseReasons = ( createInterface ) => { const questionId = window . location . href . match ( /\d+/ ) ! [ 0 ] ; console . log ( 'Stack One Click VTC: Fetching site-specific close reasons...' ) ; fetch ( `${ window . location . origin }/flags/questions/${ questionId }/close/popup` ) . then ( res => res . text ( ) ) . then ( ( popupText ) => { handlePopup ( popupText , createInterface ) ; } ) . catch ( ( error ) => { console . error ( error ) ; showToastError ( 'Stack One Click VTC: An error occurred while fetching site-specific close reasons, see console for details' ) ; } ) ; } ; const handlePopup = ( popupText , createInterface ) => { const doc = new DOMParser ( ) . parseFromString ( popupText , 'text/html' ) ; const siteSpecificRadios = doc . querySelectorAll < HTMLInputElement > ( 'input[name="siteSpecificCloseReasonId"]' ) ; if ( ! siteSpecificRadios . length ) { showToastError ( 'Stack One Click VTC: No site-specific radio buttons found, try going to a different question' ) ; return ; } const thisSiteShortReasons = defaultSiteSpecificShortReasons [ window . StackExchange . options . site . name ] || [ ] ; const siteSpecificCloseReasons = [ ] ; siteSpecificRadios . forEach ( ( radio , i ) => { const origLabelText = radio . parentElement ! . nextElementSibling ! . querySelector ( 'label' ) ! . textContent ! ; const longReasonText = origLabelText . split ( '\n' ) . map ( str => str . trim ( ) ) . filter ( Boolean ) . join ( '. ' ) ; if ( longReasonText . includes ( 'add a comment' ) || longReasonText . includes ( 'another site' ) || longReasonText . includes ( 'Blatantly' ) ) { return ; } const siteSpecificCloseReasonId = Number ( radio . value ) ; const reasonText = thisSiteShortReasons [ i ] || longReasonText . match ( /.{1,29}\S(?= |$)/ ) ! [ 0 ] ; siteSpecificCloseReasons . push ( { siteSpecificCloseReasonId , longReasonText , reasonText } ) ; } ) ; saveNewSettings ( siteSpecificCloseReasons ) ; createInterface ( ) ; } ;