CSDN|简书优化

支持手机端和PC端

当前为 2023-02-14 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name CSDN|简书优化
  3. // @icon https://www.csdn.net/favicon.ico
  4. // @namespace https://greasyfork.org/zh-CN/scripts/406136-csdn-简书优化
  5. // @supportURL https://greasyfork.org/zh-CN/scripts/406136-csdn-简书优化/feedback
  6. // @version 0.5.7
  7. // @description 支持手机端和PC端
  8. // @author WhiteSevs
  9. // @match http*://*.csdn.net/*
  10. // @match http*://*.jianshu.com/*
  11. // @match http*://*.jianshu.io/*
  12. // @grant GM_addStyle
  13. // @grant GM_registerMenuCommand
  14. // @grant GM_unregisterMenuCommand
  15. // @grant GM_getValue
  16. // @grant GM_setValue
  17. // @grant GM_deleteValue
  18. // @grant GM_listValues
  19. // @grant unsafeWindow
  20. // @run-at document-start
  21. // @require https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/jquery/3.4.1/jquery.min.js
  22. // @require https://greasyfork.org/scripts/449471-viewer/code/Viewer.js?version=1081056
  23. // @require https://greasyfork.org/scripts/455186-whitesevsutils/code/WhiteSevsUtils.js?version=1149608
  24. // ==/UserScript==
  25.  
  26. (function () {
  27. "use strict";
  28. function waitForElementToRemove(_query_ = "") {
  29. /* 移除元素(未出现也可以等待出现) */
  30. Utils.waitNode(_query_, undefined, 200).then((dom) => {
  31. dom.forEach((item) => {
  32. $(item).remove();
  33. });
  34. });
  35. }
  36. function JianShu() {
  37. /* 简书 */
  38. function isJianShu() {
  39. /* 判断是否是 简书 */
  40. return Boolean(/jianshu.(com|io)/i.test(window.location.origin));
  41. }
  42. function articleCenter() {
  43. /* 全文居中 */
  44. const articleCenterCSS = `
  45. div[role=main] aside,
  46. div._3Pnjry{
  47. display: none !important;
  48. }
  49. div._gp-ck{
  50. width: 100% !important;
  51. }
  52. `;
  53. GM_addStyle(articleCenterCSS);
  54. waitForElementToRemove("div[role=main] aside");
  55. waitForElementToRemove("div._3Pnjry");
  56. Utils.waitNode("div._gp-ck").then((dom) => {
  57. dom.forEach((item) => {
  58. item.style["width"] = "100%";
  59. });
  60. });
  61. }
  62. function JianShuremoveFooterRecommendRead() {
  63. /* 手机-移除底部推荐阅读 */
  64. GM_addStyle(`
  65. #recommended-notes{
  66. display: none !important;
  67. }
  68. `);
  69. }
  70. function PC() {
  71. console.log("简书");
  72. if (GM_Menu.get("JianShuArticleCenter")) {
  73. articleCenter();
  74. }
  75. if (GM_Menu.get("JianShuremoveFooterRecommendRead")) {
  76. JianShuremoveFooterRecommendRead();
  77. }
  78. const css = `
  79. .download-app-guidance,
  80. .call-app-btn,
  81. .collapse-tips,
  82. .note-graceful-button,
  83. .app-open,
  84. .header-wrap,
  85. .recommend-wrap.recommend-ad,
  86. .call-app-Ad-bottom,
  87. #recommended-notes p.top-title span.more,
  88. #homepage .modal,
  89. button.index_call-app-btn,
  90. span.note__flow__download,
  91. .download-guide,
  92. #footer,
  93. .comment-open-app-btn-wrap,
  94. .nav.navbar-nav + div,
  95. .self-flow-ad,
  96. #free-reward-panel{
  97. display:none !important;
  98. }
  99. body.reader-day-mode.normal-size {
  100. overflow: auto !important;
  101. }
  102. .collapse-free-content{
  103. height:auto !important;
  104. }
  105. .copyright{
  106. color:#000 !important;
  107. }
  108. #note-show .content .show-content-free .collapse-free-content:after{
  109. background-image:none !important;
  110. }
  111. footer > div > div{
  112. justify-content: center;
  113. }
  114. `;
  115. GM_addStyle(css);
  116. Utils.waitNode('div#homepage div[class*="dialog-"]').then((dom) => {
  117. if (dom.length) {
  118. dom[0].style["visibility"] = "hidden";
  119. }
  120. });
  121. Utils.mutationObserver('div#homepage div[class*="dialog-"]', {
  122. fn: (mutations) => {
  123. if (mutations.length == 0) {
  124. return;
  125. }
  126. if (mutations[0].target.style["display"] != "none") {
  127. document
  128. .querySelector('div#homepage div[class*="dialog-"] .cancel')
  129. ?.click();
  130. }
  131. },
  132. config: {
  133. /* 子节点的变动(新增、删除或者更改) */
  134. childList: false,
  135. /* 属性的变动 */
  136. attributes: true,
  137. /* 节点内容或节点文本的变动 */
  138. characterData: true,
  139. /* 是否将观察器应用于该节点的所有后代节点 */
  140. subtree: true,
  141. },
  142. });
  143. }
  144. if (isJianShu()) {
  145. if (window.location.pathname === "/go-wild") {
  146. /* 禁止简书拦截跳转 */
  147. let search = window.location.href.replace(
  148. window.location.origin + "/",
  149. ""
  150. );
  151. search = decodeURIComponent(search);
  152. let newURL = search
  153. .replace(/^go-wild\?ac=2&url=/gi, "")
  154. .replace(/^https:\/\/link.zhihu.com\/\?target\=/gi, "");
  155. window.location.href = newURL;
  156. }
  157. PC();
  158. }
  159. }
  160. function CSDN() {
  161. /* csdn-移动端 */
  162. function isCSDN() {
  163. /* 判断是否是 CSDN */
  164. return Boolean(/csdn.net/i.test(window.location.origin));
  165. }
  166. function Mobile() {
  167. /* 移动端 */
  168. console.log("CSDN-移动端");
  169. const css = `
  170. #mainBox{
  171. width: auto;
  172. }
  173. .user-desc.user-desc-fix{
  174. height: auto !important;
  175. overflow: auto !important;
  176. }
  177. #operate,.feed-Sign-span,
  178. .view_comment_box,
  179. .weixin-shadowbox.wap-shadowbox,
  180. .feed-Sign-span,
  181. .user-desc.user-desc-fix,
  182. .comment_read_more_box,
  183. #content_views pre.set-code-hide .hide-preCode-box,
  184. .passport-login-container,
  185. .hljs-button[data-title='登录后复制'],
  186. .article-show-more,
  187. #treeSkill,
  188. div.btn_open_app_prompt_div,
  189. div.readall_box,
  190. div.aside-header-fixed{
  191. display:none !important;
  192. }
  193. .GM-csdn-dl{
  194. padding: .24rem .32rem;
  195. width: 100%;
  196. justify-content: space-between;
  197. -webkit-box-pack: justify;
  198. border-bottom: 1px solid #F5F6F7!important;
  199. }
  200. .GM-csdn-title{
  201. font-size: .3rem;
  202. color: #222226;
  203. letter-spacing: 0;
  204. line-height: .44rem;
  205. font-weight: 600;
  206. //max-height: .88rem;
  207. word-break: break-all;
  208. overflow: hidden;
  209. display: -webkit-box;
  210. -webkit-box-orient: vertical;
  211. -webkit-line-clamp: 2
  212. }
  213. .GM-csdn-title a{
  214. word-break: break-all;
  215. color: #222226;
  216. font-weight: 600;
  217. }
  218. .GM-csdn-title em,.GM-csdn-content em{
  219. font-style: normal;
  220. color: #fc5531
  221. }
  222. .GM-csdn-content{
  223. //max-width: 5.58rem;
  224. overflow: hidden;
  225. text-overflow: ellipsis;
  226. display: -webkit-box;
  227. -webkit-line-clamp: 1;
  228. -webkit-box-orient: vertical;
  229. color: #555666;
  230. font-size: .24rem;
  231. line-height: .34rem;
  232. max-height: .34rem;
  233. word-break: break-all;
  234. -webkit-box-flex: 1;
  235. -ms-flex: 1;
  236. flex: 1;
  237. margin-top: .16rem;
  238. }
  239. .GM-csdn-img img{
  240. width: 2.18rem;
  241. height: 1.58rem;
  242. //margin-left: .16rem
  243. }
  244. .GM-csdn-Redirect{
  245. color: #fff;
  246. background-color: #f90707;
  247. font-family: sans-serif;
  248. margin: auto 2px;
  249. border: 1px solid #ccc;
  250. border-radius: 4px;
  251. padding: 0px 3px;
  252. font-size: xx-small;
  253. display: inline;
  254. white-space: nowrap;
  255. }
  256. .component-box .praise {
  257. background: #ff5722;
  258. border-radius: 5px;
  259. padding: 0px 8px;
  260. height: auto;
  261. }
  262. .component-box .praise,.component-box .share {
  263. color: #fff;
  264. }
  265. .component-box a {
  266. display: inline-block;
  267. font-size:xx-small;
  268. }
  269. .component-box {
  270. display: inline;
  271. margin: 0;
  272. position: relative;
  273. white-space:nowrap;
  274. }
  275. .csdn-edu-title{
  276. background: #4d6de1;
  277. border-radius: 5px;
  278. padding: 0px 8px;
  279. height: auto;
  280. color: #fff !important;
  281. }
  282. #comment{
  283. max-height: none !important;
  284. }
  285. #content_views pre,
  286. #content_views pre code{
  287. webkit-touch-callout: text !important;
  288. -webkit-user-select: text !important;
  289. -khtml-user-select: text !important;
  290. -moz-user-select: text !important;
  291. -ms-user-select: text !important;
  292. user-select: text !important;
  293. }
  294. #content_views pre.set-code-hide,
  295. .article_content{
  296. height: 100% !important;
  297. overflow: auto !important;
  298. }
  299. `;
  300. GM_addStyle(css);
  301. function refactoringRecommendation() {
  302. /* 重构底部推荐 */
  303. function refactoring() {
  304. /* 反复执行的重构函数 */
  305. $(".container-fluid").each((index, item) => {
  306. item = $(item);
  307. var url = ""; /* 链接 */
  308. var title = ""; /* 标题 */
  309. var content = ""; /* 内容 */
  310. var img = ""; /* 图片 */
  311. var isCSDNDownload = false; /* 判断是否是CSDN资源下载 */
  312. var isCSDNEduDownload = false; /* 判断是否是CSDN-学院资源下载 */
  313. if (item.attr("data-url")) {
  314. /* 存在真正的URL */
  315. url = item.attr("data-url");
  316. title = item.find(".recommend_title div.left").html();
  317. content = item.find(".text").html();
  318. if (item.find(".recommend-img").length) {
  319. /* 如果有图片就加进去 */
  320. item.find(".recommend-img").each((_index_, _item_) => {
  321. img += $(_item_).html();
  322. });
  323. }
  324. } else {
  325. console.log("节点上无data-url");
  326. url = item.find("a[data-type]").attr("href");
  327. title = item.find(".recommend_title div.left").html();
  328. content = item.find(".text").html();
  329. }
  330. if (GM_Menu.get("showDirect")) {
  331. /* 开启就添加 */
  332. title += `<div class="GM-csdn-Redirect">Redirect</div>`;
  333. }
  334. var _URL_ = new URL(url);
  335. if (
  336. _URL_.host === "download.csdn.net" ||
  337. (_URL_.host === "www.iteye.com" &&
  338. _URL_.pathname.match(/^\/resource/gi))
  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.get("removeCSDNDownloadMobile")
  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.get("openNewTab")) {
  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. #csdn-toolbar-profile-nologin{
  419. display: none !important;
  420. }
  421. .comment-list-box{
  422. max-height: none !important;
  423. }
  424. .blog_container_aside,
  425. #nav{
  426. margin-left: -45px;
  427. }
  428. .recommend-right.align-items-stretch.clearfix,.dl_right_fixed{
  429. margin-left: 45px;
  430. }
  431. #content_views pre,
  432. #content_views pre code{
  433. user-select: text !important;
  434. }
  435. #article_content,
  436. .user-article.user-article-hide{
  437. height: auto !important;
  438. overflow: auto !important;
  439. }
  440. `;
  441. function removeClipboardHijacking() {
  442. /* 去除剪贴板劫持 */
  443. unsafeWindow.articleType = 0;
  444. unsafeWindow.csdn.copyright.textData = "";
  445. unsafeWindow.csdn.copyright.htmlData = "";
  446. $(".article-copyright")?.remove();
  447. }
  448. function unBlockCopy() {
  449. /* 取消禁止复制 */
  450. Utils.waitNode(".hljs-button.signin").then((dom) => {
  451. if (dom.length) {
  452. $(".hljs-button.signin").attr("data-title", "复制");
  453. $(".hljs-button.signin").on("click", function () {
  454. const copyBtn = $(this);
  455. const copyArea = $(this).parent();
  456. copyBtn.attr("data-title", "复制成功");
  457. const btnParentElement = Utils.findParentNode(this, (dom) => {
  458. return dom.className == "prettyprint" ? true : false;
  459. });
  460. if (btnParentElement) {
  461. $(btnParentElement).bind({
  462. mouseenter: function (e) {
  463. copyBtn.attr("data-title", "复制");
  464. $(btnParentElement)
  465. .unbind("mouseenter")
  466. .unbind("mouseleave");
  467. },
  468. mouseleave: function (e) {
  469. copyBtn.attr("data-title", "复制");
  470. $(btnParentElement)
  471. .unbind("mouseenter")
  472. .unbind("mouseleave");
  473. },
  474. });
  475. }
  476. Utils.setClip(copyArea.text());
  477. });
  478. }
  479. });
  480. }
  481. function clickPreCodeAutomatically() {
  482. /* 点击代码块自动展开 */
  483. $("pre[data-index]").on("click", function () {
  484. let obj = $(this);
  485. obj.css("height", "auto");
  486. obj.find(".hide-preCode-box")?.remove();
  487. });
  488. }
  489. function restoreComments() {
  490. /* 恢复评论到正确位置 */
  491. /* 第一条评论 */
  492. Utils.waitNode(".first-recommend-box").then((dom) => {
  493. $(".recommend-box.insert-baidu-box.recommend-box-style").prepend(
  494. $(dom)
  495. );
  496. });
  497. /* 第二条评论 */
  498. Utils.waitNode(".second-recommend-box").then((dom) => {
  499. $(".recommend-box.insert-baidu-box.recommend-box-style").prepend(
  500. $(dom)
  501. );
  502. });
  503. }
  504. function identityCSDNDownload() {
  505. /* 标识CSDN下载的链接 */
  506. $(".recommend-item-box[data-url*='https://download.csdn.net/']").each(
  507. (index, item) => {
  508. if (GM_Menu.get("removeCSDNDownloadPC")) {
  509. item.remove();
  510. } else {
  511. $(item).find(".content-box").css("border", "2px solid red");
  512. }
  513. }
  514. );
  515. }
  516.  
  517. function articleCenter() {
  518. /* 全文居中 */
  519. if (!GM_Menu.get("articleCenter")) {
  520. return;
  521. }
  522. GM_addStyle(
  523. `aside.blog_container_aside{
  524. display:none !important;
  525. }
  526. #mainBox main{
  527. width: inherit !important;
  528. }
  529. `
  530. );
  531. }
  532. function addGotoRecommandButton() {
  533. /* 添加前往评论的按钮,在返回顶部的下面 */
  534. const btnElement = $(`
  535. <a class="option-box" data-type="gorecommand">
  536. <span class="show-txt" style="display:flex;opacity:100;">前往<br>评论</span>
  537. </a>
  538. `);
  539.  
  540. Utils.waitNode(".csdn-side-toolbar").then((dom) => {
  541. $(dom).append(btnElement);
  542. $('.option-box[data-type="gorecommand"]').on("click", function () {
  543. console.log("滚动到评论");
  544. $("html, body").animate(
  545. {
  546. scrollTop:
  547. $("#toolBarBox").offset().top -
  548. $("#csdn-toolbar").height() -
  549. 8,
  550. },
  551. 1000
  552. );
  553. });
  554. });
  555. }
  556. function shieldLoginDialog() {
  557. /* 屏蔽登录弹窗 */
  558. if (GM_Menu.get("shieldLoginDialog")) {
  559. window.GM_CSS_GM_shieldLoginDialog = [
  560. GM_addStyle(`.passport-login-container{display: none !important;}`),
  561. ];
  562. }
  563. }
  564. GM_addStyle(css);
  565. articleCenter();
  566. shieldLoginDialog();
  567. $(document).ready(function () {
  568. removeClipboardHijacking();
  569. unBlockCopy();
  570. identityCSDNDownload();
  571. clickPreCodeAutomatically();
  572. restoreComments();
  573. addGotoRecommandButton();
  574. });
  575. }
  576.  
  577. if (isCSDN()) {
  578. if (window.location.host === "link.csdn.net") {
  579. /* 禁止CSDN拦截跳转 */
  580. let search = window.location.href.replace(
  581. window.location.origin + "/",
  582. ""
  583. );
  584. search = decodeURIComponent(search);
  585. let newURL = search
  586. .replace(/^\?target\=/gi, "")
  587. .replace(/^https:\/\/link.zhihu.com\/\?target\=/gi, "");
  588. window.location.href = newURL;
  589. }
  590. if (Utils.isPhone()) {
  591. Mobile(); /* 移动端 */
  592. } else {
  593. PC(); /* 桌面端 */
  594. }
  595. }
  596. }
  597. if (Boolean(/csdn.net/i.test(window.location.origin))) {
  598. var GM_Menu = new Utils.GM_Menu({
  599. removeCSDNDownloadPC: {
  600. text: "电脑-移除文章底部的CSDN下载",
  601. enable: false,
  602. showText: (_text_, _enable_) => {
  603. return (_enable_ ? "✅" : "❌") + " " + _text_;
  604. },
  605. },
  606. articleCenter: {
  607. text: "电脑-全文居中",
  608. enable: true,
  609. showText: (_text_, _enable_) => {
  610. return (_enable_ ? "✅" : "❌") + " " + _text_;
  611. },
  612. },
  613. shieldLoginDialog: {
  614. text: "电脑-屏蔽登录弹窗",
  615. enable: true,
  616. showText: (_text_, _enable_) => {
  617. return (_enable_ ? "✅" : "❌") + " " + _text_;
  618. },
  619. callback: (_key_, _enable_) => {
  620. if (!_enable_) {
  621. window.GM_CSS_GM_shieldLoginDialog.forEach((item) => {
  622. item.remove();
  623. });
  624. } else {
  625. if (typeof window.GM_CSS_GM_shieldLoginDialog !== "undefined") {
  626. window.GM_CSS_GM_shieldLoginDialog = [
  627. ...window.GM_CSS_GM_shieldLoginDialog,
  628. GM_addStyle(
  629. `.passport-login-container{display: none !important;}`
  630. ),
  631. ];
  632. } else {
  633. window.GM_CSS_GM_shieldLoginDialog = [
  634. GM_addStyle(
  635. `.passport-login-container{display: none !important;}`
  636. ),
  637. ];
  638. }
  639. }
  640. },
  641. },
  642. showDirect: {
  643. text: "手机-标识处理过的底部推荐文章",
  644. enable: true,
  645. showText: (_text_, _enable_) => {
  646. return (_enable_ ? "✅" : "❌") + " " + _text_;
  647. },
  648. },
  649. openNewTab: {
  650. text: "手机-底部推荐文章新标签页打开",
  651. enable: true,
  652. showText: (_text_, _enable_) => {
  653. return (_enable_ ? "✅" : "❌") + " " + _text_;
  654. },
  655. },
  656. removeCSDNDownloadMobile: {
  657. text: "手机-移除文章底部的CSDN下载",
  658. enable: false,
  659. showText: (_text_, _enable_) => {
  660. return (_enable_ ? "✅" : "❌") + " " + _text_;
  661. },
  662. },
  663. });
  664. } else if (Boolean(/jianshu.(com|io)/i.test(window.location.origin))) {
  665. var GM_Menu = new Utils.GM_Menu({
  666. JianShuArticleCenter: {
  667. text: "电脑-全文居中",
  668. enable: true,
  669. showText: (_text_, _enable_) => {
  670. return (_enable_ ? "✅" : "❌") + " " + _text_;
  671. },
  672. callback: () => {
  673. window.location.reload();
  674. },
  675. },
  676. JianShuremoveFooterRecommendRead: {
  677. text: "手机-移除底部推荐阅读",
  678. enable: false,
  679. showText: (_text_, _enable_) => {
  680. return (_enable_ ? "✅" : "❌") + " " + _text_;
  681. },
  682. callback: () => {
  683. window.location.reload();
  684. },
  685. },
  686. });
  687. }
  688.  
  689. JianShu();
  690. CSDN();
  691. })();