import KifuStore from "./stores/KifuStore" ; declare var $ ; declare var jQuery ; declare var params ; declare var so ; async function start ( ) { if ( window . getSelection && getSelection ( ) . toString ( ) !== "" ) { return replaceSelected ( ) . catch ( fail ) ; } else { return replaceAll ( ) . catch ( fail ) ; } function fail ( e ) { window . alert ( "読込失敗: " + e ) ; throw e ; } async function replaceAll ( ) { const targetList = [ ] ; $ ( "applet param[name=KIFU]" ) . each ( function ( ) { const $parent = $ ( this ) . parent ( ) ; const base = $parent . attr ( "codebase" ) || "." ; const targetId = makeRandomId ( ) ; const kifuPath = base . replace ( /\/$/ , "" ) + "/" + $ ( this ) . val ( ) ; $parent . replaceWith ( "<div id='" + targetId + "'></div>" ) ; targetList . push ( { targetId , kifuPath , method : "applet" } ) ; } ) ; $ ( "object param[name=FlashVars]" ) . each ( function ( ) { const $parent = $ ( this ) . parent ( ) ; let kifuPath ; $ ( this ) . val ( ) . split ( "&" ) . map ( ( kv ) => { const s = kv . split ( "=" ) ; if ( s [ 0 ] === "kifu" ) { kifuPath = s [ 1 ] ; } } ) ; if ( ! kifuPath ) { return ; } const targetId = makeRandomId ( ) ; $parent . replaceWith ( "<div id='" + targetId + "'></div>" ) ; targetList . push ( { kifuPath , targetId , method : "object" } ) ; } ) ; const $flashcontent = $ ( "#flashcontent" ) ; const $so = $ ( "#so" ) ; if ( typeof so !== "undefined" && so . variables && so . variables . kifu && $flashcontent . length > 0 ) { $flashcontent . replaceWith ( "<div id='flashcontent'></div>" ) ; targetList . push ( { targetId : "flashcontent" , kifuPath : so . variables . kifu , method : "swo_flashcontent" } ) ; } else if ( typeof params !== "undefined" && params . FlashVars && $so . length > 0 ) { let kifuPath ; params . FlashVars . split ( "&" ) . forEach ( ( kv ) => { const s = kv . split ( "=" ) ; if ( s [ 0 ] === "kifu" ) { kifuPath = s [ 1 ] ; } } ) ; if ( kifuPath ) { $so . replaceWith ( "<div id='so'></div>" ) ; $so . css ( "visibility" , "visible" ) ; targetList . push ( { targetId : "so" , kifuPath , method : "swo_so" } ) ; } } else { $ ( "script" ) . filter ( ( i , script ) => script . textContent . indexOf ( "SWFObject" ) >= 0 ) . each ( ( i , script ) => { const kifuMatch = script . textContent . match ( /addVariable.+kifu.+"(.+)"/ ) ; const idMatch = script . textContent . match ( /write.+"(.+)"/ ) ; if ( kifuMatch && idMatch ) { targetList . push ( { kifuPath : kifuMatch [ 1 ] , targetId : idMatch [ 1 ] , method : "swo_bruteforce" } ) ; } } ) ; } if ( targetList . length === 0 ) { return Promise . reject ( "将棋盤が見つかりませんでした" ) ; } const Kifu = await loadKifuForJS ( ) ; return Promise . all ( targetList . map ( ( target ) => { return Kifu . load ( target . kifuPath . replace ( /\.(z|gz)$/gi , "" ) , target . targetId ) ; } ) ) ; } async function replaceSelected ( ) { const Kifu = await loadKifuForJS ( ) ; const selection = getSelection ( ) ; const id = makeRandomId ( ) ; $ ( "<div id='" + id + "'>" ) . insertAfter ( $ ( selection . focusNode ) ) ; return Promise . all ( [ Kifu . loadString ( selection . toString ( ) , id ) ] ) ; } } function makeRandomId ( ) { return ( "kifuforjs_" + Math . random ( ) . toString ( 36 ) . slice ( 2 ) ) ; } let promise = Promise . resolve ( ) ; if ( typeof $ === "undefined" || ! $ . fn || ! $ . fn . jquery ) { promise = promise . then ( ( ) => new Promise < void > ( ( resolve ) => { const scr = document . createElement ( "script" ) ; scr . src = "//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js" ; scr . charset = "utf-8" ; scr . onload = ( ) => { $ = jQuery ; resolve ( ) ; } ; document . body . appendChild ( scr ) ; } ) ) ; } const exp = promise . then ( start ) ; export default exp ; async function loadKifuForJS ( ) { return await import ( "./index" ) ; }