CSDN 文章页绿化

(自用脚本) 绿化 CSDN 文章页面、支持手机端 CSDN 文章页绿化

当前为 2023-04-22 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @author       Lete114
// @version      0.2
// @license      MIT License
// @name         CSDN 文章页绿化
// @description  (自用脚本) 绿化 CSDN 文章页面、支持手机端 CSDN 文章页绿化
// @match        https://blog.csdn.net/*/article/details/*
// @icon         https://g.csdnimg.cn/static/logo/favicon32.ico
// @namespace    https://github.com/Lete114/my-tampermonkey-scripts
// @grant        GM_addStyle
// ==/UserScript==

;(function () {
  'use strict'

  const $ = (Selector, el) => (el || document).querySelector(Selector)
  const $$ = (Selector, el) => (el || document).querySelectorAll(Selector)
  const nullSpan = document.createElement('span')

  GM_addStyle(`
    /* 过滤文章页面的垃圾信息 */
    #asideHotArticle,
    #asideCategory,
    #asideNewComments,
    #asideNewNps,
    #asideArchive,
    #recommendNps,
    #treeSkill,
    .programmer1Box,
    .hide-article-box,
    .weixin-shadowbox,
    span[data-href^='csdnapp://'],
    #operate, .readall_box,
    .btn_open_app_prompt_div { 
        display: none !important
    }
    /* 解除文章内容可读限制 */
    .article_content {
        height: unset !important
    }
    /* 解除代码块无法自由选中 */
    #content_views pre,
    #content_views pre>code { 
        -webkit-touch-callout: auto !important;
        -webkit-user-select: auto !important;
        -khtml-user-select: auto !important;
        -moz-user-select: auto !important;
        -ms-user-select: auto !important;
        user-select: auto !important;
    }
    `)

  // 优化文章侧边栏
  try {
    const aside = $('.blog_container_aside')
    if (aside) {
      // 重排侧边栏顺序
      aside.appendChild($('#asideSearchArticle') || nullSpan)
      aside.appendChild($('#asideProfile') || nullSpan)
      aside.appendChild($('#asidedirectory') || nullSpan)
      aside.appendChild($('#asideArchive') || nullSpan)
    }
  } catch (error) {
    console.log('优化文章侧边栏失败', error)
  }

  // 关闭登录弹窗
  try {
    const style = document.createElement('style')
    style.textContent = '.passport-login-container{display:none}'
    document.head.appendChild(style)
    const timer = setInterval(() => {
      const closeLogin = $('#passportbox>span')
      if (closeLogin) {
        clearInterval(timer)
        closeLogin.click()
        style.remove()
      }
    }, 500)
  } catch (error) {
    console.log('关闭登录弹窗失败', error)
  }

  // 免登录复制
  try {
    $$('pre').forEach((el) => {
      const code = $('code', el)
      code.setAttribute('onclick', 'mdcp.copyCode(event)')

      const attrKey = 'data-title'
      const attrValue = '免登录复制'
      // 修改复制按钮文字
      const observer = new MutationObserver((mutationList) => {
        mutationList.forEach((mutation) => {
          const target = mutation.target
          if (
            mutation.type === 'attributes' &&
            target.getAttribute(attrKey) === '登录后复制'
          )
            target.setAttribute(attrKey, attrValue)
        })
      })
      const btn = $('.hljs-button', el)
      btn.setAttribute(attrKey, attrValue)
      observer.observe(btn, { attributes: true })
    })
  } catch (error) {
    console.log('免登录复制失败', error)
  }

  // 链接跳转(避免打开 link.csdn.net)
  try {
    const content_views = $('#content_views')
    const clone = content_views.cloneNode()
    while (content_views.firstChild) clone.appendChild(content_views.firstChild)
    content_views.parentNode.replaceChild(clone, content_views)
    $$('#content_views a[href]').forEach((el) => {
      el.setAttribute('target', '_blank')
    })
  } catch (error) {
    console.log('链接跳转(避免打开 link.csdn.net)失败', error)
  }
})()