ent = content.replace(/]+?href=["']?([^"']+)["']?[^>]*>([^<]+)<\/a>/gi, '[链接]') // replace url content = content.replace(/
.*?<\/pre>/gi, '[代码]') // replace code
    content = content.replace(/<[^>]+>/g,"") // remove html tag

    if (content.length > 150) {
      content = content.substring(0,150) + '...'
    }
    return content
  }

  const generateHtml = array => {
    let result = ''

    if (array.length) {
      for (let i = 0; i < array.length; i++) {
        result += '
' if (true) { const name = 'src' result += `${array[i].nick}` } result += `
${array[i].content}
${array[i].nick} /
` } } else { result += '没有评论' } let $dom = document.querySelector('#card-newest-comments .aside-list') $dom.innerHTML= result window.lazyLoadInstance && window.lazyLoadInstance.update() window.pjax && window.pjax.refresh($dom) } const getComment = async () => { try { const res = await fetch('https://comment.up-4ever.site/api/comment?type=recent&count=6', { method: 'GET' }) const result = await res.json() const walineArray = result.data.map(e => { return { 'content': changeContent(e.comment), 'avatar': e.avatar, 'nick': e.nick, 'url': e.url + '#' + e.objectId, 'date': e.time || e.insertedAt } }) saveToLocal.set('waline-newest-comments', JSON.stringify(walineArray), 30/(60*24)) generateHtml(walineArray) } catch (err) { console.error(err) const $dom = document.querySelector('#card-newest-comments .aside-list') $dom.textContent= "无法获取评论,请确认相关配置是否正确" } } const newestCommentInit = () => { if (document.querySelector('#card-newest-comments .aside-list')) { const data = saveToLocal.get('waline-newest-comments') if (data) { generateHtml(JSON.parse(data)) } else { getComment() } } } newestCommentInit() document.addEventListener('pjax:complete', newestCommentInit) })