cnblogs首页美化

美化 Cnblogs 首页,包括顶部导航,左侧边栏和右侧边栏,分页选择按钮,文章列表

  1. // ==UserScript==
  2. // @name cnblogs首页美化
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @license MIT
  6. // @icon https://www.cnblogs.com/images/logo.svg?v=2SMrXdIvlZwVoB1akyXm38WIKuTHVqvGD0CweV-B6cY
  7. // @description 美化 Cnblogs 首页,包括顶部导航,左侧边栏和右侧边栏,分页选择按钮,文章列表
  8. // @author xiaoye
  9. // @match https://www.cnblogs.com/*
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. // 包裹 "周边" 链接在 li 元素中
  17. const surroundWithLi = () => {
  18. const navLeft = document.getElementById('nav_left');
  19. if (navLeft) {
  20. navLeft.querySelectorAll('a').forEach(link => {
  21. if (link.textContent.includes('周边') && link.parentElement.tagName !== 'LI') {
  22. const li = document.createElement('li');
  23. navLeft.insertBefore(li, link);
  24. li.appendChild(link);
  25. }
  26. });
  27. }
  28. };
  29.  
  30. // 美化文章列表项
  31. const stylePostItems = () => {
  32. document.querySelectorAll('.post-item').forEach(article => {
  33. // 设置样式
  34. Object.assign(article.style, {
  35. backgroundColor: "#ffffff",
  36. boxShadow: "0px 0px 10px rgba(0, 0, 0, 0.1)",
  37. borderRadius: "8px",
  38. padding: "20px",
  39. marginBottom: "20px",
  40. transition: "transform 0.3s, box-shadow 0.3s, border 0.3s",
  41. cursor: "pointer",
  42. border: "1px solid #ddd"
  43. });
  44.  
  45. // 鼠标移入和移出效果
  46. article.addEventListener('mouseenter', () => {
  47. article.style.boxShadow = "0px 0px 15px rgba(0, 0, 0, 0.2)";
  48. article.style.transform = "scale(1.02)";
  49. article.style.border = "1px solid #007acc";
  50. });
  51.  
  52. article.addEventListener('mouseleave', () => {
  53. article.style.boxShadow = "0px 0px 10px rgba(0, 0, 0, 0.1)";
  54. article.style.transform = "scale(1)";
  55. article.style.border = "1px solid #ddd";
  56. });
  57.  
  58. // 添加点击事件
  59. const titleLink = article.querySelector('a.post-item-title');
  60. if (titleLink) {
  61. const href = titleLink.getAttribute('href');
  62. article.addEventListener('click', (event) => {
  63. if (!event.target.closest('a')) {
  64. window.open(href, '_blank');
  65. }
  66. });
  67. }
  68.  
  69. // 防止其他链接的点击事件冒泡
  70. article.querySelectorAll('a').forEach(link => {
  71. link.addEventListener('click', (event) => {
  72. event.stopPropagation();
  73. });
  74. });
  75. });
  76. };
  77.  
  78. // 美化侧边栏
  79. const styleSidenavItems = () => {
  80. document.querySelectorAll('.sidenav-item a').forEach(item => {
  81. Object.assign(item.style, {
  82. borderRadius: '8px',
  83. padding: '10px',
  84. transition: 'background-color 0.3s, transform 0.3s, border 0.3s',
  85. border: "1px solid #ddd"
  86. });
  87.  
  88. item.addEventListener('mouseenter', () => {
  89. item.style.backgroundColor = '#007acc';
  90. item.style.color = '#ffffff';
  91. item.style.transform = 'scale(1.05)';
  92. item.style.border = "1px solid #007acc";
  93. });
  94.  
  95. item.addEventListener('mouseleave', () => {
  96. item.style.backgroundColor = '';
  97. item.style.color = '';
  98. item.style.transform = 'scale(1)';
  99. item.style.border = "1px solid #ddd";
  100. });
  101. });
  102. };
  103.  
  104.  
  105. // 美化侧边栏
  106. const beautifySidebar = () => {
  107. const sidebarItems = document.querySelectorAll('.sidebar .card, .sidebar .sidebar-bh');
  108. sidebarItems.forEach(function(item) {
  109. item.style.backgroundColor = '#ffffff';
  110. item.style.boxShadow = '0px 0px 10px rgba(0, 0, 0, 0.1)';
  111. item.style.borderRadius = '8px';
  112. item.style.padding = '15px';
  113. item.style.marginBottom = '20px';
  114. item.style.marginLeft = '15px';
  115. item.style.transition = 'transform 0.3s, box-shadow 0.3s';
  116. });
  117.  
  118. // 侧边栏标题样式
  119. const cardTitles = document.querySelectorAll('.card-title a');
  120. cardTitles.forEach(function(title) {
  121. title.style.color = '#007acc';
  122. title.style.textDecoration = 'none';
  123. title.style.fontSize = '16px';
  124. title.style.fontWeight = 'bold';
  125. title.style.transition = 'color 0.3s';
  126. });
  127.  
  128. cardTitles.forEach(function(title) {
  129. title.addEventListener('mouseenter', function() {
  130. title.style.color = '#005f99';
  131. });
  132.  
  133. title.addEventListener('mouseleave', function() {
  134. title.style.color = '#007acc';
  135. });
  136. });
  137.  
  138. // 侧边栏项目列表样式
  139. const itemList = document.querySelectorAll('.item-list li');
  140. itemList.forEach(function(item) {
  141. item.style.borderBottom = '1px solid #ddd';
  142. item.style.padding = ' 0';
  143. });
  144.  
  145. // 侧边栏图片样式
  146. const sidebarImages = document.querySelectorAll('.sidebar-image img');
  147. sidebarImages.forEach(function(image) {
  148. image.style.borderRadius = '8px';
  149. image.style.boxShadow = '0px 0px 10px rgba(0, 0, 0, 0.1)';
  150. image.style.maxWidth = '100%';
  151. image.style.height = 'auto';
  152. });
  153. };
  154.  
  155.  
  156.  
  157. // 修改下拉菜单的a标签宽度
  158. const styleDropdownLinks = () => {
  159. document.querySelectorAll('.dropdown-menu a').forEach(link => {
  160. Object.assign(link.style, {
  161. width: '80px',
  162. display: 'inline-block',
  163. textAlign: 'center'
  164. });
  165. });
  166. };
  167.  
  168.  
  169. // 创建样式元素并插入到页面头部
  170. const applyGlobalStyles = () => {
  171. const styleElement = document.createElement('style');
  172. styleElement.textContent = `
  173. .pager a, .pager span {
  174. display: inline-block;
  175. padding: 8px 12px;
  176. margin: 0 4px;
  177. border-radius: 5px;
  178. text-decoration: none;
  179. color: #007acc;
  180. border: 1px solid #ddd;
  181. background-color: #fff;
  182. transition: all 0.3s ease;
  183. box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.1);
  184. }
  185. .pager a.current, .pager span.current {
  186. background-color: #007acc;
  187. color: #fff;
  188. border-color: #007acc;
  189. }
  190. .pager a:hover {
  191. background-color: #005f99;
  192. color: #fff;
  193. border-color: #005f99;
  194. box-shadow: 0px 3px 6px rgba(0, 0, 0, 0.2);
  195. transform: scale(1.05);
  196. }
  197. .headline ul {
  198. display: flex;
  199. flex-wrap: wrap;
  200. justify-content: space-between;
  201. padding: 0;
  202. list-style: none;
  203. }
  204. .headline li {
  205. width: 48%;
  206. margin-bottom: 15px;
  207. display: flex;
  208. justify-content: space-between;
  209. align-items: center;
  210. }
  211. .headline a {
  212. display: inline-block;
  213. width: calc(100% - 40px);
  214. padding: 10px;
  215. border-radius: 8px;
  216. text-decoration: none;
  217. color: #333;
  218. border: 1px solid #ddd;
  219. background-color: #f9f9f9;
  220. transition: all 0.3s ease;
  221. box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.1);
  222. }
  223. .headline a:hover {
  224. background-color: #f9f9f9;
  225. color: #333;
  226. border-color: #007acc;
  227. box-shadow: 0px 3px 6px rgba(0, 0, 0, 0.2);
  228. transform: scale(1.05);
  229. }
  230. .headline .headline-label {
  231. font-weight: bold;
  232. color: #007acc;
  233. margin-right: 5px;
  234. }
  235. .headline .right_more {
  236. width: 30px;
  237. margin-left: 10px;
  238. padding: 5px 10px;
  239. font-size: 0.9em;
  240. background-color: #f9f9f9;
  241. color: #333;
  242. border: 1px solid #ddd;
  243. border-radius: 5px;
  244. text-decoration: none;
  245. transition: all 0.3s ease;
  246. box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.1);
  247. }
  248. .headline .right_more:hover {
  249. border-color: #007acc;
  250. box-shadow: 0px 3px 6px rgba(0, 0, 0, 0.2);
  251. transform: scale(1.05);
  252. }
  253. #nav_left li a {
  254. color: #333;
  255. text-decoration: none;
  256. padding: 10px 15px;
  257. display: block;
  258. position: relative;
  259. transition: color 0.3s ease;
  260. }
  261. #nav_left li a::after {
  262. content: '';
  263. position: absolute;
  264. left: 0;
  265. bottom: 0;
  266. width: 100%;
  267. height: 2px;
  268. background-color: #007acc;
  269. transform: scaleX(0);
  270. transform-origin: bottom right;
  271. transition: transform 0.3s ease;
  272. }
  273. #nav_left li a:hover {
  274. color: #007acc;
  275. }
  276. #nav_left li a:hover::after {
  277. transform: scaleX(1);
  278. transform-origin: bottom left;
  279. }
  280. `;
  281. document.head.appendChild(styleElement);
  282. };
  283.  
  284. // 初始化所有样式和功能
  285. const init = () => {
  286. surroundWithLi();
  287. stylePostItems();
  288. styleSidenavItems();
  289. styleDropdownLinks();
  290. applyGlobalStyles();
  291. beautifySidebar();
  292. };
  293.  
  294. // 页面初始加载时应用所有样式和功能
  295. init();
  296.  
  297. // 监听DOM变化,确保动态内容的样式保持
  298. const observer = new MutationObserver(init);
  299. observer.observe(document.body, { childList: true, subtree: true });
  300. })();
  301.  
  302.  

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址