CSDN|简书优化

支持手机端和PC端

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

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