import { APP_RENDER_ID } from "../../../shared/constants/App" ; import { CastAny } from "../../../shared/types/utils" ; import { querySelector , querySelectorAll , querySelectorStrict , } from "../QuerySelector" ; /**
 * DOMアクセス全般を実装する
 */ const RX_SWAGGER_PAGE = /^https:\/\/github\.com\/.*\.(ya?ml|json)($|#L\d+$|#L\d+-L\d+$)/ ; export const isAcceptableLocation = ( documentInstance ) : boolean => { return RX_SWAGGER_PAGE . test ( documentInstance . location . href ) ; } ; export const isConverted = ( ) : boolean => { return querySelector ( `#${ APP_RENDER_ID }` ) != null ; } ; export const getElmOfSrcCode = ( ) : HTMLElement => { const selector = "#repo-content-pjax-container div.Box-body table" ; const element = querySelector ( selector ) ; if ( element ?. textContent ?. length == null || element . textContent . length === 0 ) { throw new Error ( `Unexpected DOM. Probably GitHub has been updated. Please contact the developer or wait until the extension is fixed. selector: "${ selector }"` ) ; } return element ; } ; export const extractSrc = ( ) : string => { const elm = getElmOfSrcCode ( ) ; if ( ! elm . textContent ) { throw new Error ( "Unexpected null" ) ; } return ( elm . textContent . trim ( ) . split ( "\n" ) . filter ( ( line ) => line . trim ( ) . length !== 0 ) . map ( ( line ) => line . replace ( /^ {10}/ , "" ) ) . join ( "\n" ) ) ; } ; export const toggleAppOrSrcCodeDom = ( ) : void => { const elm = getElmOfSrcCode ( ) ; const srcDom = elm . children [ 0 ] as HTMLElement ; if ( srcDom . style . display === "none" ) { srcDom . style . display = "" ; querySelectorStrict ( `#${ APP_RENDER_ID }` ) . style . display = "none" ; } else { srcDom . style . display = "none" ; querySelectorStrict ( `#${ APP_RENDER_ID }` ) . style . display = "" ; } } ; /**
 * API定義・Modelsのタイトル（サマリー）の開閉エリアを取得して返す
 */ export const getElmOfSwaggerDefOpener = ( isOpened ) : readonly HTMLButtonElement [ ] => { if ( isOpened ) { const apiDefs = querySelectorAll ( `.opblock-tag-section .opblock-tag[data-is-open="true"]` ) ; const modelDefs = querySelectorAll ( `section.models.is-open > h4` ) ; return [ ... apiDefs , ... modelDefs ] as CastAny ; } const apiDefs = querySelectorAll ( `.opblock-tag-section .opblock-tag[data-is-open="false"] button` ) ; const modelDefs = querySelectorAll ( `section.models:not(.is-open) > h4` ) ; return [ ... apiDefs , ... modelDefs ] as CastAny ; } ; param { boolean } isOpened export const getElmOfSwaggerEndPointDefHeaders = ( isOpened ) : readonly HTMLDivElement [ ] => { if ( isOpened ) { return querySelectorAll ( "div.opblock.is-open > .opblock-summary" ) as CastAny ; } return querySelectorAll ( "div.opblock:not(.is-open) > .opblock-summary" ) as CastAny ; } ; param { boolean } isOpened export const getElmOfSwaggerSchemasModelHeaders = ( isOpened ) : readonly HTMLDivElement [ ] => { if ( isOpened ) { return querySelectorAll ( "span.model-box span.model-toggle:not(.collapsed)" ) as CastAny ; } return querySelectorAll ( "span.model-box span.model-toggle.collapsed" ) as CastAny ; } ;