基于 Gloria 实现多说垃圾评论主动式过滤 v2

由于多说官方升级了API的安全性, 所以原基于 Gloria 实现多说垃圾评论主动式过滤失效, 更新此v2版本.

使用方法与先前一样, 请参考原文.

const DOMAIN = 'xxx.duoshuo.com'

function spamFilter(response) {  
  let ps = response.map(x => {
    if (x.role_name === '游客'
    && ['Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)', 'Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 6.1)'].includes(x.agent)) {
      return (nonce) => fetch(`http://${ DOMAIN }/api/posts/spam.json`, {
        method: 'POST'
      , body: (() => {
          let params = new URLSearchParams()
          params.set('post_id', x.post_id)
          params.set('nonce', nonce)
          return params
        })()
      })
    }
  }).filter(x => !!x)

  if (ps.length > 0) {
    return nonce => Promise.all(ps.map(f => f(nonce)))
  }
}

fetch(`http://${ DOMAIN }/admin/`)  
.then(res => res.text())
.then(html => {
  let [, nonce] = html.match(/window.DUOSHUO.nonce = '([\w\d]+)'/)
  return nonce
})
.then(nonce => Promise.all([
  importScripts('gloria-utils')
, fetch(`http://${ DOMAIN }/api/posts/list.json?related[]=thread&limit=30&nonce=${ nonce }`)
  .then(res => res.json())
, nonce
]))
.then(args => {
  const [, { response }, nonce] = args
  let result = spamFilter(response)
  if (result instanceof Function) {
    return result(nonce).then(() => args).catch(() => args)
  }
  return args
})
.then(([{ underscoreString: { stripTags }}, { response }]) => response
  .filter(x => x.role_name !== '管理员')
  .map(x => ({
    title: x.author.name
  , message: stripTags(x.message).trim()
  , id: x.post_id
  , iconUrl: x.author.avatar_url || 'http://static.duoshuo.com/images/noavatar_default.png'
  , url: x.thread.url
  }))
)
.then(commit)

以上代码由自有站点的多说评论提醒修改而来.