import "regenerator-runtime/runtime" ; import { extractSrc , getElmOfSrcCode , isAcceptableLocation , isConverted , toggleAppOrSrcCodeDom , } from "../../app-src/contentscript/data/DomRepository" ; import { getDocument } from "../../app-src/contentscript/data/QuerySelector/Document" ; import { render } from "../../app-src/contentscript/presentation" ; import { convertToObject } from "../../app-src/contentscript/util/YmlUtils" ; import { APP_RENDER_ID } from "../../app-src/shared/constants/App" ; import { EXEC_CONVERT_SWAGGER } from "../../app-src/shared/constants/SendMessageTypes" ; import { ExecConvertSwaggerMessage } from "../../app-src/shared/types/SendMessage" ; /**
 * contentscriptのエントリーポイント
 * backgroundからイベントを受け取って実行
 */ chrome . runtime . onMessage . addListener ( ( message ) => { if ( message . type === EXEC_CONVERT_SWAGGER ) { try { execConvertSwagger ( ) ; } catch ( _error ) { const error = _error ; alert ( `No operation. Can not convert.

[Cause]
${ error . message }` ) ; console . error ( error . stack ) ; } } } ) ; const execConvertSwagger = ( ) : void => { if ( ! isAcceptableLocation ( getDocument ( ) ) ) { throw new Error ( "Unsupported site or filename-extension." ) ; } if ( ! isConverted ( ) ) { const srcCode = extractSrc ( ) ; const swaggerJson = convertToObject ( srcCode ) ; injectApp ( ) ; render ( swaggerJson || "" ) ; } toggleAppOrSrcCodeDom ( ) ; } ; /**
 * この App を描画するための DOM 等を inject する
 */ const injectApp = ( ) : void => { const injWrapper = getDocument ( ) . createElement ( "div" ) ; injWrapper . innerHTML = `
<script>
  var global = global || window;
</script>
<div id="${ APP_RENDER_ID }"><div>
` ; const elm = getElmOfSrcCode ( ) ; elm . appendChild ( injWrapper ) ; elm . style . width = "-webkit-fill-available" ; global . Buffer = global . Buffer || require ( "buffer" ) . Buffer ; } ;