Rllmuk Topic Ignore List (Invision 4)

Ignore topics

当前为 2018-02-09 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name        Rllmuk Topic Ignore List (Invision 4)
// @description Ignore topics
// @namespace   https://github.com/insin/greasemonkey/
// @match       https://www.rllmukforum.com/index.php?/discover/unread*
// @version     1
// ==/UserScript==

const TOPIC_LINK_ID_RE = /index\.php\?\/topic\/(\d+)/

let ignoredTopics = localStorage.til_ignoredTopics ? JSON.parse(localStorage.til_ignoredTopics) : []
let ignoredTopicIds = ignoredTopics.map(topic => topic.id)

function processTopic($topic) {
  let $topicLink = $topic.querySelector('a[href*="index.php?/topic/"][data-linktype="link"]')
  let id = TOPIC_LINK_ID_RE.exec($topicLink.href)[1]
  let title = $topicLink.innerText
  if (ignoredTopicIds.includes(id)) {
    $topic.style.display = 'none'
  }
  else {
    let $topicStats = $topic.querySelector('ul.ipsStreamItem_stats')
    $topicStats.insertAdjacentHTML('afterbegin', `
      <li><a style="cursor: pointer"><i class="fa fa-trash"></i></a></li>
    `)
    $topicStats.querySelector('i.fa-trash').addEventListener('click', () => {
      if (!confirm(`Are you sure you want to ignore "${title}"?`)) return
      ignoredTopics.unshift({id, title})
      localStorage.til_ignoredTopics = JSON.stringify(ignoredTopics)
      $topic.style.display = 'none'
    })
  }
}

function filterTopics($el) {
  // Hide or add ignore controls to topics
  Array.from($el.querySelectorAll(':scope > li.ipsStreamItem'), processTopic)

  // Watch for a new topic container being added
  new MutationObserver((mutations) => {
    mutations.forEach((mutation) => {
      if (mutation.addedNodes[0].tagName === 'DIV') {
        filterTopics(mutation.addedNodes[0])
      }
    })
  }).observe($el, {childList: true})
}

filterTopics(document.querySelector('ol.ipsStream'))