CSDN|简书优化

支持手机端和PC端

当前为 2023-01-10 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name CSDN|简书优化
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.4.8
  5. // @description 支持手机端和PC端
  6. // @author MT-戒酒的李白染
  7. // @include http*://www.csdn.net/*
  8. // @include http*://bbs.csdn.net/*
  9. // @include http*://www.jianshu.com/*
  10. // @include http*://*blog.csdn.net/*
  11. // @include http*://download.csdn.net/*
  12. // @include http*://huaweicloud.csdn.net/*
  13. // @grant GM_addStyle
  14. // @grant GM_registerMenuCommand
  15. // @grant GM_unregisterMenuCommand
  16. // @grant GM_getValue
  17. // @grant GM_setValue
  18. // @grant GM_deleteValue
  19. // @grant GM_listValues
  20. // @grant unsafeWindow
  21. // @run-at document-start
  22. // @require https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/jquery/3.4.1/jquery.min.js
  23. // @require https://greasyfork.org/scripts/455186-whitesevsutils/code/WhiteSevsUtils.js?version=1135447
  24. // @require https://greasyfork.org/scripts/449471-viewer/code/Viewer.js?version=1081056
  25. // ==/UserScript==
  26.  
  27. (function () {
  28. "use strict";
  29. var GM_Menu = {
  30. data: {
  31. removeCSDNDownloadPC: {
  32. text: "电脑-移除文章底部的CSDN下载",
  33. enable: false,
  34. },
  35. articleCenter: {
  36. text: "电脑-全文居中",
  37. enable: true,
  38. },
  39. showDirect: {
  40. text: "手机-标识处理过的底部推荐文章",
  41. enable: true,
  42. },
  43. openNewTab: {
  44. text: "手机-底部推荐文章新标签页打开",
  45. enable: true,
  46. },
  47. removeCSDNDownloadMobile: {
  48. text: "手机-移除文章底部的CSDN下载",
  49. enable: false,
  50. },
  51. },
  52. init: function () {
  53. /* 初始化数据 */
  54. let _this_ = this;
  55. Object.keys(this.data).forEach((key) => {
  56. let value = GM_getValue(key);
  57. if (value == null) {
  58. GM_setValue(key, _this_.data[key].enable);
  59. value = GM_getValue(key);
  60. }
  61. _this_.data[key]["enable"] = value;
  62. });
  63. },
  64. register: function () {
  65. /* 注册油猴菜单 */
  66. let _this_ = this;
  67. Object.keys(this.data).forEach((key) => {
  68. let text = _this_.data[key]["text"];
  69. let enable = _this_.data[key]["enable"];
  70. let showText = "[" + (enable ? "√" : "×") + "]" + text;
  71. GM_registerMenuCommand(showText, function () {
  72. GM_setValue(key, enable ? false : true);
  73. window.location.reload();
  74. });
  75. });
  76. },
  77. };
  78. function waitForElementToRemove(_query_ = "") {
  79. /* 移除元素(未出现也可以等待出现) */
  80. Utils.waitForDOM(_query_).then((dom) => {
  81. dom.forEach((item) => {
  82. $(item).remove();
  83. });
  84. });
  85. }
  86. function JianShu() {
  87. /* 简书 */
  88. function isJianShu() {
  89. /* 判断是否是 简书 */
  90. return Boolean(/jianshu.com/i.test(window.location.origin));
  91. }
  92. function PC() {
  93. console.log("简书");
  94. const css = `
  95. .download-app-guidance,
  96. .call-app-btn,
  97. .collapse-tips,
  98. .note-graceful-button,
  99. .app-open,
  100. .header-wrap,
  101. .recommend-wrap.recommend-ad,
  102. .call-app-Ad-bottom,
  103. #recommended-notes p.top-title span.more,
  104. #homepage .modal,
  105. button.index_call-app-btn,
  106. span.note__flow__download,
  107. .download-guide,
  108. #footer,
  109. .comment-open-app-btn-wrap,
  110. .nav.navbar-nav + div{
  111. display:none !important;
  112. }
  113. body.reader-day-mode.normal-size {
  114. overflow: auto !important;
  115. }
  116. .collapse-free-content{
  117. height:auto !important;
  118. }
  119. .copyright{
  120. color:#000 !important;
  121. }
  122. #note-show .content .show-content-free .collapse-free-content:after{
  123. background-image:none !important;
  124. }
  125. `;
  126. GM_addStyle(css);
  127. Utils.waitForDOM('div#homepage div[class*="dialog-"]').then((dom) => {
  128. if (dom.length) {
  129. dom[0].style["visibility"] = "hidden";
  130. }
  131. });
  132. Utils.mutationObserver('div#homepage div[class*="dialog-"]', {
  133. fn: (mutations) => {
  134. if (mutations.length == 0) {
  135. return;
  136. }
  137. if (mutations[0].target.style["display"] != "none") {
  138. document
  139. .querySelector('div#homepage div[class*="dialog-"] .cancel')
  140. ?.click();
  141. }
  142. },
  143. config: {
  144. /* 子节点的变动(新增、删除或者更改) */
  145. childList: false,
  146. /* 属性的变动 */
  147. attributes: true,
  148. /* 节点内容或节点文本的变动 */
  149. characterData: true,
  150. /* 是否将观察器应用于该节点的所有后代节点 */
  151. subtree: true,
  152. },
  153. });
  154. }
  155. if (isJianShu()) {
  156. PC();
  157. }
  158. }
  159. function CSDN() {
  160. /* csdn-移动端 */
  161. function isCSDN() {
  162. /* 判断是否是 CSDN */
  163. return Boolean(/csdn.net/i.test(window.location.origin));
  164. }
  165. function Mobile() {
  166. /* 移动端 */
  167. console.log("CSDN-移动端");
  168. const css = `
  169. #mainBox{
  170. width: auto;
  171. }
  172. .user-desc.user-desc-fix{
  173. height: auto !important;
  174. overflow: auto !important;
  175. }
  176. #operate,.feed-Sign-span,
  177. .view_comment_box,
  178. .weixin-shadowbox.wap-shadowbox,
  179. .feed-Sign-span,
  180. .user-desc.user-desc-fix,
  181. .comment_read_more_box,
  182. #content_views pre.set-code-hide .hide-preCode-box,
  183. .passport-login-container,
  184. .hljs-button[data-title='登录后复制'],
  185. .article-show-more,
  186. #treeSkill,
  187. div.btn_open_app_prompt_div,
  188. div.readall_box,
  189. div.aside-header-fixed{
  190. display:none !important;
  191. }
  192. .GM-csdn-dl{
  193. padding: .24rem .32rem;
  194. width: 100%;
  195. justify-content: space-between;
  196. -webkit-box-pack: justify;
  197. border-bottom: 1px solid #F5F6F7!important;
  198. }
  199. .GM-csdn-title{
  200. font-size: .3rem;
  201. color: #222226;
  202. letter-spacing: 0;
  203. line-height: .44rem;
  204. font-weight: 600;
  205. //max-height: .88rem;
  206. word-break: break-all;
  207. overflow: hidden;
  208. display: -webkit-box;
  209. -webkit-box-orient: vertical;
  210. -webkit-line-clamp: 2
  211. }
  212. .GM-csdn-title a{
  213. word-break: break-all;
  214. color: #222226;
  215. font-weight: 600;
  216. }
  217. .GM-csdn-title em,.GM-csdn-content em{
  218. font-style: normal;
  219. color: #fc5531
  220. }
  221. .GM-csdn-content{
  222. //max-width: 5.58rem;
  223. overflow: hidden;
  224. text-overflow: ellipsis;
  225. display: -webkit-box;
  226. -webkit-line-clamp: 1;
  227. -webkit-box-orient: vertical;
  228. color: #555666;
  229. font-size: .24rem;
  230. line-height: .34rem;
  231. max-height: .34rem;
  232. word-break: break-all;
  233. -webkit-box-flex: 1;
  234. -ms-flex: 1;
  235. flex: 1;
  236. margin-top: .16rem;
  237. }
  238. .GM-csdn-img img{
  239. width: 2.18rem;
  240. height: 1.58rem;
  241. //margin-left: .16rem
  242. }
  243. .GM-csdn-Redirect{
  244. color: #fff;
  245. background-color: #f90707;
  246. font-family: sans-serif;
  247. margin: auto 2px;
  248. border: 1px solid #ccc;
  249. border-radius: 4px;
  250. padding: 0px 3px;
  251. font-size: xx-small;
  252. display: inline;
  253. white-space: nowrap;
  254. }
  255. .component-box .praise {
  256. background: #ff5722;
  257. border-radius: 5px;
  258. padding: 0px 8px;
  259. height: auto;
  260. }
  261. .component-box .praise,.component-box .share {
  262. color: #fff;
  263. }
  264. .component-box a {
  265. display: inline-block;
  266. font-size:xx-small;
  267. }
  268. .component-box {
  269. display: inline;
  270. margin: 0;
  271. position: relative;
  272. white-space:nowrap;
  273. }
  274. .csdn-edu-title{
  275. background: #4d6de1;
  276. border-radius: 5px;
  277. padding: 0px 8px;
  278. height: auto;
  279. color: #fff !important;
  280. }
  281. #comment{
  282. max-height: none !important;
  283. }
  284. #content_views pre,
  285. #content_views pre code{
  286. webkit-touch-callout: text !important;
  287. -webkit-user-select: text !important;
  288. -khtml-user-select: text !important;
  289. -moz-user-select: text !important;
  290. -ms-user-select: text !important;
  291. user-select: text !important;
  292. }
  293. #content_views pre.set-code-hide,
  294. .article_content{
  295. height: 100% !important;
  296. overflow: auto !important;
  297. }
  298. `;
  299. GM_addStyle(css);
  300. function refactoringRecommendation() {
  301. /* 重构底部推荐 */
  302. function refactoring() {
  303. /* 反复执行的重构函数 */
  304. $(".container-fluid").each((index, item) => {
  305. item = $(item);
  306. var url = ""; /* 链接 */
  307. var title = ""; /* 标题 */
  308. var content = ""; /* 内容 */
  309. var img = ""; /* 图片 */
  310. var isCSDNDownload = false; /* 判断是否是CSDN资源下载 */
  311. var isCSDNEduDownload = false; /* 判断是否是CSDN-学院资源下载 */
  312. if (item.attr("data-url")) {
  313. /* 存在真正的URL */
  314. url = item.attr("data-url");
  315. title = item.find(".recommend_title div.left").html();
  316. content = item.find(".text").html();
  317. if (item.find(".recommend-img").length) {
  318. /* 如果有图片就加进去 */
  319. item.find(".recommend-img").each((_index_, _item_) => {
  320. img += $(_item_).html();
  321. });
  322. }
  323. } else {
  324. console.log("节点上无data-url");
  325. url = item.find("a[data-type]").attr("href");
  326. title = item.find(".recommend_title div.left").html();
  327. content = item.find(".text").html();
  328. }
  329. if (GM_Menu.data.showDirect.enable) {
  330. /* 开启就添加 */
  331. title += `<div class="GM-csdn-Redirect">Redirect</div>`;
  332. }
  333. var _URL_ = new URL(url);
  334. if (
  335. _URL_.origin.match(
  336. /download.csdn.net/gi ||
  337. _URL_.pathname.match(/www.iteye.com\/resource/gi)
  338. )
  339. ) {
  340. /* 该链接为csdn资源下载 */
  341. console.log("该链接为csdn资源下载");
  342. isCSDNDownload = true;
  343. title += `<div class="component-box"><a class="praise" href="javascript:;">CSDN下载</a></div>`;
  344. } else if (_URL_.origin.match(/edu.csdn.net/gi)) {
  345. /* 该链接为csdn学院下载 */
  346. isCSDNEduDownload = true;
  347. console.log("该链接为csdn学院下载");
  348. title += `<div class="component-box"><a class="csdn-edu-title" href="javascript:;">CSDN学院</a></div>`;
  349. }
  350. item.attr("class", "GM-csdn-dl");
  351. item.attr("data-url", url);
  352. item.html(
  353. `<div class="GM-csdn-title"><div class="left">${title}</div></div><div class="GM-csdn-content">${content}</div><div class="GM-csdn-img">${img}</div>`
  354. );
  355. if (
  356. (isCSDNDownload || isCSDNEduDownload) &&
  357. GM_Menu.data.removeCSDNDownloadMobile.enable
  358. ) {
  359. item.remove();
  360. }
  361. /* $("#recommend")
  362. .find(".recommend_list")
  363. .before($("#first_recommend_list").find("dl").parent().html()); */
  364. });
  365. }
  366.  
  367. Utils.mutationObserver("#recommend", {
  368. fn: () => {
  369. setTimeout(() => {
  370. refactoring();
  371. }, 300);
  372. },
  373. config: { childList: true, subtree: true, attributes: true },
  374. });
  375.  
  376. gmRecommendClickEvent();
  377. }
  378.  
  379. function gmRecommendClickEvent() {
  380. /* 底部推荐点击跳转事件 */
  381. $("body").on("click", ".GM-csdn-dl", function () {
  382. let url = $(this).attr("data-url");
  383. if (GM_Menu.data.openNewTab.enable) {
  384. window.open(url, "_blank");
  385. } else {
  386. window.location.href = url;
  387. }
  388. });
  389. }
  390.  
  391. function removeAds() {
  392. /* 去除广告 */
  393. waitForElementToRemove(".passport-login-container");
  394. waitForElementToRemove(".btn_open_app_prompt_box.detail-open-removed");
  395. waitForElementToRemove(".add-firstAd");
  396. }
  397.  
  398. $(document).ready(function () {
  399. removeAds();
  400. refactoringRecommendation();
  401. });
  402. }
  403. function PC() {
  404. /* 桌面端 */
  405. console.log("CSDN-桌面端访问");
  406. const css = `
  407. .ecommend-item-box.recommend-recommend-box,
  408. .login-mark,
  409. .opt-box.text-center,
  410. .leftPop,
  411. #csdn-shop-window,
  412. .toolbar-advert,
  413. .hide-article-box,
  414. .user-desc.user-desc-fix,
  415. .recommend-card-box,
  416. .more-article,
  417. .article-show-more{
  418. display: none !important;
  419. }
  420. .comment-list-box{
  421. max-height: none !important;
  422. }
  423. .blog_container_aside,
  424. #nav{
  425. margin-left: -45px;
  426. }
  427. .recommend-right.align-items-stretch.clearfix,.dl_right_fixed{
  428. margin-left: 45px;
  429. }
  430. #content_views pre code{
  431. user-select: text !important;
  432. }
  433. #article_content,
  434. .user-article.user-article-hide{
  435. height: auto !important;
  436. overflow: auto !important;
  437. }
  438. `;
  439. function removeClipboardHijacking() {
  440. /* 去除剪贴板劫持 */
  441. unsafeWindow.articleType = 0;
  442. unsafeWindow.csdn.copyright.textData = undefined;
  443. unsafeWindow.csdn.copyright.htmlData = undefined;
  444. $(".article-copyright")?.remove();
  445. }
  446. function unBlockCopy() {
  447. /* 取消禁止复制 */
  448. Utils.waitForDOM(".hljs-button.signin").then((dom) => {
  449. if (dom.length) {
  450. $(".hljs-button.signin").attr("data-title", "复制");
  451. $(".hljs-button.signin").on("click", function () {
  452. const copyBtn = $(this);
  453. const copyArea = $(this).parent();
  454. copyBtn.attr("data-title", "复制成功");
  455. const btnParentElement = Utils.findParentDOM(this, (dom) => {
  456. return dom.className == "prettyprint" ? true : false;
  457. });
  458. if (btnParentElement) {
  459. $(btnParentElement).bind({
  460. mouseenter: function (e) {
  461. copyBtn.attr("data-title", "复制");
  462. $(btnParentElement)
  463. .unbind("mouseenter")
  464. .unbind("mouseleave");
  465. },
  466. mouseleave: function (e) {
  467. copyBtn.attr("data-title", "复制");
  468. $(btnParentElement)
  469. .unbind("mouseenter")
  470. .unbind("mouseleave");
  471. },
  472. });
  473. }
  474. Utils.setClip(copyArea.text());
  475. });
  476. }
  477. });
  478. }
  479. function clickPreCodeAutomatically() {
  480. /* 点击代码块自动展开 */
  481. $("pre[data-index]").on("click", function () {
  482. let obj = $(this);
  483. obj.css("height", "auto");
  484. obj.find(".hide-preCode-box")?.remove();
  485. });
  486. }
  487. function restoreComments() {
  488. /* 恢复评论到正确位置 */
  489. /* 第一条评论 */
  490. Utils.waitForDOM(".first-recommend-box").then((dom) => {
  491. $(".recommend-box.insert-baidu-box.recommend-box-style").prepend(
  492. $(dom)
  493. );
  494. });
  495. /* 第二条评论 */
  496. Utils.waitForDOM(".second-recommend-box").then((dom) => {
  497. $(".recommend-box.insert-baidu-box.recommend-box-style").prepend(
  498. $(dom)
  499. );
  500. });
  501. }
  502. function identityCSDNDownload() {
  503. /* 标识CSDN下载的链接 */
  504. $(".recommend-item-box[data-url*='https://download.csdn.net/']").each(
  505. (index, item) => {
  506. if (GM_Menu.data.removeCSDNDownloadPC.enable) {
  507. item.remove();
  508. } else {
  509. $(item).find(".content-box").css("border", "2px solid red");
  510. }
  511. }
  512. );
  513. }
  514.  
  515. function articleCenter() {
  516. /* 全文居中 */
  517. if (!GM_Menu.data.articleCenter.enable) {
  518. return;
  519. }
  520. GM_addStyle(
  521. `aside.blog_container_aside{
  522. display:none !important;
  523. }
  524. #mainBox main{
  525. width: inherit !important;
  526. }
  527. `
  528. );
  529. }
  530. function addGotoRecommandButton() {
  531. /* 添加前往评论的按钮,在返回顶部的下面 */
  532. const btnElement = $(`
  533. <a class="option-box" data-type="gorecommand">
  534. <span class="show-txt" style="display:flex;opacity:100;">前往<br>评论</span>
  535. </a>
  536. `);
  537.  
  538. Utils.waitForDOM(".csdn-side-toolbar").then((dom) => {
  539. $(dom).append(btnElement);
  540. $('.option-box[data-type="gorecommand"]').on("click", function () {
  541. console.log("滚动到评论");
  542. $("html, body").animate(
  543. {
  544. scrollTop:
  545. $("#toolBarBox").offset().top -
  546. $("#csdn-toolbar").height() -
  547. 8,
  548. },
  549. 1000
  550. );
  551. });
  552. });
  553. }
  554. GM_addStyle(css);
  555. articleCenter();
  556. $(document).ready(function () {
  557. removeClipboardHijacking();
  558. unBlockCopy();
  559. identityCSDNDownload();
  560. clickPreCodeAutomatically();
  561. restoreComments();
  562. addGotoRecommandButton();
  563. });
  564. }
  565.  
  566. if (isCSDN()) {
  567. if (Utils.isPhone()) {
  568. Mobile(); /* 移动端 */
  569. } else {
  570. PC(); /* 桌面端 */
  571. }
  572. }
  573. }
  574.  
  575. GM_Menu.init();
  576. GM_Menu.register();
  577.  
  578. JianShu();
  579. CSDN();
  580. })();