CSDN|简书优化

支持手机端和PC端

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

  1. // ==UserScript==
  2. // @name CSDN|简书优化
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.4.7
  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. ["menu_showRedirect", "手机csdn显示Redirect", true],
  32. ["menu_thistab", "手机csdn新页面打开", true],
  33. ],
  34. init: function () {
  35. /* 初始化数据 */
  36. let _this_ = this;
  37. Array.from(this.data).forEach((item, index) => {
  38. let menuKey = item[0];
  39. let menuValue = GM_getValue(menuKey);
  40. if (menuValue == null) {
  41. GM_setValue(menuKey, true);
  42. menuValue = GM_getValue(menuKey);
  43. }
  44. _this_.data[index][item.length - 1] = menuValue;
  45. });
  46. },
  47. register: function () {
  48. /* 注册油猴菜单 */
  49. Array.from(this.data).forEach((item) => {
  50. let menuKey = item[0];
  51. let menuName = item[1];
  52. let menuValue = item[2];
  53. let menuShowName = "[" + (menuValue ? "√" : "×") + "]" + menuName;
  54. GM_registerMenuCommand(menuShowName, function () {
  55. if (menuValue) {
  56. GM_setValue(menuKey, false);
  57. } else {
  58. GM_setValue(menuKey, true);
  59. }
  60. window.location.reload();
  61. });
  62. });
  63. },
  64. };
  65.  
  66. function JianShu() {
  67. /* 简书 */
  68. function isJianShu() {
  69. /* 判断是否是 简书 */
  70. return Boolean(/jianshu.com/i.test(window.location.origin));
  71. }
  72. function PC() {
  73. console.log("简书");
  74. const css = `
  75. .download-app-guidance,
  76. .call-app-btn,
  77. .collapse-tips,
  78. .note-graceful-button,
  79. .app-open,
  80. .header-wrap,
  81. .recommend-wrap.recommend-ad,
  82. .call-app-Ad-bottom,
  83. #recommended-notes p.top-title span.more,
  84. #homepage .modal,
  85. button.index_call-app-btn,
  86. span.note__flow__download,
  87. .download-guide,
  88. #footer,
  89. .comment-open-app-btn-wrap,
  90. .nav.navbar-nav + div{
  91. display:none !important;
  92. }
  93. body.reader-day-mode.normal-size {
  94. overflow: auto !important;
  95. }
  96. .collapse-free-content{
  97. height:auto !important;
  98. }
  99. .copyright{
  100. color:#000 !important;
  101. }
  102. #note-show .content .show-content-free .collapse-free-content:after{
  103. background-image:none !important;
  104. }
  105. `;
  106. GM_addStyle(css);
  107. Utils.waitForDOM('div#homepage div[class*="dialog-"]').then((dom) => {
  108. if (dom.length) {
  109. dom[0].style["visibility"] = "hidden";
  110. }
  111. });
  112. Utils.mutationObserver('div#homepage div[class*="dialog-"]', {
  113. fn: (mutations) => {
  114. if (mutations.length == 0) {
  115. return;
  116. }
  117. if (mutations[0].target.style["display"] != "none") {
  118. document
  119. .querySelector('div#homepage div[class*="dialog-"] .cancel')
  120. ?.click();
  121. }
  122. },
  123. config: {
  124. /* 子节点的变动(新增、删除或者更改) */
  125. childList: false,
  126. /* 属性的变动 */
  127. attributes: true,
  128. /* 节点内容或节点文本的变动 */
  129. characterData: true,
  130. /* 是否将观察器应用于该节点的所有后代节点 */
  131. subtree: true,
  132. },
  133. });
  134. }
  135. if (isJianShu()) {
  136. PC();
  137. }
  138. }
  139. function CSDN() {
  140. /* csdn-移动端 */
  141. function isCSDN() {
  142. /* 判断是否是 CSDN */
  143. return Boolean(/csdn.net/i.test(window.location.origin));
  144. }
  145. function Mobile() {
  146. /* 移动端 */
  147. console.log("CSDN-移动端");
  148. const css = `
  149. #mainBox{
  150. width: auto;
  151. }
  152. .user-desc.user-desc-fix{
  153. height: auto !important;
  154. overflow: auto !important;
  155. }
  156. #operate,.feed-Sign-span,
  157. .view_comment_box,
  158. .weixin-shadowbox.wap-shadowbox,
  159. .feed-Sign-span,
  160. .user-desc.user-desc-fix,
  161. .comment_read_more_box,
  162. #content_views pre.set-code-hide .hide-preCode-box,
  163. .passport-login-container,
  164. .hljs-button[data-title='登录后复制'],
  165. .article-show-more,
  166. #treeSkill,
  167. div.btn_open_app_prompt_div{
  168. display:none !important;
  169. }
  170. .GM-csdn-dl{
  171. padding: .24rem .32rem;
  172. width: 100%;
  173. justify-content: space-between;
  174. -webkit-box-pack: justify;
  175. border-bottom: 1px solid #F5F6F7!important;
  176. }
  177. .GM-csdn-title{
  178. font-size: .3rem;
  179. color: #222226;
  180. letter-spacing: 0;
  181. line-height: .44rem;
  182. font-weight: 600;
  183. //max-height: .88rem;
  184. word-break: break-all;
  185. overflow: hidden;
  186. display: -webkit-box;
  187. -webkit-box-orient: vertical;
  188. -webkit-line-clamp: 2
  189. }
  190. .GM-csdn-title a{
  191. word-break: break-all;
  192. color: #222226;
  193. font-weight: 600;
  194. }
  195. .GM-csdn-title em,.GM-csdn-content em{
  196. font-style: normal;
  197. color: #fc5531
  198. }
  199. .GM-csdn-content{
  200. //max-width: 5.58rem;
  201. overflow: hidden;
  202. text-overflow: ellipsis;
  203. display: -webkit-box;
  204. -webkit-line-clamp: 1;
  205. -webkit-box-orient: vertical;
  206. color: #555666;
  207. font-size: .24rem;
  208. line-height: .34rem;
  209. max-height: .34rem;
  210. word-break: break-all;
  211. -webkit-box-flex: 1;
  212. -ms-flex: 1;
  213. flex: 1;
  214. margin-top: .16rem;
  215. }
  216. .GM-csdn-img img{
  217. width: 2.18rem;
  218. height: 1.58rem;
  219. //margin-left: .16rem
  220. }
  221. .GM-csdn-Redirect{
  222. color: #fff;
  223. background-color: #f90707;
  224. font-family: sans-serif;
  225. margin: auto 2px;
  226. border: 1px solid #ccc;
  227. border-radius: 4px;
  228. padding: 0px 3px;
  229. font-size: xx-small;
  230. display: inline;
  231. white-space: nowrap;
  232. }
  233. .component-box .praise {
  234. background: #ff5722;
  235. border-radius: 5px;
  236. padding: 0px 8px;
  237. height: auto;
  238. }
  239. .component-box .praise,.component-box .share {
  240. color: #fff;
  241. }
  242. .component-box a {
  243. display: inline-block;
  244. font-size:xx-small;
  245. }
  246. .component-box {
  247. display: inline;
  248. margin: 0;
  249. position: relative;
  250. white-space:nowrap;
  251. }
  252. .csdn-edu-title{
  253. background: #4d6de1;
  254. border-radius: 5px;
  255. padding: 0px 8px;
  256. height: auto;
  257. color: #fff !important;
  258. }
  259. #comment{
  260. max-height: none !important;
  261. }
  262. #content_views pre,
  263. #content_views pre code{
  264. webkit-touch-callout: text !important;
  265. -webkit-user-select: text !important;
  266. -khtml-user-select: text !important;
  267. -moz-user-select: text !important;
  268. -ms-user-select: text !important;
  269. user-select: text !important;
  270. }
  271. #content_views pre.set-code-hide{
  272. height: 100% !important;
  273. overflow-y: auto !important;
  274. }
  275. `;
  276. GM_addStyle(css);
  277. function replace_all_commend() {
  278. //替换所有的推荐
  279. var commend_list = $(".container-fluid");
  280. for (var coml = 0; coml < commend_list.length; coml++) {
  281. let current_commend_className =
  282. commend_list[coml].getAttribute("class");
  283. let current_commend_url = "";
  284. let current_commend_title = "";
  285. let current_commend_content = "";
  286. let current_commend_img = "";
  287. if (commend_list[coml].getAttribute("data-url")) {
  288. //有data-url 就有recommend_title
  289. /* console.log("有 data-url"); */
  290. current_commend_url = commend_list[coml].getAttribute("data-url");
  291. current_commend_title = $(commend_list[coml])
  292. .find(".recommend_title")
  293. .html();
  294. current_commend_content = $(commend_list[coml])
  295. .find(".text")
  296. .html();
  297. // current_commend_title = commend_list[coml].getElementsByClassName("recommend_title")[0].innerHTML;
  298. // current_commend_content = commend_list[coml].getElementsByClassName("text active")[0].innerHTML;
  299. let current_commend_img_dom = $(commend_list[coml]).find(
  300. ".recommend-img"
  301. );
  302. if (current_commend_img_dom.length) {
  303. for (
  304. var imgs = 0;
  305. imgs < current_commend_img_dom.length;
  306. imgs++
  307. ) {
  308. current_commend_img =
  309. current_commend_img + current_commend_img_dom[imgs].innerHTML;
  310. }
  311. }
  312. } else {
  313. /* console.log("没有data-url"); */
  314. current_commend_url =
  315. commend_list[coml].getElementsByTagName("a")[0].href;
  316. current_commend_title =
  317. commend_list[coml].getElementsByTagName("a")[0].innerHTML;
  318. current_commend_content =
  319. commend_list[coml].getElementsByClassName("text")[0].innerHTML;
  320. current_commend_img = "";
  321. }
  322. /* console.log("真实url:", current_commend_url); */
  323. if (GM_menu.data[0][2]) {
  324. current_commend_title =
  325. current_commend_title +
  326. `<div class="GM-csdn-Redirect">Redirect</div>`;
  327. }
  328. if (
  329. current_commend_url.match(
  330. /http(s|):\/\/(download.csdn.net|www.iteye.com\/resource)/g
  331. )
  332. ) {
  333. console.log("该链接为csdn资源下载,标识");
  334. current_commend_title =
  335. current_commend_title +
  336. `<div class="component-box"><a class="praise" href="javascript:;">CSDN下载</a></div>`;
  337. } else if (current_commend_url.match(/edu.csdn.net/g)) {
  338. console.log("该链接为csdn学院下载,标识");
  339. current_commend_title =
  340. current_commend_title +
  341. `<div class="component-box"><a class="csdn-edu-title" href="javascript:;">CSDN学院</a></div>`;
  342. }
  343. commend_list[coml].className = "GM-csdn-dl";
  344. commend_list[coml].setAttribute("data-url", current_commend_url);
  345. commend_list[coml].innerHTML =
  346. `<div class="GM-csdn-title">` +
  347. current_commend_title +
  348. "</div>" +
  349. `<div class="GM-csdn-content">` +
  350. current_commend_content +
  351. "</div>" +
  352. `<div class="GM-csdn-img">` +
  353. current_commend_img +
  354. "</div>";
  355. $("#recommend")
  356. .find(".recommend_list")
  357. .before($("#first_recommend_list").find("dl").parent().html());
  358. $("#first_recommend_list")?.remove();
  359. }
  360. }
  361.  
  362. function new_commend_event() {
  363. //新的推荐跳转事件
  364. $(".GM-csdn-dl").bind("click", function (e) {
  365. let current_click_url = e.currentTarget.dataset.url;
  366. if (GM_menu.data[1][2]) {
  367. window.open(current_click_url);
  368. } else {
  369. window.location.href = current_click_url;
  370. }
  371. });
  372. }
  373.  
  374. function auto_review() {
  375. //自动展开
  376. $(".article_content")?.removeAttr("style");
  377. $(".readall_box")?.show();
  378. $(".readall_box")?.addClass("readall_box_nobg");
  379. $(".readall_box")?.hide();
  380. $(".readall_box")?.addClass("readall_box_nobg");
  381. $(".detail-open-app-isshow")?.css("display", "block");
  382. $(".isshow-mask-lock-box")?.show();
  383. }
  384. function removeElement() {
  385. /* 移除一些元素 */
  386. $(".passport-login-container")?.remove();
  387. }
  388.  
  389. $(document).ready(function () {
  390. var csdn_interval_runum = 0;
  391. var csdn_interval = setInterval(function () {
  392. csdn_interval_runum = csdn_interval_runum + 1;
  393. console.log("展开");
  394. if (csdn_interval_runum <= 5) {
  395. auto_review();
  396. removeElement();
  397. } else {
  398. clearInterval(csdn_interval);
  399. }
  400. }, 200);
  401. var loding_comment_dom_num = 0;
  402. var loding_comment_dom = setInterval(function () {
  403. loding_comment_dom_num = loding_comment_dom_num + 1;
  404. if (loding_comment_dom_num <= 5) {
  405. try {
  406. replace_all_commend();
  407. } catch (err) {
  408. console.log("替换底部链接失败", err);
  409. }
  410. } else {
  411. clearInterval(loding_comment_dom);
  412. new_commend_event();
  413. }
  414. }, 500);
  415. });
  416. }
  417. function PC() {
  418. /* 桌面端 */
  419. console.log("CSDN-桌面端访问");
  420. const css = `
  421. .ecommend-item-box.recommend-recommend-box,
  422. .login-mark,
  423. .opt-box.text-center,
  424. .leftPop,
  425. #csdn-shop-window,
  426. #passportbox,
  427. .passport-login-container,
  428. .toolbar-advert,
  429. .hide-article-box,
  430. .user-desc.user-desc-fix,
  431. .recommend-card-box,
  432. .more-article,
  433. .article-show-more{
  434. display: none !important;
  435. }
  436. .comment-list-box{
  437. max-height: none !important;
  438. }
  439. .blog_container_aside,
  440. #nav{
  441. margin-left: -45px;
  442. }
  443. .recommend-right.align-items-stretch.clearfix,.dl_right_fixed{
  444. margin-left: 45px;
  445. }
  446. #content_views pre code{
  447. user-select: text !important;
  448. }
  449. #article_content,
  450. .user-article.user-article-hide{
  451. height: auto !important;
  452. overflow: auto !important;
  453. }
  454. `;
  455. function UnblockCopy(dom) {
  456. /* 取消禁止复制 */
  457. if (dom.length) {
  458. $(".hljs-button.signin").attr("data-title", "复制");
  459. $(".hljs-button.signin").on("click", function () {
  460. const copyBtn = $(this);
  461. const copyArea = $(this).parent();
  462. copyBtn.attr("data-title", "复制成功");
  463. const btnParentElement = Utils.findParentDOM(this, (dom) => {
  464. return dom.className == "prettyprint" ? true : false;
  465. });
  466. if (btnParentElement) {
  467. $(btnParentElement).bind({
  468. mouseenter: function (e) {
  469. copyBtn.attr("data-title", "复制");
  470. $(btnParentElement).unbind("mouseenter").unbind("mouseleave");
  471. },
  472. mouseleave: function (e) {
  473. copyBtn.attr("data-title", "复制");
  474. $(btnParentElement).unbind("mouseenter").unbind("mouseleave");
  475. },
  476. });
  477. }
  478. Utils.setClip(copyArea.text());
  479. });
  480. }
  481. }
  482. function clickPreCodeAutoPreview() {
  483. /* 点击代码块自动展开 */
  484. $("pre[data-index]").on("click", function () {
  485. let obj = $(this);
  486. obj.css("height", "auto");
  487. obj.find(".hide-preCode-box")?.remove();
  488. });
  489. }
  490. GM_addStyle(css);
  491. $(document).ready(function () {
  492. unsafeWindow.articleType = 0;
  493. unsafeWindow.csdn.copyright.textData = undefined;
  494. unsafeWindow.csdn.copyright.htmlData = undefined;
  495. $(".article-copyright")?.remove();
  496. Utils.waitForDOM(".hljs-button.signin").then((dom) => {
  497. UnblockCopy(dom);
  498. });
  499. $(".recommend-item-box[data-url*='https://download.csdn.net/']").each(
  500. (index, item) => {
  501. $(item).find(".content-box").css("border", "2px solid red");
  502. }
  503. );
  504. clickPreCodeAutoPreview();
  505. });
  506. }
  507.  
  508. if (isCSDN()) {
  509. if (Utils.isPhone()) {
  510. Mobile();
  511. } else {
  512. PC();
  513. }
  514. }
  515. }
  516.  
  517. GM_menu.init();
  518. GM_menu.register();
  519.  
  520. JianShu();
  521. CSDN();
  522. })();