new Promise(function(){
 "use strict";

const
  _O={all:'カテゴリ合算'
     ,g_creation:'創作',original:'オリジナル',portrait:'似顔絵'
     ,g_fanart:'ファンアート',anime:'アニメ',game:'ゲーム',character:'キャラクター'
     ,g_popular:'殿堂入り',toho:'東方',vocaloid:'VOCALOID',kancolle:'艦これ'
     ,r15:'R-15'}

  /* ↓設定↓ */
  //表示カテゴリー
  //,_category=['original','anime','game','character','toho','kancolle']
  ,_category=['g_creation','g_fanart','g_popular']
  //表示数
  ,_rankNumber=10
  /* ↑設定↑ */

  ,$XHR=(url,p)=>{
    return new Promise(function(resolve, reject){
      var httpObj=new XMLHttpRequest();
      httpObj.open('GET',url,true);
      httpObj.onload=()=>{resolve([httpObj,p]);};
      httpObj.onerror=()=>{reject(httpObj.error);};
      httpObj.send();
    });
  }
  ,$Func=(R)=>{
    var H=R[1],R=R[0].responseXML.getElementsByTagName('image_list')[0].children,E=[];
    for(let i=_rankNumber;i--;){
      let R2=R[i]
        ,id=R2.getElementsByTagName('id')[0].textContent
        ,title=(R2.getElementsByTagName('title')[0].textContent).replace(/^\d+位 /,'')
        ,name=R2.getElementsByTagName('nickname')[0].textContent;
      E.unshift('<li class="item">'+
        '<p class="num rankingNumber'+(i+1)+'">'+(i+1)+'</p>'+
        '<div class="itemThumb">'+
          '<a href="http://seiga.nicovideo.jp/seiga/im'+id+'">'+
            '<img title="'+title+'" src="http://lohas.nicoseiga.jp/thumb/'+id+'q?" />'+
          '</a>'+
        '</div>'+
        '<div class="itemContent">'+
          '<p class="itemTitle"><a title="'+title+'" href="http://seiga.nicovideo.jp/seiga/im'+id+'" onMouseOver="return popThumb2(\'seiga/im'+id+'\', 0)" onMouseOut="return nd()">'+title+'</a></p>'+
          '<p class="contributor">投稿者:'+name+'</p>'+
        '</div>'+
      '<li>');
    }
    return [H,'<div id="nlSeiga_'+H+'" class="nlSeiga"><h2 class="categorylTitle"><a href="http://seiga.nicovideo.jp/illust/ranking/point/hourly/'+H+'" class="arrow">'+_O[H]+'</a></h2><ol class="list">'+E.join('')+'</ol></div>'];
  }
  ,$F=()=>{
    if(!document.getElementsByClassName('seiga').length)return setTimeout($F,100);
    const
      _seiga=document.getElementsByClassName('seiga')[0]
      ,_elem=document.getElementsByClassName('column sub')[0]
      ,_host=location.host;
    Promise.all([for(C of _category)$XHR('http://'+_host+'/pr_Redirect_ext.seiga.nicovideo.jp/api/illust/blogparts?mode=ranking&key=hourly%2c'+C,C).then($Func)])
      .then((value)=>{
        value=new Map(value);
        _seiga.insertAdjacentHTML('AfterBegin',[for(C of _category)value.get(C)].join(''));
      }).then(()=>{_elem.insertBefore(_seiga.parentNode,_elem.firstElementChild);});
  }

new Promise($F);
});
