<!-- 一言 -->
<div class="nexmoe-widget-wrap">
  <h3 class="nexmoe-widget-title">
    <%= __('hitokoto') %>
  </h3>
  <div class="nexmoe-widget">
    <ul class="hitokoto-box">
      <li id="hitokoto_text_parent" class="hitokoto-text" hitokotoCategory="<%= options.widgetHitokoto.category %>">
        <a href="#" id="hitokoto_text">
          <%if (!options.widgetHitokoto.loading_placeholder) { %> 🚀 获取中... <% } %>
        </a>
        <a href="#" id="hitokoto_error_text" style="display: none;">
          <%if (!options.widgetHitokoto.loading_error_placeholder) { %> 🐞 获取失败... <% } %>
        </a>
      </li>
    </ul>
  </div>
</div>

<script>
  let hitokotoText = document.getElementById('hitokoto_text')
  let hitokotoErroText = document.getElementById('hitokoto_error_text')
  let hitokotoCategory = document.getElementById('hitokoto_text_parent').getAttribute('hitokotoCategory')
  window.addEventListener('load', function () {
    let url = 'https://v1.hitokoto.cn'
    if (hitokotoCategory) {
      url += '?c=' + hitokotoCategory
    }
    fetch(url)
      .then(response => response.json())
      .then(data => {
        hitokotoText.innerText = "「 " + data.hitokoto + " 」 from " + data.from
        hitokotoText.href = 'https://hitokoto.cn/?uuid=' + data.uuid
      })
      .catch((reason) => {
        console.error(11, reason)
        hitokotoText.style.display = 'none'
        hitokotoErroText.style.display = 'block'
      })
  })
</script>