CSDN|简书优化

支持手机端和PC端

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

  1. // ==UserScript==
  2. // @name CSDN|简书优化
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.4.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. // @run-at document-start
  20. // @require http://cdn.staticfile.org/jquery/2.1.4/jquery.min.js
  21. // ==/UserScript==
  22.  
  23. (function () {
  24. 'use strict';
  25. var GM_menus = [
  26. ["menu_showRedirect", "手机csdn显示Redirect", true],
  27. ["menu_thistab", "手机csdn新页面打开", true]
  28. ]
  29.  
  30. function change_normal_menu() {
  31. //修改菜单默认值
  32. console.log(GM_listValues())
  33. for (let i = 0; i < GM_menus.length; i++) {
  34. let get_GM_value = GM_getValue(GM_menus[i][0]);
  35. if (get_GM_value) {
  36.  
  37. } else {
  38. console.log("修改菜单值");
  39. GM_menus[i][2] = false;
  40. }
  41. console.log(GM_menus);
  42. }
  43. }
  44.  
  45. function register_GM_Menu() {
  46. //注册油猴菜单
  47. for (let i = 0; i < GM_menus.length; i++) {
  48. let current_v = GM_menus[i][0];
  49. let current_name = GM_menus[i][1];
  50. let current_status = GM_menus[i][2];
  51. GM_registerMenuCommand(`[${current_status?"√":"×"}]${current_name}`, function () {
  52. if (current_status) {
  53. console.log("关闭");
  54. GM_deleteValue(current_v);
  55. } else {
  56. console.log("开启");
  57. GM_setValue(current_v, "1");
  58. }
  59. window.location.reload();
  60. })
  61. }
  62.  
  63. }
  64.  
  65. function isPhone(){/* 判断是否是手机访问 */
  66. return Boolean(/(iPhone|iPad|iPod|iOS|Android)/i.test(navigator.userAgent));
  67. }
  68.  
  69. function isCSDN(){
  70. return Boolean(/csdn.net/i.test(window.location.href));
  71. }
  72.  
  73. function isJianShu(){
  74. return Boolean(/jianshu.com/i.test(window.location.href));
  75. }
  76. change_normal_menu();
  77. register_GM_Menu();
  78. var usa = navigator.userAgent.match('Windows');
  79.  
  80. if (!isPhone()) { //电脑
  81. console.log("电脑访问");
  82. GM_addStyle(`
  83. .ecommend-item-box.recommend-recommend-box,
  84. .login-mark,
  85. .opt-box.text-center,
  86. .leftPop,
  87. #csdn-shop-window,
  88. #passportbox,
  89. .passport-login-container,
  90. .toolbar-advert{
  91. display: none !important;
  92. }
  93. .comment-list-box{
  94. max-height: none !important;
  95. }
  96. .blog_container_aside,
  97. #nav{
  98. margin-left: -45px;
  99. }
  100. .recommend-right.align-items-stretch.clearfix,.dl_right_fixed{
  101. margin-left: 45px;
  102. }
  103. `)
  104. } else { //手机
  105. if (isCSDN()) {
  106. GM_addStyle(`
  107. #mainBox {
  108. width: auto;
  109. }
  110. #operate,.feed-Sign-span,
  111. .view_comment_box,
  112. .weixin-shadowbox.wap-shadowbox,
  113. .feed-Sign-span{
  114. display:none !important;
  115. }
  116. .GM-csdn-dl{
  117. padding: .24rem .32rem;
  118. width: 100%;
  119. justify-content: space-between;
  120. -webkit-box-pack: justify;
  121. border-bottom: 1px solid #F5F6F7!important;
  122.  
  123. }
  124. .GM-csdn-title{
  125. font-size: .3rem;
  126. color: #222226;
  127. letter-spacing: 0;
  128. line-height: .44rem;
  129. font-weight: 600;
  130. //max-height: .88rem;
  131. word-break: break-all;
  132. overflow: hidden;
  133. display: -webkit-box;
  134. -webkit-box-orient: vertical;
  135. -webkit-line-clamp: 2
  136. }
  137. .GM-csdn-title a{
  138. word-break: break-all;
  139. color: #222226;
  140. font-weight: 600;
  141. }
  142. .GM-csdn-title em,.GM-csdn-content em{
  143. font-style: normal;
  144. color: #fc5531
  145. }
  146. .GM-csdn-content{
  147. //max-width: 5.58rem;
  148. overflow: hidden;
  149. text-overflow: ellipsis;
  150. display: -webkit-box;
  151. -webkit-line-clamp: 1;
  152. -webkit-box-orient: vertical;
  153. color: #555666;
  154. font-size: .24rem;
  155. line-height: .34rem;
  156. max-height: .34rem;
  157. word-break: break-all;
  158. -webkit-box-flex: 1;
  159. -ms-flex: 1;
  160. flex: 1;
  161. margin-top: .16rem;
  162. }
  163. .GM-csdn-img img{
  164. width: 2.18rem;
  165. height: 1.58rem;
  166. //margin-left: .16rem
  167. }
  168. .GM-csdn-Redirect{
  169. color: #fff;
  170. background-color: #f90707;
  171. font-family: sans-serif;
  172. margin: auto 2px;
  173. border: 1px solid #ccc;
  174. border-radius: 4px;
  175. padding: 0px 3px;
  176. font-size: xx-small;
  177. display: inline;
  178. white-space: nowrap;
  179. }
  180. .component-box .praise {
  181. background: #ff5722;
  182. border-radius: 5px;
  183. padding: 0px 8px;
  184. height: auto;
  185. }
  186. .component-box .praise,.component-box .share {
  187. color: #fff;
  188. }
  189. .component-box a {
  190. display: inline-block;
  191. font-size:xx-small;
  192. }
  193. .component-box {
  194. display: inline;
  195. margin: 0;
  196. position: relative;
  197. white-space:nowrap;
  198. }
  199. .csdn-edu-title{
  200. background: #4d6de1;
  201. border-radius: 5px;
  202. padding: 0px 8px;
  203. height: auto;
  204. color: #fff !important;
  205. }
  206. #comment{
  207. max-height: none !important;
  208. }
  209. .comment_read_more_box,
  210. #content_views pre.set-code-hide .hide-preCode-box,
  211. .passport-login-container,
  212. .hljs-button[data-title='登录后复制']
  213. {
  214. display:none !important;
  215. }
  216. #content_views pre,
  217. #content_views pre code{
  218. webkit-touch-callout: text !important;
  219. -webkit-user-select: text !important;
  220. -khtml-user-select: text !important;
  221. -moz-user-select: text !important;
  222. -ms-user-select: text !important;
  223. user-select: text !important;
  224. }
  225. #content_views pre.set-code-hide{
  226. height: 100% !important;
  227. overflow-y: auto !important;
  228. }
  229. `)
  230. function replace_all_commend() { //替换所有的推荐
  231. var commend_list = $(".container-fluid");
  232. for (var coml = 0; coml < commend_list.length; coml++) {
  233. let current_commend_className = commend_list[coml].getAttribute("class");
  234. let current_commend_url = "";
  235. let current_commend_title = "";
  236. let current_commend_content = "";
  237. let current_commend_img = "";
  238. if (commend_list[coml].getAttribute("data-url")) {
  239. //有data-url 就有recommend_title
  240. /* console.log("有 data-url"); */
  241. current_commend_url = commend_list[coml].getAttribute("data-url");
  242. current_commend_title = $(commend_list[coml]).find(".recommend_title").html();
  243. current_commend_content = $(commend_list[coml]).find(".text").html();
  244. // current_commend_title = commend_list[coml].getElementsByClassName("recommend_title")[0].innerHTML;
  245. // current_commend_content = commend_list[coml].getElementsByClassName("text active")[0].innerHTML;
  246. let current_commend_img_dom = $(commend_list[coml]).find(".recommend-img");
  247. if (current_commend_img_dom.length) {
  248. for (var imgs = 0; imgs < current_commend_img_dom.length; imgs++) {
  249. current_commend_img = current_commend_img + current_commend_img_dom[imgs].innerHTML;
  250. }
  251. }
  252.  
  253.  
  254. } else {
  255. /* console.log("没有data-url"); */
  256. current_commend_url = commend_list[coml].getElementsByTagName("a")[0].href;
  257. current_commend_title = commend_list[coml].getElementsByTagName("a")[0].innerHTML;
  258. current_commend_content = commend_list[coml].getElementsByClassName("text")[0].innerHTML;
  259. current_commend_img = "";
  260. }
  261. /* console.log("真实url:", current_commend_url); */
  262. if (GM_menus[0][2]) {
  263. current_commend_title = current_commend_title + `<div class="GM-csdn-Redirect">Redirect</div>`;
  264. }
  265. if (current_commend_url.match(/http(s|):\/\/(download.csdn.net|www.iteye.com\/resource)/g)) {
  266. console.log("该链接为csdn资源下载,标识");
  267. current_commend_title = current_commend_title + `<div class="component-box"><a class="praise" href="javascript:;">CSDN下载</a></div>`;
  268. } else if (current_commend_url.match(/edu.csdn.net/g)) {
  269. console.log("该链接为csdn学院下载,标识");
  270. current_commend_title = current_commend_title + `<div class="component-box"><a class="csdn-edu-title" href="javascript:;">CSDN学院</a></div>`;
  271. }
  272. commend_list[coml].className = "GM-csdn-dl";
  273. commend_list[coml].setAttribute("data-url", current_commend_url);
  274. commend_list[coml].innerHTML =
  275. `<div class="GM-csdn-title">` + current_commend_title + "</div>" +
  276. `<div class="GM-csdn-content">` + current_commend_content + "</div>" +
  277. `<div class="GM-csdn-img">` + current_commend_img + "</div>"
  278. $("#recommend").find(".recommend_list").before($("#first_recommend_list").find("dl").parent().html())
  279. $("#first_recommend_list")?.remove();
  280.  
  281. }
  282. }
  283.  
  284. function new_commend_event() { //新的推荐跳转事件
  285. $(".GM-csdn-dl").bind("click", function (e) {
  286. let current_click_url = e.currentTarget.dataset.url;
  287. if (GM_menus[1][2]) {
  288. window.open(current_click_url);
  289. } else {
  290. window.location.href = current_click_url;
  291. }
  292.  
  293. })
  294. }
  295.  
  296. function auto_review() {
  297. //自动展开
  298. $(".article_content")?.removeAttr("style");
  299. $(".readall_box")?.show();
  300. $(".readall_box")?.addClass("readall_box_nobg");
  301. $(".readall_box")?.hide();
  302. $(".readall_box")?.addClass("readall_box_nobg");
  303. $(".detail-open-app-isshow")?.css("display", "block");
  304. $(".isshow-mask-lock-box")?.show();
  305. }
  306. function removeElement(){
  307. /* 移除一些元素 */
  308. $(".passport-login-container")?.remove();
  309. }
  310.  
  311. $(document).ready(function () {
  312. console.log("mobile csdn");
  313. var csdn_interval_runum = 0;
  314. var csdn_interval = setInterval(function () {
  315. csdn_interval_runum = csdn_interval_runum + 1;
  316. console.log("展开");
  317. if (csdn_interval_runum <= 5) {
  318. auto_review();
  319. removeElement();
  320. } else {
  321. clearInterval(csdn_interval)
  322. }
  323.  
  324. }, 200)
  325. var loding_comment_dom_num = 0
  326. var loding_comment_dom = setInterval(function () {
  327. loding_comment_dom_num = loding_comment_dom_num + 1;
  328. if (loding_comment_dom_num <= 5) {
  329. try {
  330. replace_all_commend();
  331. } catch (err) {
  332. console.log("替换底部链接失败", err);
  333. }
  334.  
  335. } else {
  336. clearInterval(loding_comment_dom);
  337. new_commend_event();
  338. }
  339. }, 500)
  340.  
  341.  
  342. });
  343. } else if (isJianShu()) {
  344. console.log("简书");
  345. GM_addStyle(`
  346. .download-app-guidance,
  347. .call-app-btn,
  348. .collapse-tips,
  349. .note-graceful-button,
  350. .app-open,
  351. .header-wrap,
  352. .recommend-wrap.recommend-ad,
  353. .call-app-Ad-bottom,
  354. #recommended-notes p.top-title span.more,
  355. #homepage .modal,
  356. button.index_call-app-btn,
  357. span.note__flow__download,
  358. .download-guide,
  359. #footer,
  360. .comment-open-app-btn-wrap{
  361. display:none !important;
  362. }
  363. body.reader-day-mode.normal-size {
  364. overflow: auto !important;
  365. }
  366. .collapse-free-content{
  367. height:auto !important;
  368. }
  369. .copyright{
  370. color:#000 !important;
  371. }
  372. #note-show .content .show-content-free .collapse-free-content:after{
  373. background-image:none !important;
  374. }
  375. `);
  376. var MutationObserver = window.MutationObserver || window.webkitMutationObserver || window.MozMutationObserver;
  377. var mutationObserver = new MutationObserver(async function (mutations) {
  378. if(mutations.length == 0){return}
  379. if( mutations[0].target.style["display"] != "none"){
  380. document.querySelector('div#homepage div[class*="dialog-"] .cancel')?.click();
  381. }
  382. })
  383. var intervalNum = 0;
  384. var interval = setInterval(()=>{
  385. if(intervalNum > 30 || document.querySelector('div#homepage div[class*="dialog-"]')){
  386. document.querySelector('div#homepage div[class*="dialog-"]').style["visibility"]="hidden";
  387. mutationObserver.observe(document.querySelector('div#homepage div[class*="dialog-"]'), {
  388. /* 子节点的变动(新增、删除或者更改) */
  389. childList: false,
  390. /* 属性的变动 */
  391. attributes: true,
  392. /* 节点内容或节点文本的变动 */
  393. characterData: true,
  394. /* 是否将观察器应用于该节点的所有后代节点 */
  395. subtree: true
  396. });
  397. clearInterval(interval);
  398. };
  399. intervalNum++;
  400. },500);
  401. }
  402. }
  403. })();