var topcontent = [];


function topcontents(){
    var contents_json_url = "https://script.google.com/macros/s/AKfycbx77xP4__a6eEGf4COKo6tQOEKVrtFeEJEQoQ1alFoAZmELAuZJbiB_xABgXCir85lG_g/exec"

    //APIを使って非同期データを取得する
    fetch(contents_json_url)
    .then(response => response.json())
    /*成功した処理*/
    .then(data => {
        //JSONから配列に変換
        topcontent = data;
        console.log(data);
        topchenge(topcontent);
        console.log("データ取得" + topcontent);
    });
    

}


function topchenge(contents){

    var contentslist = contents[0];
    var contentskeys = Object.keys(contentslist);

    for(var i=0; i<contentskeys.length;++i){
        var content = document.getElementById(contentskeys[i]);
        console.log(content);
        if(content){
            if (contentskeys[i] == "topimage" || contentskeys[i] == "topmap"){
                if(contentslist[contentskeys[i]]){
                    if(contentslist[contentskeys[i]].match(/https:\/\/drive.google.com\/file\/d\//) != null){
                        content.src = contentslist[contentskeys[i]].replace(/https:\/\/drive.google.com\/file\/d\//, "http://drive.google.com/uc?export=view&id=").replace(/\/view\?usp=share_link/,"");
                    }else{
                        content.src = contentslist[contentskeys[i]];
                    }
                }else{
                    content.src = "img/errer.jpg";
                }
                
            } else{
                content.innerHTML = contentslist[contentskeys[i]];
            }
        }
        console.log(content+ "終了");
    }
}

topcontents();
