csdn_optimizer

make csdn better

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

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