CSDN|简书优化

支持手机端和PC端

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

  1. // ==UserScript==
  2. // @name CSDN|简书优化
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.4.5
  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. // @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 http://cdn.staticfile.org/jquery/2.1.4/jquery.min.js
  22. // @require https://greasyfork.org/scripts/455186-whitesevsutils/code/WhiteSevsUtils.js?version=1135447
  23. // @require https://greasyfork.org/scripts/449471-viewer/code/Viewer.js?version=1081056
  24. // ==/UserScript==
  25.  
  26. (function () {
  27. "use strict";
  28.  
  29. var GM_menus = [
  30. ["menu_showRedirect", "手机csdn显示Redirect", true],
  31. ["menu_thistab", "手机csdn新页面打开", true],
  32. ];
  33.  
  34. function change_normal_menu() {
  35. //修改菜单默认值
  36. for (let i = 0; i < GM_menus.length; i++) {
  37. let get_GM_value = GM_getValue(GM_menus[i][0]);
  38. if (get_GM_value) {
  39. } else {
  40. console.log("修改菜单值");
  41. GM_menus[i][2] = false;
  42. }
  43. }
  44. }
  45.  
  46. function register_GM_Menu() {
  47. //注册油猴菜单
  48. for (let i = 0; i < GM_menus.length; i++) {
  49. let current_v = GM_menus[i][0];
  50. let current_name = GM_menus[i][1];
  51. let current_status = GM_menus[i][2];
  52. GM_registerMenuCommand(
  53. `[${current_status ? "√" : "×"}]${current_name}`,
  54. function () {
  55. if (current_status) {
  56. console.log("关闭");
  57. GM_deleteValue(current_v);
  58. } else {
  59. console.log("开启");
  60. GM_setValue(current_v, "1");
  61. }
  62. window.location.reload();
  63. }
  64. );
  65. }
  66. }
  67.  
  68. function isPhone() {
  69. /* 判断是否是手机访问 */
  70. return Boolean(/(iPhone|iPad|iPod|iOS|Android)/i.test(navigator.userAgent));
  71. }
  72.  
  73. function isCSDN() {
  74. return Boolean(/csdn.net/i.test(window.location.href));
  75. }
  76.  
  77. function isJianShu() {
  78. return Boolean(/jianshu.com/i.test(window.location.href));
  79. }
  80. change_normal_menu();
  81. register_GM_Menu();
  82.  
  83. if (!isPhone()) {
  84. //电脑
  85. console.log("电脑访问");
  86. GM_addStyle(`
  87. .ecommend-item-box.recommend-recommend-box,
  88. .login-mark,
  89. .opt-box.text-center,
  90. .leftPop,
  91. #csdn-shop-window,
  92. #passportbox,
  93. .passport-login-container,
  94. .toolbar-advert,
  95. .hide-article-box{
  96. display: none !important;
  97. }
  98. .comment-list-box{
  99. max-height: none !important;
  100. }
  101. .blog_container_aside,
  102. #nav{
  103. margin-left: -45px;
  104. }
  105. .recommend-right.align-items-stretch.clearfix,.dl_right_fixed{
  106. margin-left: 45px;
  107. }
  108. #content_views pre code{
  109. user-select: text !important;
  110. }
  111. #article_content{
  112. height: auto !important;
  113. overflow: auto !important;
  114. }
  115. `);
  116. $(document).ready(function () {
  117. unsafeWindow.articleType = 0;
  118. Utils.waitForDOM(".hljs-button.signin").then((dom) => {
  119. if (dom.length) {
  120. $(".hljs-button.signin").attr("data-title", "复制");
  121. $(".hljs-button.signin").on("click", function () {
  122. const copyBtn = $(this);
  123. const copyArea = $(this).parent();
  124. copyBtn.attr("data-title", "复制成功");
  125. const btnParentElement = Utils.findParentDOM(this, (dom) => {
  126. return dom.className == "prettyprint" ? true : false;
  127. });
  128. if (btnParentElement) {
  129. $(btnParentElement).bind({
  130. mouseenter: function (e) {
  131. copyBtn.attr("data-title", "复制");
  132. $(btnParentElement).unbind("mouseenter").unbind("mouseleave");
  133. },
  134. mouseleave: function (e) {
  135. copyBtn.attr("data-title", "复制");
  136. $(btnParentElement).unbind("mouseenter").unbind("mouseleave");
  137. },
  138. });
  139. }
  140. Utils.setClip(copyArea.text());
  141. });
  142. }
  143. });
  144. $(".recommend-item-box[data-url*='https://download.csdn.net/']").each(
  145. (index, item) => {
  146. $(item).find(".content-box").css("border", "2px solid red");
  147. }
  148. );
  149. });
  150. } else {
  151. //手机
  152. if (isCSDN()) {
  153. GM_addStyle(`
  154. #mainBox {
  155. width: auto;
  156. }
  157. #operate,.feed-Sign-span,
  158. .view_comment_box,
  159. .weixin-shadowbox.wap-shadowbox,
  160. .feed-Sign-span{
  161. display:none !important;
  162. }
  163. .GM-csdn-dl{
  164. padding: .24rem .32rem;
  165. width: 100%;
  166. justify-content: space-between;
  167. -webkit-box-pack: justify;
  168. border-bottom: 1px solid #F5F6F7!important;
  169.  
  170. }
  171. .GM-csdn-title{
  172. font-size: .3rem;
  173. color: #222226;
  174. letter-spacing: 0;
  175. line-height: .44rem;
  176. font-weight: 600;
  177. //max-height: .88rem;
  178. word-break: break-all;
  179. overflow: hidden;
  180. display: -webkit-box;
  181. -webkit-box-orient: vertical;
  182. -webkit-line-clamp: 2
  183. }
  184. .GM-csdn-title a{
  185. word-break: break-all;
  186. color: #222226;
  187. font-weight: 600;
  188. }
  189. .GM-csdn-title em,.GM-csdn-content em{
  190. font-style: normal;
  191. color: #fc5531
  192. }
  193. .GM-csdn-content{
  194. //max-width: 5.58rem;
  195. overflow: hidden;
  196. text-overflow: ellipsis;
  197. display: -webkit-box;
  198. -webkit-line-clamp: 1;
  199. -webkit-box-orient: vertical;
  200. color: #555666;
  201. font-size: .24rem;
  202. line-height: .34rem;
  203. max-height: .34rem;
  204. word-break: break-all;
  205. -webkit-box-flex: 1;
  206. -ms-flex: 1;
  207. flex: 1;
  208. margin-top: .16rem;
  209. }
  210. .GM-csdn-img img{
  211. width: 2.18rem;
  212. height: 1.58rem;
  213. //margin-left: .16rem
  214. }
  215. .GM-csdn-Redirect{
  216. color: #fff;
  217. background-color: #f90707;
  218. font-family: sans-serif;
  219. margin: auto 2px;
  220. border: 1px solid #ccc;
  221. border-radius: 4px;
  222. padding: 0px 3px;
  223. font-size: xx-small;
  224. display: inline;
  225. white-space: nowrap;
  226. }
  227. .component-box .praise {
  228. background: #ff5722;
  229. border-radius: 5px;
  230. padding: 0px 8px;
  231. height: auto;
  232. }
  233. .component-box .praise,.component-box .share {
  234. color: #fff;
  235. }
  236. .component-box a {
  237. display: inline-block;
  238. font-size:xx-small;
  239. }
  240. .component-box {
  241. display: inline;
  242. margin: 0;
  243. position: relative;
  244. white-space:nowrap;
  245. }
  246. .csdn-edu-title{
  247. background: #4d6de1;
  248. border-radius: 5px;
  249. padding: 0px 8px;
  250. height: auto;
  251. color: #fff !important;
  252. }
  253. #comment{
  254. max-height: none !important;
  255. }
  256. .comment_read_more_box,
  257. #content_views pre.set-code-hide .hide-preCode-box,
  258. .passport-login-container,
  259. .hljs-button[data-title='登录后复制']
  260. {
  261. display:none !important;
  262. }
  263. #content_views pre,
  264. #content_views pre code{
  265. webkit-touch-callout: text !important;
  266. -webkit-user-select: text !important;
  267. -khtml-user-select: text !important;
  268. -moz-user-select: text !important;
  269. -ms-user-select: text !important;
  270. user-select: text !important;
  271. }
  272. #content_views pre.set-code-hide{
  273. height: 100% !important;
  274. overflow-y: auto !important;
  275. }
  276. `);
  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_menus[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_menus[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. console.log("mobile csdn");
  391. var csdn_interval_runum = 0;
  392. var csdn_interval = setInterval(function () {
  393. csdn_interval_runum = csdn_interval_runum + 1;
  394. console.log("展开");
  395. if (csdn_interval_runum <= 5) {
  396. auto_review();
  397. removeElement();
  398. } else {
  399. clearInterval(csdn_interval);
  400. }
  401. }, 200);
  402. var loding_comment_dom_num = 0;
  403. var loding_comment_dom = setInterval(function () {
  404. loding_comment_dom_num = loding_comment_dom_num + 1;
  405. if (loding_comment_dom_num <= 5) {
  406. try {
  407. replace_all_commend();
  408. } catch (err) {
  409. console.log("替换底部链接失败", err);
  410. }
  411. } else {
  412. clearInterval(loding_comment_dom);
  413. new_commend_event();
  414. }
  415. }, 500);
  416. });
  417. } else if (isJianShu()) {
  418. console.log("简书");
  419. GM_addStyle(`
  420. .download-app-guidance,
  421. .call-app-btn,
  422. .collapse-tips,
  423. .note-graceful-button,
  424. .app-open,
  425. .header-wrap,
  426. .recommend-wrap.recommend-ad,
  427. .call-app-Ad-bottom,
  428. #recommended-notes p.top-title span.more,
  429. #homepage .modal,
  430. button.index_call-app-btn,
  431. span.note__flow__download,
  432. .download-guide,
  433. #footer,
  434. .comment-open-app-btn-wrap{
  435. display:none !important;
  436. }
  437. body.reader-day-mode.normal-size {
  438. overflow: auto !important;
  439. }
  440. .collapse-free-content{
  441. height:auto !important;
  442. }
  443. .copyright{
  444. color:#000 !important;
  445. }
  446. #note-show .content .show-content-free .collapse-free-content:after{
  447. background-image:none !important;
  448. }
  449. `);
  450. Utils.waitForDOM('div#homepage div[class*="dialog-"]').then((dom) => {
  451. if (dom.length) {
  452. dom[0].style["visibility"] = "hidden";
  453. }
  454. });
  455. Utils.mutationObserver('div#homepage div[class*="dialog-"]', {
  456. fn: (mutations) => {
  457. if (mutations.length == 0) {
  458. return;
  459. }
  460. if (mutations[0].target.style["display"] != "none") {
  461. document
  462. .querySelector('div#homepage div[class*="dialog-"] .cancel')
  463. ?.click();
  464. }
  465. },
  466. config: {
  467. /* 子节点的变动(新增、删除或者更改) */
  468. childList: false,
  469. /* 属性的变动 */
  470. attributes: true,
  471. /* 节点内容或节点文本的变动 */
  472. characterData: true,
  473. /* 是否将观察器应用于该节点的所有后代节点 */
  474. subtree: true,
  475. },
  476. });
  477. }
  478. }
  479. })();