NGA 版头/版规/置顶部分折叠

自动折叠 NGA 版头/版规/置顶部分,需要的时候可以点击版头按钮显示(替换跳转功能)

当前为 2021-05-19 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         NGA 版头/版规/置顶部分折叠
// @namespace    https://greasyfork.org/zh-CN/users/164691-shy07
// @version      0.20
// @description  自动折叠 NGA 版头/版规/置顶部分,需要的时候可以点击版头按钮显示(替换跳转功能)
// @author       Shy07
// @match        *://nga.178.com/*
// @match        *://bbs.ngacn.cc/*
// @match        *://bbs.nga.cn/*
// @grant        none
// jshint esversion:6
// ==/UserScript==

((ui, self) => {
  'use strict'

  if (ui === undefined) return

  const targetNode = document.querySelector('body')
  const config = {
    childList: true
  }
  let manualOpen = false

  const toggle = () => {
    const toppedTopic = document.querySelector('#toppedtopic')
    toppedTopic.style.display = manualOpen ? 'none' : 'block'
    manualOpen = !manualOpen
  }
  const hookClickEvent = () => {
    const el = document.querySelector('#toptopics a[class="block_txt block_txt_c0"]')
    if (el) {
      el.href = 'javascript:;'
      el.addEventListener('click', toggle)
    }
  }

  const hideToppedTopic = () => {
    const toppedTopic = document.querySelector('#toppedtopic')
    if (!manualOpen && toppedTopic) {
      toppedTopic.style.display = 'none'
    }
  }
  hideToppedTopic()
  hookClickEvent()

  // const observer = new MutationObserver((mutationsList, observer) => {
  //   hideToppedTopic()
  //   hookClickEvent()
  // })
  // observer.observe(targetNode, config)

  // 钩子
  const hookFunction = (object, functionName, callback) => {
    ((originalFunction) => {
      object[functionName] = function () {
        const returnValue = originalFunction.apply(this, arguments)
        callback.apply(this, [returnValue, originalFunction, arguments])
        return returnValue
      }
    })(object[functionName])
  }

  let initialized = false

  hookFunction(ui, 'eval', () => {
    if (initialized) return
    if (ui.topicArg) {
      hookFunction(
        ui.topicArg,
        'add',
        (returnValue, originalFunction, args) => {
          hideToppedTopic()
          hookClickEvent()
        }
      )
      initialized = true
    }
  })

})(commonui, __CURRENT_UID)