CSDN|简书优化

支持手机端和PC端

当前为 2022-12-12 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name CSDN|简书优化
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.4.3.2
  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=1127606
  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. display: none !important;
  96. }
  97. .comment-list-box{
  98. max-height: none !important;
  99. }
  100. .blog_container_aside,
  101. #nav{
  102. margin-left: -45px;
  103. }
  104. .recommend-right.align-items-stretch.clearfix,.dl_right_fixed{
  105. margin-left: 45px;
  106. }
  107. #content_views pre code{
  108. user-select: text !important;
  109. }
  110. `);
  111. $(document).ready(function () {
  112. unsafeWindow.articleType = 0;
  113. Utils.waitForDOM(".hljs-button.signin").then((dom) => {
  114. if (dom.length) {
  115. $(".hljs-button.signin").attr("data-title", "复制");
  116. $(".hljs-button.signin").on("click", function () {
  117. let obj = $(this).parent();
  118. Utils.setClip(obj.text());
  119. });
  120. }
  121. });
  122. });
  123. } else {
  124. //手机
  125. if (isCSDN()) {
  126. GM_addStyle(`
  127. #mainBox {
  128. width: auto;
  129. }
  130. #operate,.feed-Sign-span,
  131. .view_comment_box,
  132. .weixin-shadowbox.wap-shadowbox,
  133. .feed-Sign-span{
  134. display:none !important;
  135. }
  136. .GM-csdn-dl{
  137. padding: .24rem .32rem;
  138. width: 100%;
  139. justify-content: space-between;
  140. -webkit-box-pack: justify;
  141. border-bottom: 1px solid #F5F6F7!important;
  142.  
  143. }
  144. .GM-csdn-title{
  145. font-size: .3rem;
  146. color: #222226;
  147. letter-spacing: 0;
  148. line-height: .44rem;
  149. font-weight: 600;
  150. //max-height: .88rem;
  151. word-break: break-all;
  152. overflow: hidden;
  153. display: -webkit-box;
  154. -webkit-box-orient: vertical;
  155. -webkit-line-clamp: 2
  156. }
  157. .GM-csdn-title a{
  158. word-break: break-all;
  159. color: #222226;
  160. font-weight: 600;
  161. }
  162. .GM-csdn-title em,.GM-csdn-content em{
  163. font-style: normal;
  164. color: #fc5531
  165. }
  166. .GM-csdn-content{
  167. //max-width: 5.58rem;
  168. overflow: hidden;
  169. text-overflow: ellipsis;
  170. display: -webkit-box;
  171. -webkit-line-clamp: 1;
  172. -webkit-box-orient: vertical;
  173. color: #555666;
  174. font-size: .24rem;
  175. line-height: .34rem;
  176. max-height: .34rem;
  177. word-break: break-all;
  178. -webkit-box-flex: 1;
  179. -ms-flex: 1;
  180. flex: 1;
  181. margin-top: .16rem;
  182. }
  183. .GM-csdn-img img{
  184. width: 2.18rem;
  185. height: 1.58rem;
  186. //margin-left: .16rem
  187. }
  188. .GM-csdn-Redirect{
  189. color: #fff;
  190. background-color: #f90707;
  191. font-family: sans-serif;
  192. margin: auto 2px;
  193. border: 1px solid #ccc;
  194. border-radius: 4px;
  195. padding: 0px 3px;
  196. font-size: xx-small;
  197. display: inline;
  198. white-space: nowrap;
  199. }
  200. .component-box .praise {
  201. background: #ff5722;
  202. border-radius: 5px;
  203. padding: 0px 8px;
  204. height: auto;
  205. }
  206. .component-box .praise,.component-box .share {
  207. color: #fff;
  208. }
  209. .component-box a {
  210. display: inline-block;
  211. font-size:xx-small;
  212. }
  213. .component-box {
  214. display: inline;
  215. margin: 0;
  216. position: relative;
  217. white-space:nowrap;
  218. }
  219. .csdn-edu-title{
  220. background: #4d6de1;
  221. border-radius: 5px;
  222. padding: 0px 8px;
  223. height: auto;
  224. color: #fff !important;
  225. }
  226. #comment{
  227. max-height: none !important;
  228. }
  229. .comment_read_more_box,
  230. #content_views pre.set-code-hide .hide-preCode-box,
  231. .passport-login-container,
  232. .hljs-button[data-title='登录后复制']
  233. {
  234. display:none !important;
  235. }
  236. #content_views pre,
  237. #content_views pre code{
  238. webkit-touch-callout: text !important;
  239. -webkit-user-select: text !important;
  240. -khtml-user-select: text !important;
  241. -moz-user-select: text !important;
  242. -ms-user-select: text !important;
  243. user-select: text !important;
  244. }
  245. #content_views pre.set-code-hide{
  246. height: 100% !important;
  247. overflow-y: auto !important;
  248. }
  249. `);
  250. function replace_all_commend() {
  251. //替换所有的推荐
  252. var commend_list = $(".container-fluid");
  253. for (var coml = 0; coml < commend_list.length; coml++) {
  254. let current_commend_className =
  255. commend_list[coml].getAttribute("class");
  256. let current_commend_url = "";
  257. let current_commend_title = "";
  258. let current_commend_content = "";
  259. let current_commend_img = "";
  260. if (commend_list[coml].getAttribute("data-url")) {
  261. //有data-url 就有recommend_title
  262. /* console.log("有 data-url"); */
  263. current_commend_url = commend_list[coml].getAttribute("data-url");
  264. current_commend_title = $(commend_list[coml])
  265. .find(".recommend_title")
  266. .html();
  267. current_commend_content = $(commend_list[coml])
  268. .find(".text")
  269. .html();
  270. // current_commend_title = commend_list[coml].getElementsByClassName("recommend_title")[0].innerHTML;
  271. // current_commend_content = commend_list[coml].getElementsByClassName("text active")[0].innerHTML;
  272. let current_commend_img_dom = $(commend_list[coml]).find(
  273. ".recommend-img"
  274. );
  275. if (current_commend_img_dom.length) {
  276. for (
  277. var imgs = 0;
  278. imgs < current_commend_img_dom.length;
  279. imgs++
  280. ) {
  281. current_commend_img =
  282. current_commend_img + current_commend_img_dom[imgs].innerHTML;
  283. }
  284. }
  285. } else {
  286. /* console.log("没有data-url"); */
  287. current_commend_url =
  288. commend_list[coml].getElementsByTagName("a")[0].href;
  289. current_commend_title =
  290. commend_list[coml].getElementsByTagName("a")[0].innerHTML;
  291. current_commend_content =
  292. commend_list[coml].getElementsByClassName("text")[0].innerHTML;
  293. current_commend_img = "";
  294. }
  295. /* console.log("真实url:", current_commend_url); */
  296. if (GM_menus[0][2]) {
  297. current_commend_title =
  298. current_commend_title +
  299. `<div class="GM-csdn-Redirect">Redirect</div>`;
  300. }
  301. if (
  302. current_commend_url.match(
  303. /http(s|):\/\/(download.csdn.net|www.iteye.com\/resource)/g
  304. )
  305. ) {
  306. console.log("该链接为csdn资源下载,标识");
  307. current_commend_title =
  308. current_commend_title +
  309. `<div class="component-box"><a class="praise" href="javascript:;">CSDN下载</a></div>`;
  310. } else if (current_commend_url.match(/edu.csdn.net/g)) {
  311. console.log("该链接为csdn学院下载,标识");
  312. current_commend_title =
  313. current_commend_title +
  314. `<div class="component-box"><a class="csdn-edu-title" href="javascript:;">CSDN学院</a></div>`;
  315. }
  316. commend_list[coml].className = "GM-csdn-dl";
  317. commend_list[coml].setAttribute("data-url", current_commend_url);
  318. commend_list[coml].innerHTML =
  319. `<div class="GM-csdn-title">` +
  320. current_commend_title +
  321. "</div>" +
  322. `<div class="GM-csdn-content">` +
  323. current_commend_content +
  324. "</div>" +
  325. `<div class="GM-csdn-img">` +
  326. current_commend_img +
  327. "</div>";
  328. $("#recommend")
  329. .find(".recommend_list")
  330. .before($("#first_recommend_list").find("dl").parent().html());
  331. $("#first_recommend_list")?.remove();
  332. }
  333. }
  334.  
  335. function new_commend_event() {
  336. //新的推荐跳转事件
  337. $(".GM-csdn-dl").bind("click", function (e) {
  338. let current_click_url = e.currentTarget.dataset.url;
  339. if (GM_menus[1][2]) {
  340. window.open(current_click_url);
  341. } else {
  342. window.location.href = current_click_url;
  343. }
  344. });
  345. }
  346.  
  347. function auto_review() {
  348. //自动展开
  349. $(".article_content")?.removeAttr("style");
  350. $(".readall_box")?.show();
  351. $(".readall_box")?.addClass("readall_box_nobg");
  352. $(".readall_box")?.hide();
  353. $(".readall_box")?.addClass("readall_box_nobg");
  354. $(".detail-open-app-isshow")?.css("display", "block");
  355. $(".isshow-mask-lock-box")?.show();
  356. }
  357. function removeElement() {
  358. /* 移除一些元素 */
  359. $(".passport-login-container")?.remove();
  360. }
  361.  
  362. $(document).ready(function () {
  363. console.log("mobile csdn");
  364. var csdn_interval_runum = 0;
  365. var csdn_interval = setInterval(function () {
  366. csdn_interval_runum = csdn_interval_runum + 1;
  367. console.log("展开");
  368. if (csdn_interval_runum <= 5) {
  369. auto_review();
  370. removeElement();
  371. } else {
  372. clearInterval(csdn_interval);
  373. }
  374. }, 200);
  375. var loding_comment_dom_num = 0;
  376. var loding_comment_dom = setInterval(function () {
  377. loding_comment_dom_num = loding_comment_dom_num + 1;
  378. if (loding_comment_dom_num <= 5) {
  379. try {
  380. replace_all_commend();
  381. } catch (err) {
  382. console.log("替换底部链接失败", err);
  383. }
  384. } else {
  385. clearInterval(loding_comment_dom);
  386. new_commend_event();
  387. }
  388. }, 500);
  389. });
  390. } else if (isJianShu()) {
  391. console.log("简书");
  392. GM_addStyle(`
  393. .download-app-guidance,
  394. .call-app-btn,
  395. .collapse-tips,
  396. .note-graceful-button,
  397. .app-open,
  398. .header-wrap,
  399. .recommend-wrap.recommend-ad,
  400. .call-app-Ad-bottom,
  401. #recommended-notes p.top-title span.more,
  402. #homepage .modal,
  403. button.index_call-app-btn,
  404. span.note__flow__download,
  405. .download-guide,
  406. #footer,
  407. .comment-open-app-btn-wrap{
  408. display:none !important;
  409. }
  410. body.reader-day-mode.normal-size {
  411. overflow: auto !important;
  412. }
  413. .collapse-free-content{
  414. height:auto !important;
  415. }
  416. .copyright{
  417. color:#000 !important;
  418. }
  419. #note-show .content .show-content-free .collapse-free-content:after{
  420. background-image:none !important;
  421. }
  422. `);
  423. Utils.waitForDOM('div#homepage div[class*="dialog-"]').then((dom) => {
  424. if (dom.length) {
  425. dom[0].style["visibility"] = "hidden";
  426. }
  427. });
  428. Utils.mutationObserver('div#homepage div[class*="dialog-"]', {
  429. fn: (mutations) => {
  430. if (mutations.length == 0) {
  431. return;
  432. }
  433. if (mutations[0].target.style["display"] != "none") {
  434. document
  435. .querySelector('div#homepage div[class*="dialog-"] .cancel')
  436. ?.click();
  437. }
  438. },
  439. config: {
  440. /* 子节点的变动(新增、删除或者更改) */
  441. childList: false,
  442. /* 属性的变动 */
  443. attributes: true,
  444. /* 节点内容或节点文本的变动 */
  445. characterData: true,
  446. /* 是否将观察器应用于该节点的所有后代节点 */
  447. subtree: true,
  448. },
  449. });
  450. }
  451. }
  452. })();