csdn页面优化脚本

简述

篡改猴是一款功能强大的浏览器扩展功能,可以在网站上运行用户脚本,从而提升您的浏览体验。它适用于 ChromeMicrosoft EdgeSafariOpera NextFirefox

用户脚本是一些小型程序,可以修改页面布局、添加或删除功能以及自动执行操作,个性化您的网络体验。

脚本主要功能

  • 移除顶部toolbar
  • 移除左侧侧边栏所有内容,仅保留个人资料
  • 移除右侧侧边栏所有内容,仅保留文章目录
  • 移除复制限制,免登录复制(js事件覆盖)
  • 移除文章页登录弹窗

安装使用

一:安装浏览器插件

二:添加新脚本

以 Chrome 为例:

  1. 打开浏览器插件管理页面,找到【篡改猴】插件并打开插件功能列表。

扩展程序

  1. 点击【添加新脚本】选项卡。

添加新脚本

  1. 添加以下代码并保存:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// @match        https://*.blog.csdn.net/*
// @run-at document-start
// @grant GM_addStyle

(function() {
'use strict';
GM_addStyle(`
/* 登录弹窗隐藏 */
.passport-login-container,
/* 登录提示弹窗隐藏 */
.passport-login-tip-container
{
display: none !important;
}
/* 覆盖code不可复制样式 */
#content_views pre,#content_views pre code {
user-select: auto !important;
}
/* 移除关注博主即可阅读全文蒙层 */
#article_content {
height: auto !important;
}
/* 移除code隐藏 */
div.blog-content-box pre {
max-height: initial !important;
}
.hide-article-box.hide-article-pos {
display: none !important;
}
`);

// 需要移除的id类节点
const removeIdList = [
"toolbarBox",
"swiper-remuneration-container",
"recommendAdBox",
"his-selection",
"asideHotArticle",
"footerRightAds",
"blogAiChat"
];
// 需要移除的class类节点
const removeClassList = [
"js-aside-cknows",
"rightside-fixed-hide","kind_person",
"starmap-box"
];
// 定义要阻止的脚本 URL(支持部分匹配)
const removeScriptList = [
// 移除限制复制js
'copyright.js',
// 移除用户行为分析js
'https://www.clarity.ms/tag'
];

const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
mutation.addedNodes.forEach((node) => {
if (node.tagName === "SCRIPT") {
removeScriptList.forEach((scriptItem) => {
if (
(node.src && node.src.indexOf(scriptItem) !== -1) ||
node.innerText.indexOf(scriptItem) !== -1
) {
node.remove();
}
});
} else if (node.nodeType === 1) {
// 收集所有需要删除的元素
const toRemove = [];

// 1. 检查当前节点
if (removeIdList.includes(node.id)) {
toRemove.push(node);
}

// 检查当前节点的类名
removeClassList.forEach((className) => {
if (node.classList?.contains(className)) {
toRemove.push(node);
}
});

// 2. 检查后代节点 - 批量查询优化
const selectors = [
...removeIdList.map((id) => `#${id}`),
...removeClassList.map((className) => `.${className}`),
];

if (selectors.length) {
// 使用逗号连接所有选择器,一次查询搞定
const descendants = node.querySelectorAll(selectors.join(","));
descendants.forEach((el) => toRemove.push(el));
}

// 3. 批量删除
toRemove.forEach((el) => {
// 确保元素还在DOM中
if (el.isConnected) {
el.remove();
}
});
toRemove.length = 0;
}
});
});
});

observer.observe(document, { childList: true, subtree: true });

window.onload = () => {
const hljsButtonList = document.querySelectorAll(".hljs-button");
Array.from(hljsButtonList).forEach((buttonItem) => {
// 移除默认的onClick事件
buttonItem.removeAttribute("onclick");
buttonItem.setAttribute("data-title", "免登录复制");
// 新增点击事件,复制对应code
buttonItem.addEventListener("click", (e) => {
const codeText =
e.currentTarget.parentElement.previousElementSibling.textContent;
// 使用系统复制版复制文本
navigator.clipboard.writeText(codeText);
buttonItem.setAttribute("data-title", "复制成功");
setTimeout(() => {
buttonItem.setAttribute("data-title", "免登录复制");
}, 3000);
});
});
}
// Your code here...
})();

添加完成之后选择【文件】【保存】新增脚本文件。

保存脚本

文件保存后重新刷新浏览器页面即可生效。

效果图展示

样式对应关系

样式类 页面展示 说明
.passport-login-container
.passport-login-tip-container
#toolbarBox
#swiper-remuneration-container
.cknows-side-card.js-aside-cknows
#recommendAdBox
#his-selection
#asideHotArticle
.rightside-fixed-hide
#footerRightAds
#blogAiChat
.starmap-box

csdn页面优化脚本
https://www.my-web.cn/tampermonkey/csdn/
发布于
2026年5月15日
许可协议