RPS Hide Read Posts

Hides posts on the RPS homepage when they're clicked & adds a "Mark all as read" button

当前为 2021-01-26 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        RPS Hide Read Posts
// @description Hides posts on the RPS homepage when they're clicked & adds a "Mark all as read" button
// @version     4
// @namespace   https://github.com/insin/greasemonkey
// @grant       none
// @match       https://www.rockpapershotgun.com/
// ==/UserScript==

// @ts-ignore
let debug = false

let $style = document.createElement('style')
$style.innerText = `
/* Prevent the top section taking up space when posts are removed */
#content_above {
  min-height: unset !important;
}
`
document.head.appendChild($style)

/**
 * @param {HTMLElement} section
 * @returns {boolean}
 */
function areAllPostsHidden(section) {
  return Array.from(section.querySelectorAll('article.summary')).every(
    (article) => article.parentElement.style.display === 'none'
  )
}

/**
 * @param {HTMLElement} target
 * @returns {string}
 */
function getPostClickHref(target) {
  if (target.tagName === 'A' && target.closest('article.summary')) {
    return /** @type {HTMLAnchorElement} */ (target).pathname
  }
  return ''
}

function postsPage() {
  /** @type {string[]} */
  let clickedPosts = JSON.parse(localStorage.getItem('clickedPosts') || '[]')

  function hideEmptySections() {
    for (let section of /** @type {NodeListOf<HTMLElement>} */ (document.querySelectorAll(
      '#content_above, section.featured_tag_shelf, section.latest_shelf, section.shelf, section.supporters_shelf'
    ))) {
      if (section.style.display === 'none') continue
      if (areAllPostsHidden(section)) {
        section.style.display = 'none'
      }
    }
  }

  function hideClickedPosts() {
    for (let link of /** @type {NodeListOf<HTMLAnchorElement>} */ (document.querySelectorAll(
      'a.link_overlay'
    ))) {
      if (clickedPosts.includes(link.pathname)) {
        let nodeToHide = link.closest('li')
        if (nodeToHide && nodeToHide.style.display !== 'none') {
          nodeToHide.style.display = 'none'
        }
      }
    }
    hideEmptySections()
  }

  function markAllAsRead(e) {
    e.preventDefault()
    for (let link of /** @type {NodeListOf<HTMLAnchorElement>} */ (document.querySelectorAll(
      'a.link_overlay'
    ))) {
      if (!clickedPosts.includes(link.pathname)) {
        clickedPosts.unshift(link.pathname)
      }
    }
    localStorage.setItem('clickedPosts', JSON.stringify(clickedPosts))
    hideClickedPosts()
  }

  document.addEventListener('click', (e) => {
    let target = /** @type {HTMLElement} */ (e.target)
    if (e.button === 0 && getPostClickHref(target)) {
      // Hold ctrl + shift when clicking a post to hide it without opening it
      if (debug || (e.shiftKey && e.ctrlKey)) e.preventDefault()
      clickedPosts.unshift(getPostClickHref(target))
      localStorage.setItem('clickedPosts', JSON.stringify(clickedPosts))
      hideClickedPosts()
    }
  })

  document.addEventListener('auxclick', (e) => {
    let target = /** @type {HTMLElement} */ (e.target)
    if (e.button === 1 && getPostClickHref(target)) {
      // Hold ctrl when middle-clicking a post to hide it without opening it
      if (debug || e.ctrlKey) e.preventDefault()
      clickedPosts.unshift(getPostClickHref(target))
      localStorage.setItem('clickedPosts', JSON.stringify(clickedPosts))
      hideClickedPosts()
    }
  })

  let topButtons = document.querySelector('div.commercial.button_group')
  if (topButtons != null) {
    let button = document.createElement('a')
    button.className = 'button supporter'
    button.innerText = 'Mark all as read'
    button.href = '#'
    button.addEventListener('click', markAllAsRead)
    topButtons.appendChild(button)
  }

  hideClickedPosts()
}

if (location.pathname === '/') {
  postsPage()
}