new Promise(()=>{
 "use strict";

window.zenzaExt.zenzaExt2 = {
  F1() {
    /* タグの百科タグ入れ替え */
    const
      headerPanel = document.getElementsByClassName('zenzaWatchVideoHeaderPanel')[0];
    
    if (!headerPanel.getElementsByClassName('zenza-tag')[0]) return;
    
    const
      smid = window.zenzaExt.smid
      ,zTags = headerPanel.getElementsByClassName('zenza-tag')
      ,data = window.popThumb.data[`obj_${smid}`]
      ,tags = data? data[1].tags:
        window.popThumb.HttpRequest(
          `/pr_Redirect_ext.nicovideo.jp/api/getthumbinfo/${smid}?nlFilter`
          ,(res, pop_smid, object) => object.tags
          ,false ,smid ,["api", smid]
        );
    
    var tags_ = {};
    tags.forEach(T => tags_[T.text] = T);
    
    const func = aElement => {
      let
        tagLink = aElement.getElementsByClassName('tagLink')[0]
        ,tag = tags_[tagLink.textContent];
      
      aElement.insertBefore(tagLink, aElement.firstElementChild);
      
      if (!tag) return;
      if (tag.lock) aElement.insertAdjacentHTML('AfterBegin', '<div class="zTagLock"></div>');
      if (tag.category)  aElement.insertAdjacentHTML('AfterBegin', '<div class="zTagCategory"></div>');
    };
    Array.from(zTags).forEach(e => Promise.resolve(func(e)));
    
    if (!!document.getElementById('zTagStyleLC')) return;
    document.body.insertAdjacentHTML('BeforeEnd', '<style type="text/css" id="zTagStyleLC">.zTagLock,.zTagCategory{display:inline-block; vertical-align:middle;} .zTagLock {background-image:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAwAAAAPCAYAAADQ4S5JAAABFUlEQVQokZWOMWvCQBiGPwQRVHA/RBxusODo4CLEQWdHx/yOrB2yJYM/wGwuBam7S5d4dEg6OAmGQux0oVYqZgh9O5SkIeegHzzL894DR5Q7wNMBPwDe8If/CXg6XbskEZOf5BXXSBIxUYL4YHnxh4X4YHmXwGxfArOdd0oQiTEiMYZ0R9kXpDvSU68E+xXHfsWxW3Itdbsl11KvBMLhEA6HO/8P3DnXUp89PB3l+vQV4SaOck3n0MQ90Lf/gDwLu4lBr4pBr4qF3URxJ/nCkKdRL4GIQERo1Eso7vT+zJCn361kQb9bQXGnrcNQpNMqo9MqK37rMNBmxlBkOqxhOqwpfjNjoKdHhnsgy2ChbTDcgmWw8Be9jY3GCMTjCgAAAABJRU5ErkJggg==); width:12px; height:15px;} .zTagCategory{background-image:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABUAAAAJCAYAAADdA2d2AAAARklEQVQokWNgYGBgOMPA8J9amIFcA////49CYxiMTTEyHxkQ7VpSDUBXg83VWF2KTOOyiGxD8YUbVQ0l5HIMQwc8WeEzEACuuJ8JQiivJgAAAABJRU5ErkJggg==); width:21px; height:9px; margin-right:2px;}</style>');
  },

  F2() {
    /* キャッシュ消去追加 */
    const
      smid = window.zenzaExt.smid
      ,hoverLinkContainer = document.getElementsByClassName('zenzaWatchVideoHeaderPanel')[0].getElementsByClassName('hoverLinkContainer')[0]
      ,nlCacheLink = hoverLinkContainer.getElementsByClassName('nlCacheLink');
    
    if (!nlCacheLink.length) {
      hoverLinkContainer.insertAdjacentHTML('beforeEnd', `<div class="hoverLink nlCache"><a class="nlCacheLink noHoverMenu" href="/cache/ajax_rmall?${smid}" onclick="window.zenzaExt.zenzaExt2.CacheRemove(this); return false;">キャッシュ消去</a></div>`);
    } else {
      nlCacheLink[0].setAttribute('href', `/cache/ajax_rmall?${smid}`);
      nlCacheLink[0].textContent = 'キャッシュ消去';
    }
  },

  CacheRemove(aElement) {
    window.zenzaExt
      .$XHR(aElement.getAttribute('href'), aElement)
      .then(([aRes, aElem]) => {
        aElem.textContent = `${aElem.textContent} ${aRes.responseText}`;
      })
  },
};

window.zenzaExt.addVideoLoadEventListener([window.zenzaExt.zenzaExt2.F1, window.zenzaExt.zenzaExt2.F2,]);
});