csdn_optimizer

make csdn better

当前为 2021-10-13 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name csdn_optimizer
  3. // @namespace https://github.com/Kyouichirou
  4. // @version 1.4
  5. // @description make csdn better
  6. // @author HLA
  7. // @include /https:\/\/(\w+\.)?\w+\.csdn\.net.*/
  8. // @grant unsafeWindow
  9. // @grant GM_setValue
  10. // @grant GM_getValue
  11. // @grant GM_addStyle
  12. // @grant GM_registerMenuCommand
  13. // @grant GM_unregisterMenuCommand
  14. // @grant GM_setClipboard
  15. // @grant GM_notification
  16. // @grant GM_openInTab
  17. // @run-at document-start
  18. // @license MIT
  19. // @compatiable chrome; just test on chrome 80+
  20. // @noframes
  21. // @icon https://blog.csdn.net/favicon.ico
  22. // ==/UserScript==
  23.  
  24. (() => {
  25. "use strict";
  26. const Notification = (
  27. content = "",
  28. title = "",
  29. duration = 2500,
  30. cfunc,
  31. ofunc
  32. ) => {
  33. GM_notification({
  34. text: content,
  35. title: title,
  36. timeout: duration,
  37. onclick: cfunc,
  38. ondone: ofunc,
  39. });
  40. };
  41. const escapeHTML_Blank = (html) => html.replace(/\s/g, " ");
  42. const CSDN = {
  43. input: {
  44. get article() {
  45. const bgp = `data:image/webp;base64,UklGRlgRAABXRUJQVlA4TEwRAAAvK8FKAM9gkG2kvb/nW/xXg0zaJv717l4NpG1T/+au6Nn8x+6Cc6g3Ek7qAWjCS6gBmOdBwgh8A8ZR0LYN4/Kn3R0IETEBSH9Blha/Fo8827YlSZIkSQ8AoXkA2AB2RMT4/5/qpKYsqgMd+rkR/Wfkto2jnQ+MthdBe3fcSJIiafcY/TeZL3Svroro/x86VsvMI29U6VgsXgbH8l4lPAJ51XOGWX7CZwtkAtR54Eqa5r1FbR6lUS21RW/l4ONp5muy6PNlcX1mN3Kc76kHXTVsRzQPmfH4rlmBFItmySjhUIcaXZIxj06gkYJLGhcBSINZxiKV4Nl62CwuX69hdEhRZVddn31WlNqWg2/vYbUn3I4tAK61qg9SlFTH1WM+XlWKa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2tuabHaNVfJQf0Zsr5HGXM+1xCpJc4p6/z5EhHH9v///XBv/z7U/5lwb/8+1vDzqGv/PtSUZ/8+18f9cG//PtfH/XBv/z7XvOtfG/3Pt+HLX+H+ujf/n2vh/ro3/59r4f64RFwE=`;
  46. return `
  47. div#article_content{
  48. height: auto !important;
  49. }
  50. .article-header-box {background-color: transparent !important;}
  51. .blog-content-box {background-image: url(${bgp}) !important;}
  52. .comment-box .comment-list-container .comment-list-box {
  53. overflow: auto !important;
  54. max-height: none !important;
  55. }
  56. #content_views pre code,
  57. #content_views pre{user-select: unset !important;}
  58. #content_views span {background-color: transparent !important;}
  59. .toolbar-advert,
  60. div.hide-article-box,
  61. div#recommendAdBox,
  62. .hljs-button.signin,
  63. #blogColumnPayAdvert,
  64. .opt-box.text-center,
  65. img.comment-sofa-flag,
  66. .recommend-item-box.recommend-download-box.clearfix,
  67. .recommend-item-box.type_download.clearfix,
  68. .recommend-item-box.type_course.clearfix,
  69. .recommend-item-box.recommend-other-item-box.type_download,
  70. .recommend-item-box.recommend-other-item-box.type_discussion_topic,
  71. .recommend-item-box.recommend-other-item-box.type_course,
  72. .recommend-item-box.type_download.clearfix,
  73. .recommend-item-box.baiduSearch,`;
  74. },
  75. placeholder(mode) {
  76. GM_addStyle(`
  77. input::-webkit-input-placeholder {
  78. font-size: 0px;
  79. text-align: right;
  80. }
  81. ${mode ? this.article : ""}
  82. .login-box,
  83. .login-mark,
  84. .passport-login-container,
  85. .csdn-side-toolbar,
  86. .toolbar-search-hot {
  87. display: none !important;
  88. }
  89. `);
  90. },
  91. dropmenu() {
  92. let input = document.getElementsByClassName(
  93. "toolbar-search onlySearch"
  94. );
  95. if (input.length > 0) {
  96. const m = new MutationObserver((records) => {
  97. for (const e of records) {
  98. if (e.addedNodes.length > 0) {
  99. for (const a of e.addedNodes) {
  100. if (
  101. a.className ===
  102. "toolbar-search-drop-menu "
  103. ) {
  104. a.remove();
  105. break;
  106. }
  107. }
  108. break;
  109. }
  110. }
  111. });
  112. m.observe(input[0], { childList: true });
  113. unsafeWindow.addEventListener(
  114. "visibilitychange",
  115. (e) => {
  116. e.preventDefault();
  117. e.stopPropagation();
  118. },
  119. true
  120. );
  121. let boxs = input[0].getElementsByTagName("input");
  122. let buttons = input[0].getElementsByTagName("button");
  123. if (boxs.length > 0 && buttons.length > 0) {
  124. const box = boxs[0];
  125. buttons[0].addEventListener(
  126. "click",
  127. (e) => {
  128. if (!box.value) {
  129. e.preventDefault();
  130. e.stopImmediatePropagation();
  131. }
  132. },
  133. true
  134. );
  135. box.addEventListener(
  136. "keydown",
  137. (e) => {
  138. if (!box.value) {
  139. e.preventDefault();
  140. e.stopImmediatePropagation();
  141. }
  142. },
  143. true
  144. );
  145. buttons = null;
  146. boxs = null;
  147. }
  148. input = null;
  149. }
  150. },
  151. },
  152. antiRedirect() {
  153. const links = Object.getOwnPropertyDescriptors(
  154. HTMLAnchorElement.prototype
  155. ).href;
  156. const trackids = ["?utm_", "utm_source", "?ops_request"];
  157. Object.defineProperty(HTMLAnchorElement.prototype, "href", {
  158. ...links,
  159. get() {
  160. const href = decodeURIComponent(links.get.call(this));
  161. for (const id of trackids) {
  162. const tmp = href.split(id);
  163. if (tmp.length > 1) {
  164. this.href = tmp[1];
  165. return tmp[1];
  166. }
  167. }
  168. return href;
  169. },
  170. });
  171. },
  172. antiLeech() {
  173. // if those pictures are not from csdn
  174. const content = document.getElementById("content_views");
  175. if (!content) return;
  176. const imgs = content.getElementsByTagName("img");
  177. const hosts = ["csdnimg.cn", "csdn.net"];
  178. for (const img of imgs) {
  179. const src = img.src;
  180. if (src && !hosts.some((e) => src.includes(e))) {
  181. img.setAttribute("referrerPolicy", "no-referrer");
  182. img.src = img.src + "?";
  183. }
  184. }
  185. },
  186. comment: {
  187. // covert the link of text to href
  188. textTolink(text) {
  189. const reg =
  190. /((http|https):\/\/[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|])/g;
  191. return reg.test(text)
  192. ? text.replace(
  193. reg,
  194. "<a href='$1' target='_blank' style='color: #00a0e9;width: 360px;display: inline;'>$1</a>"
  195. )
  196. : null;
  197. },
  198. text_convertor() {
  199. const snc = document.getElementsByClassName("new-comment");
  200. for (const e of snc) {
  201. if (!e.innerHTML.includes("href")) {
  202. const s = this.textTolink(e.textContent);
  203. s && (e.innerHTML = s);
  204. }
  205. }
  206. },
  207. },
  208. menus: {
  209. s_id: null,
  210. r_id: null,
  211. n_id: null,
  212. reader_style: null,
  213. simple_style: null,
  214. create_simple_style(m = true) {
  215. if (this.simple_style) return;
  216. this.reset_reader();
  217. let i = 7;
  218. let t = "";
  219. const none_display = `
  220. div#asideCustom,
  221. div#asideNewComments,
  222. div#asideNewNps{display: none !important;}`;
  223. const sider_bar = `
  224. aside.blog_container_aside,
  225. div#rightAside{opacity: 0.1;}
  226. aside.blog_container_aside:hover{
  227. opacity: 1;
  228. transition: opacity 3s;
  229. }
  230. div#rightAside:hover{
  231. opacity: 1;
  232. transition: opacity 3s;
  233. }`;
  234. for (i; i > 1; i--)
  235. t += `.csdn-toolbar-fl.toolbar-menus > li:nth-of-type(${i}),`;
  236. this.simple_style = GM_addStyle(
  237. t +
  238. ".toolbar-btn.toolbar-btn-vip.csdn-toolbar-fl {visibility: hidden !important;}" +
  239. none_display +
  240. (m ? sider_bar : "")
  241. );
  242. },
  243. create_reader_style() {
  244. if (this.reader_style) return;
  245. this.reset_simple();
  246. const sty = `
  247. #csdn-toolbar,
  248. aside.blog_container_aside,
  249. .aside-box.kind_person.d-flex.flex-column,
  250. .comment-box,
  251. .first-recommend-box,
  252. .second-recommend-box,
  253. .recommend-box.insert-baidu-box,
  254. .recommend-tit-mod {display: none ! important;}
  255. `;
  256. this.reader_style = GM_addStyle(sty);
  257. },
  258. simple_action() {
  259. if (this.s_id) {
  260. GM_unregisterMenuCommand(this.s_id);
  261. this.s_id = null;
  262. }
  263. GM_setValue("mode", "s");
  264. this.create_simple_style();
  265. this.reader();
  266. this.normal();
  267. },
  268. reader_action() {
  269. if (this.r_id) {
  270. GM_unregisterMenuCommand(this.r_id);
  271. this.r_id = null;
  272. }
  273. GM_setValue("mode", "r");
  274. this.create_reader_style();
  275. this.simple();
  276. this.normal();
  277. },
  278. reset_simple() {
  279. if (this.simple_style) {
  280. this.simple_style.remove();
  281. this.simple_style = null;
  282. }
  283. },
  284. reset_reader() {
  285. if (this.reader_style) {
  286. this.reader_style.remove();
  287. this.reader_style = null;
  288. }
  289. },
  290. normal_action() {
  291. if (this.n_id) {
  292. GM_unregisterMenuCommand(this.n_id);
  293. this.n_id = null;
  294. }
  295. GM_setValue("mode", "");
  296. this.reset_reader();
  297. this.reset_simple();
  298. this.simple();
  299. this.reader();
  300. },
  301. simple() {
  302. !this.s_id &&
  303. (this.s_id = GM_registerMenuCommand(
  304. "Simple Mode",
  305. this.simple_action.bind(this)
  306. ));
  307. },
  308. reader() {
  309. !this.r_id &&
  310. (this.r_id = GM_registerMenuCommand(
  311. "Reader Mode",
  312. this.reader_action.bind(this)
  313. ));
  314. },
  315. normal() {
  316. !this.n_id &&
  317. (this.n_id = GM_registerMenuCommand(
  318. "Normal Mode",
  319. this.normal_action.bind(this)
  320. ));
  321. },
  322. create(m) {
  323. const mode = GM_getValue("mode");
  324. if (!m && mode) {
  325. this.create_simple_style(m);
  326. return;
  327. }
  328. if (mode) {
  329. if (mode === "r") {
  330. this.simple();
  331. this.create_reader_style();
  332. } else {
  333. this.reader();
  334. this.create_simple_style();
  335. }
  336. this.normal();
  337. } else {
  338. this.simple();
  339. this.reader();
  340. }
  341. },
  342. },
  343. code: {
  344. // ctrl + right mouse => copy the code directly
  345. clipboard(text) {
  346. if (!text) {
  347. Notification("failed to get the content of code");
  348. return;
  349. }
  350. try {
  351. try {
  352. navigator.clipboard.writeText(text);
  353. } catch (err) {
  354. console.log(err);
  355. GM_setClipboard(text, "text");
  356. }
  357. Notification("the code has been copied to clipboard");
  358. } catch (err) {
  359. console.log(err);
  360. Notification("some error on copy the code");
  361. }
  362. },
  363. get_code(node) {
  364. const num = node.getElementsByClassName("pre-numbering");
  365. if (num.length > 0) {
  366. let text = "";
  367. for (const e of node.childNodes) {
  368. if (e.className === "pre-numbering") continue;
  369. text += e.innerText;
  370. }
  371. return text;
  372. }
  373. return node.innerText;
  374. },
  375. event() {
  376. const pres = document.getElementsByTagName("pre");
  377. for (const pre of pres)
  378. pre.title = "Ctrl + Right mouse button to copy this code";
  379. pres.length > 0 &&
  380. document.addEventListener("contextmenu", (e) => {
  381. if (e.ctrlKey) {
  382. for (const p of e.path) {
  383. if (p.localName === "pre") {
  384. e.preventDefault();
  385. e.stopImmediatePropagation();
  386. const text = this.get_code(p);
  387. this.clipboard(text);
  388. break;
  389. }
  390. }
  391. }
  392. });
  393. },
  394. },
  395. anti_prevent_copy() {
  396. // disable initialization of csdn copy event(this feature will add copyrigth content to the data of copy)
  397. unsafeWindow.csdn = {};
  398. Object.defineProperty(unsafeWindow.csdn, "copyright", {
  399. value: null,
  400. });
  401. },
  402. anti_click_redirect() {
  403. // anti track what you click
  404. document.addEventListener(
  405. "click",
  406. (e) => {
  407. let ic = 0;
  408. for (const a of e.path) {
  409. if (a.localName === "a") {
  410. e.preventDefault();
  411. e.stopImmediatePropagation();
  412. const href = a.href;
  413. if (href) {
  414. const trackids = [
  415. "?utm_",
  416. "utm_source",
  417. "?ops_request",
  418. ];
  419. const index = trackids.findIndex((e) =>
  420. href.includes(e)
  421. );
  422. GM_openInTab(
  423. index < 0 ? href : href.slice(0, index),
  424. {
  425. insert: true,
  426. active: true,
  427. }
  428. );
  429. }
  430. break;
  431. } else if (ic > 2) break;
  432. ic++;
  433. }
  434. },
  435. true
  436. );
  437. },
  438. clear_bottom() {
  439. // clear the bottom zone that displays some relative articles
  440. const b = document.getElementsByClassName(
  441. "recommend-box insert-baidu-box"
  442. );
  443. const rubbish = [
  444. "\u534e\u4e3a",
  445. "CSDN\u8d44\u8baf",
  446. "CSDNnew",
  447. "\u5927\u5b66\u751f",
  448. "\u5e94\u5c4a\u751f",
  449. "\u6bd5\u4e1a\u751f",
  450. "\u79c1\u6d3b",
  451. "\u6708\u85aa",
  452. "\u5e74\u85aa",
  453. "\u8df3\u69fd",
  454. "\u5de5\u8d44",
  455. "\u4e8b\u4e1a",
  456. "\u5de5\u7a0b\u5e08",
  457. "\u85aa\u8d44",
  458. "\u517c\u804c",
  459. "\u7c89\u4e1d",
  460. "\u627e\u5de5\u4f5c",
  461. "\u7b80\u5386",
  462. "\u9762\u8bd5",
  463. "\u540c\u4e8b",
  464. "\u4f17\u5305",
  465. "HR",
  466. "\u85aa\u6c34",
  467. "\u5916\u5305",
  468. "\u79bb\u804c",
  469. "\u8fbe\u5185",
  470. ];
  471. if (b.length > 0) {
  472. const items = b[0].getElementsByClassName(
  473. "recommend-item-box type_blog clearfix"
  474. );
  475. let i = items.length;
  476. const trackids = ["?utm_", "utm_source", "?ops_request"];
  477. for (i; i--; ) {
  478. const text = items[i].innerText;
  479. if (text && rubbish.some((e) => text.includes(e))) {
  480. items[i].remove();
  481. continue;
  482. }
  483. const links = items[i].getElementsByTagName("a");
  484. let fr = false;
  485. for (const a of links) {
  486. let href = a.href;
  487. if (href && href.startsWith("http")) {
  488. const index = trackids.findIndex((e) =>
  489. href.includes(e)
  490. );
  491. index > 0 && (href = href.slice(0, index));
  492. if (href.includes("/article/") || !this.blackLists)
  493. continue;
  494. const i = href.lastIndexOf("/");
  495. if (i > 0) {
  496. const id = href.slice(i + 1);
  497. if (this.blackLists.includes(id)) {
  498. fr = true;
  499. break;
  500. }
  501. }
  502. index > 0 && (a.href = href);
  503. }
  504. }
  505. fr && items[i].remove();
  506. }
  507. }
  508. this.blackLists = null;
  509. },
  510. user_manage: {
  511. // block some rubbish author
  512. create_button(mode) {
  513. const postion = document.getElementsByClassName(
  514. "user-profile-operate-btn"
  515. );
  516. const [color, name, title] = mode
  517. ? ["black", "unBlock", "unblock this author"]
  518. : ["red", "Block", "block this author"];
  519. if (postion.length === 0) {
  520. const button = `
  521. <button
  522. style="
  523. margin-left: 158px;
  524. width: 65px;
  525. opacity: 0.95;
  526. font-size: 14px;
  527. line-height: 20px;
  528. text-align: center;
  529. cursor: pointer;
  530. color: ${color};
  531. background: none;
  532. border: 1px solid;
  533. border-radius: 3px;
  534. "
  535. title=${escapeHTML_Blank(title)}
  536. >
  537. ${name}
  538. </button>`;
  539. const user = document.getElementsByClassName(
  540. "user-info d-flex flex-column profile-intro-name-box"
  541. );
  542. if (user.length === 0)
  543. console.log("failed to get the postion of button");
  544. else {
  545. user[0].insertAdjacentHTML("beforeend", button);
  546. setTimeout(
  547. () =>
  548. (user[0].lastElementChild.onclick = (e) =>
  549. this.click_event(e.target)),
  550. 250
  551. );
  552. }
  553. } else {
  554. const button = `
  555. <button class="block" style="
  556. width: 92px;
  557. height: 32px;
  558. color: ${color};
  559. font-size: 15px;
  560. cursor: pointer;
  561. border-radius: 20px;
  562. opacity: 0.95;
  563. border: 1px solid #999aaa;
  564. " title=${escapeHTML_Blank(title)}>${name}</button>`;
  565. postion[0].insertAdjacentHTML("afterbegin", button);
  566. setTimeout(
  567. () =>
  568. (postion[0].firstElementChild.onclick = (e) =>
  569. this.click_event(e.target)),
  570. 350
  571. );
  572. }
  573. },
  574. click_event(target) {
  575. debugger;
  576. let blackLists = GM_getValue("block");
  577. if (target.innerText === "Block") {
  578. if (blackLists) {
  579. if (!blackLists.includes(this.id)) {
  580. blackLists.push(this.id);
  581. GM_setValue("block", blackLists);
  582. }
  583. } else {
  584. blackLists = [];
  585. blackLists.push(this.id);
  586. GM_setValue("block", blackLists);
  587. }
  588. target.innerText = "unBlock";
  589. target.title = "unblock this author";
  590. target.style.color = "black";
  591. } else {
  592. if (blackLists) {
  593. const index = blackLists.indexOf(this.id);
  594. if (index > -1) {
  595. blackLists.splice(index, 1);
  596. GM_setValue("block", blackLists);
  597. }
  598. }
  599. target.innerText = "Block";
  600. target.title = "block this author";
  601. target.style.color = "red";
  602. }
  603. },
  604. id: null,
  605. main(mode, id) {
  606. this.create_button(mode);
  607. this.id = id;
  608. },
  609. },
  610. blackLists: null,
  611. get_block() {
  612. this.blackLists = GM_getValue("block");
  613. },
  614. start() {
  615. const href = location.href;
  616. const f = href.includes("/article/");
  617. this.antiRedirect();
  618. this.input.placeholder(f);
  619. this.anti_prevent_copy();
  620. //unsafewindow, some page can not capture the event.
  621. window.onload = () => {
  622. if (f) {
  623. this.get_block();
  624. this.antiLeech();
  625. this.code.event();
  626. this.anti_click_redirect();
  627. this.comment.text_convertor();
  628. setTimeout(() => this.clear_bottom(), 330);
  629. } else {
  630. const regs = [
  631. /https:\/\/blog\.csdn\.net\/(\w+)($|\/|\/category\w+\.html|\?type=[a-z]+)/,
  632. /https:\/\/(\w+)\.blog\.csdn\.net(\/|$)/,
  633. ];
  634. let id = null;
  635. for (const reg of regs) {
  636. const ms = href.match(reg);
  637. if (ms && ms.length > 1) {
  638. id = ms[1];
  639. break;
  640. }
  641. }
  642. if (id) {
  643. this.get_block();
  644. const mode =
  645. this.blackLists && this.blackLists.includes(id);
  646. this.user_manage.main(mode, id);
  647. this.blackLists = null;
  648. this.anti_click_redirect();
  649. } else if (href.endsWith(".net/"))
  650. this.anti_click_redirect();
  651. }
  652. this.input.dropmenu();
  653. };
  654. this.menus.create(f);
  655. },
  656. };
  657. CSDN.start();
  658. })();