CSDN|简书优化

支持手机端和PC端,屏蔽广告,优化浏览体验,自动跳转简书拦截URL

当前为 2023-11-25 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name CSDN|简书优化
  3. // @icon https://www.csdn.net/favicon.ico
  4. // @namespace https://greasyfork.org/zh-CN/scripts/406136
  5. // @supportURL https://github.com/WhiteSevs/TamperMonkeyScript/issues
  6. // @version 2023.11.16
  7. // @description 支持手机端和PC端,屏蔽广告,优化浏览体验,自动跳转简书拦截URL
  8. // @author WhiteSevs
  9. // @match *://*.csdn.net/*
  10. // @match *://*.jianshu.com/*
  11. // @match *://*.jianshu.io/*
  12. // @grant GM_registerMenuCommand
  13. // @grant GM_unregisterMenuCommand
  14. // @grant GM_getValue
  15. // @grant GM_setValue
  16. // @grant GM_deleteValue
  17. // @grant GM_listValues
  18. // @grant GM_info
  19. // @grant unsafeWindow
  20. // @run-at document-start
  21. // @require https://update.greasyfork.org/scripts/449471/1249086/Viewer.js
  22. // @require https://update.greasyfork.org/scripts/455186/1285083/WhiteSevsUtils.js
  23. // @require https://update.greasyfork.org/scripts/465772/1274595/DOMUtils.js
  24. // ==/UserScript==
  25.  
  26. (function () {
  27. /**
  28. * @type {import("../库/Utils")}
  29. */
  30. const utils = window.Utils.noConflict();
  31. /**
  32. * @type {import("../库/DOMUtils")}
  33. */
  34. const DOMUtils = window.DOMUtils.noConflict();
  35. const log = new utils.Log(GM_info);
  36. log.config({
  37. logMaxCount: 20,
  38. autoClearConsole: true,
  39. });
  40. /**
  41. * 因为在有些页面上,比如:简书,当插入style元素到head中,该页面清除该元素
  42. */
  43. const GM_addStyle = utils.GM_addStyle;
  44. /**
  45. * 菜单
  46. */
  47. let GM_Menu = new utils.GM_Menu({
  48. GM_getValue,
  49. GM_setValue,
  50. GM_registerMenuCommand,
  51. GM_unregisterMenuCommand,
  52. });
  53. /**
  54. * 移除元素(未出现也可以等待出现)
  55. * @param {string} selectorText 元素选择器
  56. */
  57. const waitForElementToRemove = function (selectorText = "") {
  58. utils.waitNodeList(selectorText).then((nodeList) => {
  59. nodeList.forEach((item) => {
  60. item.remove();
  61. });
  62. });
  63. };
  64.  
  65. const Optimization = {
  66. jianshu: {
  67. /**
  68. * 判断是否是简书
  69. */
  70. locationMatch() {
  71. return Boolean(/jianshu.(com|io)/i.test(window.location.origin));
  72. },
  73. PC: {
  74. /**
  75. * 添加屏蔽CSS
  76. */
  77. addCSS() {
  78. GM_addStyle(`
  79. .download-app-guidance,
  80. .call-app-btn,
  81. .collapse-tips,
  82. .note-graceful-button,
  83. .app-open,
  84. .header-wrap,
  85. .recommend-wrap.recommend-ad,
  86. .call-app-Ad-bottom,
  87. #recommended-notes p.top-title span.more,
  88. #homepage .modal,
  89. button.index_call-app-btn,
  90. span.note__flow__download,
  91. .download-guide,
  92. #footer,
  93. .comment-open-app-btn-wrap,
  94. .nav.navbar-nav + div,
  95. .self-flow-ad,
  96. #free-reward-panel,
  97. div[id*='AdFive'],
  98. #index-aside-download-qrbox,
  99. .baidu-app-download-2eIkf_1,
  100. /* 底部的"小礼物走一走,来简书关注我"、赞赏支持和更多精彩内容,就在简书APP */
  101. div[role="main"] > div > section:first-child > div:nth-last-child(2){
  102. display:none !important;
  103. }
  104. body.reader-day-mode.normal-size {
  105. overflow: auto !important;
  106. }
  107. .collapse-free-content{
  108. height:auto !important;
  109. }
  110. .copyright{
  111. color:#000 !important;
  112. }
  113. #note-show .content .show-content-free .collapse-free-content:after{
  114. background-image:none !important;
  115. }
  116. footer > div > div{
  117. justify-content: center;
  118. }
  119. /* 修复底部最后编辑于:。。。在某些套壳浏览器上的错位问题 */
  120. #note-show .content .show-content-free .note-meta-time{
  121. margin-top: 0px !important;
  122. }
  123. `);
  124. },
  125. /**
  126. * 全文居中
  127. */
  128. articleCenter() {
  129. log.success("全文居中");
  130. GM_addStyle(`
  131. div[role=main] aside,
  132. div._3Pnjry{
  133. display: none !important;
  134. }
  135. div._gp-ck{
  136. width: 100% !important;
  137. }`);
  138. waitForElementToRemove("div[role=main] aside");
  139. waitForElementToRemove("div._3Pnjry");
  140. utils.waitNodeList("div._gp-ck").then((nodeList) => {
  141. nodeList.forEach((item) => {
  142. item.style["width"] = "100%";
  143. });
  144. });
  145. },
  146. /**
  147. * 去除剪贴板劫持
  148. */
  149. removeClipboardHijacking() {
  150. log.success("去除剪贴板劫持");
  151. const stopNativePropagation = (event) => {
  152. event.stopPropagation();
  153. };
  154. window.addEventListener("copy", stopNativePropagation, true);
  155. document.addEventListener("copy", stopNativePropagation, true);
  156. },
  157. /**
  158. * 自动展开全文
  159. */
  160. autoExpandFullText() {
  161. utils
  162. .waitNode(`div#homepage div[class*="dialog-"]`)
  163. .then((element) => {
  164. element.style["visibility"] = "hidden";
  165. utils.mutationObserver(element, {
  166. callback: (mutations) => {
  167. if (mutations.length == 0) {
  168. return;
  169. }
  170. if (mutations.target.style["display"] != "none") {
  171. log.success("自动展开全文");
  172. document
  173. .querySelector(
  174. 'div#homepage div[class*="dialog-"] .cancel'
  175. )
  176. ?.click();
  177. }
  178. },
  179. config: {
  180. /* 子节点的变动(新增、删除或者更改) */
  181. childList: false,
  182. /* 属性的变动 */
  183. attributes: true,
  184. /* 节点内容或节点文本的变动 */
  185. characterData: true,
  186. /* 是否将观察器应用于该节点的所有后代节点 */
  187. subtree: true,
  188. },
  189. });
  190. });
  191. },
  192. /**
  193. * 去除简书拦截其它网址的url并自动跳转
  194. */
  195. jumpRedirect() {
  196. if (window.location.pathname === "/go-wild") {
  197. /* 禁止简书拦截跳转 */
  198. window.stop();
  199. let search = window.location.href.replace(
  200. window.location.origin + "/",
  201. ""
  202. );
  203. search = decodeURIComponent(search);
  204. let newURL = search
  205. .replace(/^go-wild\?ac=2&url=/gi, "")
  206. .replace(/^https:\/\/link.zhihu.com\/\?target\=/gi, "");
  207. window.location.href = newURL;
  208. }
  209. },
  210. /**
  211. * 屏蔽相关文章
  212. */
  213. shieldRelatedArticles() {
  214. log.success("屏蔽相关文章");
  215. GM_addStyle(`
  216. div[role="main"] > div > section:nth-child(2){
  217. display: none !important;
  218. }
  219. `);
  220. },
  221. /**
  222. * 屏蔽评论区
  223. */
  224. shieldUserComments() {
  225. log.success("屏蔽评论区");
  226. GM_addStyle(`
  227. div#note-page-comment{
  228. display: none !important;
  229. }
  230. `);
  231. },
  232. /**
  233. * 屏蔽推荐阅读
  234. */
  235. shieldRecommendedReading() {
  236. log.success("屏蔽推荐阅读");
  237. GM_addStyle(`
  238. div[role="main"] > div > section:last-child{
  239. display: none !important;
  240. }
  241. `);
  242. },
  243. run() {
  244. this.addCSS();
  245. this.removeClipboardHijacking();
  246. this.autoExpandFullText();
  247. if (GM_Menu.get("JianShuArticleCenter")) {
  248. this.articleCenter();
  249. }
  250. if (GM_Menu.get("JianShuShieldRelatedArticles")) {
  251. this.shieldRelatedArticles();
  252. }
  253. if (GM_Menu.get("JianShuShieldUserComments")) {
  254. this.shieldUserComments();
  255. }
  256. if (GM_Menu.get("JianShuShieldRecommendedReading")) {
  257. this.shieldRecommendedReading();
  258. }
  259. },
  260. },
  261. Mobile: {
  262. addCSS() {
  263. Optimization.jianshu.PC.addCSS();
  264. },
  265. /**
  266. * 手机-移除底部推荐阅读
  267. */
  268. removeFooterRecommendRead() {
  269. log.success("移除底部推荐阅读");
  270. GM_addStyle(`
  271. #recommended-notes{
  272. display: none !important;
  273. }`);
  274. },
  275. /**
  276. * 处理原型
  277. */
  278. handlePrototype() {
  279. log.success("处理原型添加script标签");
  280. let originalAppendChild = Node.prototype.appendChild;
  281. Node.prototype.appendChild = function (element) {
  282. /* 允许添加的元素localName */
  283. let allowElementLocalNameList = ["img"];
  284. /* 不允许script标签加载包括jianshu.io的js资源,会让简书跳到广告页面 */
  285. if (
  286. element.src &&
  287. !element.src.includes("jianshu.io") &&
  288. !allowElementLocalNameList.includes(element.localName)
  289. ) {
  290. log.success(["禁止添加的元素", element]);
  291. return null;
  292. } else {
  293. return originalAppendChild.call(this, element);
  294. }
  295. };
  296. },
  297. /**
  298. * 屏蔽评论区
  299. */
  300. shieldUserComments() {
  301. log.success("屏蔽评论区");
  302. GM_addStyle(`
  303. #comment-main{
  304. display: none !important;
  305. }
  306. `);
  307. },
  308. run() {
  309. this.handlePrototype();
  310. this.addCSS();
  311. Optimization.jianshu.PC.removeClipboardHijacking();
  312. Optimization.jianshu.PC.autoExpandFullText();
  313. if (GM_Menu.get("JianShuremoveFooterRecommendRead")) {
  314. this.removeFooterRecommendRead();
  315. }
  316. if (GM_Menu.get("JianShuShieldUserComments")) {
  317. this.shieldUserComments();
  318. }
  319. },
  320. },
  321. /**
  322. * 函数入口
  323. */
  324. run() {
  325. this.PC.jumpRedirect();
  326. if (utils.isPhone()) {
  327. log.success("简书-移动端");
  328. this.Mobile.run();
  329. } else {
  330. log.success("简书-桌面端");
  331. this.PC.run();
  332. }
  333. },
  334. },
  335. csdn: {
  336. /**
  337. * 判断是否是CSDN
  338. */
  339. locationMatch() {
  340. return Boolean(/csdn.net/i.test(window.location.origin));
  341. },
  342. PC: {
  343. addCSS() {
  344. GM_addStyle(`
  345. .ecommend-item-box.recommend-recommend-box,
  346. .login-mark,
  347. .opt-box.text-center,
  348. .leftPop,
  349. #csdn-shop-window,
  350. .toolbar-advert,
  351. .hide-article-box,
  352. .user-desc.user-desc-fix,
  353. .recommend-card-box,
  354. .more-article,
  355. .article-show-more,
  356. #csdn-toolbar-profile-nologin,
  357. .guide-rr-first,
  358. #recommend-item-box-tow,
  359. /* 发文章得原力分图片提示 */
  360. div.csdn-toolbar-creative-mp,
  361. /* 阅读终点,创作起航,您可以撰写心得或摘录文章要点写篇博文。 */
  362. #toolBarBox div.write-guide-buttom-box,
  363. /* 觉得还不错? 一键收藏 */
  364. ul.toolbox-list div.tool-active-list,
  365. /* 右边按钮组的最上面的创作话题 */
  366. div.csdn-side-toolbar .activity-swiper-box,
  367. .sidetool-writeguide-box .tip-box{
  368. display: none !important;
  369. }
  370. .comment-list-box,
  371. main div.blog-content-box pre{
  372. max-height: none !important;
  373. }
  374. .blog_container_aside,
  375. #nav{
  376. margin-left: -45px;
  377. }
  378. .recommend-right.align-items-stretch.clearfix,.dl_right_fixed{
  379. margin-left: 45px;
  380. }
  381. #content_views pre,
  382. #content_views pre code{
  383. user-select: text !important;
  384. }
  385. #article_content,
  386. .user-article.user-article-hide{
  387. height: auto !important;
  388. overflow: auto !important;
  389. }
  390. `);
  391. },
  392. /**
  393. * 添加在wenku.csdn.net下的CSS
  394. */
  395. addWenKuCSS() {
  396. GM_addStyle(`
  397. /* wenku顶部横幅 */
  398. #app > div > div.main.pb-32 > div > div.top-bar,
  399. /* 底部展开全文 */
  400. #chatgpt-article-detail > div.layout-center > div.main > div.article-box > div.cont.first-show.forbid > div.open{
  401. display: none !important;
  402. }
  403. #chatgpt-article-detail > div.layout-center > div.main > div.article-box > div.cont.first-show.forbid{
  404. max-height: unset !important;
  405. height: auto !important;
  406. overflow: auto !important;
  407. }
  408. `);
  409. GM_addStyle(`
  410. .forbid{
  411. user-select: text !important;
  412. }
  413. `);
  414. },
  415. /**
  416. * 去除剪贴板劫持
  417. */
  418. removeClipboardHijacking() {
  419. log.info("去除剪贴板劫持");
  420. document.querySelector(".article-copyright")?.remove();
  421. if (unsafeWindow.articleType) {
  422. unsafeWindow.articleType = 0;
  423. }
  424. if (
  425. unsafeWindow.csdn &&
  426. unsafeWindow.csdn.copyright &&
  427. unsafeWindow.csdn.copyright.textData
  428. ) {
  429. unsafeWindow.csdn.copyright.textData = "";
  430. }
  431. if (
  432. unsafeWindow.csdn &&
  433. unsafeWindow.csdn.copyright &&
  434. unsafeWindow.csdn.copyright.htmlData
  435. ) {
  436. unsafeWindow.csdn.copyright.htmlData = "";
  437. }
  438. },
  439. /**
  440. * 取消禁止复制
  441. */
  442. unBlockCopy() {
  443. log.info("取消禁止复制");
  444. document.addEventListener("click", function (event) {
  445. let target = event.target;
  446. if (!target.classList.contains("hljs-button")) {
  447. return;
  448. }
  449. utils.preventEvent(event);
  450. /* 需要复制的文本 */
  451. let copyText = target.parentElement.textContent;
  452. utils.setClip(copyText);
  453. log.success("点击复制 复制成功~");
  454. target.setAttribute("data-title", "复制成功");
  455. });
  456. let changeDataTitle = new utils.LockFunction(function (event) {
  457. let target = event.target;
  458. if (!target.localName === "pre") {
  459. return;
  460. }
  461. target
  462. .querySelector(".hljs-button")
  463. ?.setAttribute("data-title", "复制");
  464. });
  465.  
  466. document.addEventListener("mouseenter", changeDataTitle.run, true);
  467. document.addEventListener("mouseleave", changeDataTitle.run, true);
  468. /* 取消Ctrl+C的禁止 */
  469. utils.waitNode("#content_views").then((element) => {
  470. unsafeWindow?.$("#content_views")?.unbind("copy");
  471. element.addEventListener("copy", function (event) {
  472. utils.preventEvent(event);
  473. utils.setClip(unsafeWindow.getSelection().toString());
  474. log.success("Ctrl+C 复制成功~");
  475. return false;
  476. });
  477. });
  478. /* 删除所有复制按钮的原有的复制事件 */
  479. document.querySelectorAll(".hljs-button").forEach((item) => {
  480. item.removeAttribute("onclick");
  481. });
  482. },
  483. /**
  484. * 点击代码块自动展开
  485. */
  486. clickPreCodeAutomatically() {
  487. if (!GM_Menu.get("autoExpandContent")) {
  488. return;
  489. }
  490. log.info("点击代码块自动展开");
  491. document.addEventListener("click", function (event) {
  492. let target = event.target;
  493. if (target.localName !== "pre") {
  494. return;
  495. }
  496. target.style.setProperty("height", "auto");
  497. target.querySelector(".hide-preCode-box")?.remove();
  498. });
  499. },
  500. /**
  501. * 恢复评论到正确位置
  502. */
  503. restoreComments() {
  504. /* 第一条评论 */
  505. log.info("恢复评论到正确位置-第一条评论");
  506. utils.waitNode(".first-recommend-box").then((element) => {
  507. let recommendBoxElement = document.querySelector(
  508. ".recommend-box.insert-baidu-box.recommend-box-style"
  509. );
  510. recommendBoxElement.insertBefore(
  511. element,
  512. recommendBoxElement.firstChild
  513. );
  514. });
  515. log.info("恢复评论到正确位置-第二条评论");
  516. /* 第二条评论 */
  517. utils.waitNode(".second-recommend-box").then((element) => {
  518. let recommendBoxElement = document.querySelector(
  519. ".recommend-box.insert-baidu-box.recommend-box-style"
  520. );
  521. recommendBoxElement.insertBefore(
  522. element,
  523. recommendBoxElement.firstChild
  524. );
  525. });
  526. },
  527. /**
  528. * 标识CSDN下载的链接
  529. */
  530. identityCSDNDownload() {
  531. log.info("标识CSDN下载的链接");
  532. document
  533. .querySelectorAll(
  534. ".recommend-item-box[data-url*='https://download.csdn.net/']"
  535. )
  536. .forEach((item) => {
  537. if (GM_Menu.get("removeCSDNDownloadPC")) {
  538. item.remove();
  539. } else {
  540. item
  541. .querySelector(".content-box")
  542. .style.setProperty("border", "2px solid red");
  543. }
  544. });
  545. },
  546. /**
  547. * 全文居中
  548. */
  549. articleCenter() {
  550. if (!GM_Menu.get("articleCenter")) {
  551. return;
  552. }
  553. log.info("全文居中");
  554. GM_addStyle(`
  555. aside.blog_container_aside{
  556. display:none !important;
  557. }
  558. #mainBox main{
  559. width: inherit !important;
  560. }
  561. `);
  562. GM_addStyle(`
  563. @media (min-width: 1320px) and (max-width:1380px) {
  564. .nodata .container {
  565. width:900px !important
  566. }
  567.  
  568. .nodata .container main {
  569. width: 900px
  570. }
  571. .nodata .container main #pcCommentBox pre >ol.hljs-ln {
  572. width: 490px !important
  573. }
  574. .nodata .container main .articleConDownSource {
  575. width: 500px
  576. }
  577. }
  578. @media screen and (max-width: 1320px) {
  579. .nodata .container {
  580. width:760px !important
  581. }
  582. .nodata .container main {
  583. width: 760px
  584. }
  585. .nodata .container main #pcCommentBox pre >ol.hljs-ln {
  586. width: 490px !important
  587. }
  588. .nodata .container main .toolbox-list .tool-reward {
  589. display: none
  590. }
  591. .nodata .container main .more-toolbox-new .toolbox-left .profile-box .profile-name {
  592. max-width: 128px
  593. }
  594. .nodata .container main .articleConDownSource {
  595. width: 420px
  596. }
  597. }
  598. @media screen and (min-width: 1380px) {
  599. .nodata .container {
  600. width:1010px !important
  601. }
  602. .nodata .container main {
  603. width: 1010px
  604. }
  605. .nodata .container main #pcCommentBox pre >ol.hljs-ln {
  606. width: 490px !important
  607. }
  608. .nodata .container main .articleConDownSource {
  609. width: 560px
  610. }
  611. }
  612. @media (min-width: 1550px) and (max-width:1700px) {
  613. .nodata .container {
  614. width:820px !important
  615. }
  616. .nodata .container main {
  617. width: 820px
  618. }
  619. .nodata .container main #pcCommentBox pre >ol.hljs-ln {
  620. width: 690px !important
  621. }
  622. .nodata .container main .articleConDownSource {
  623. width: 500px
  624. }
  625. }
  626. @media screen and (min-width: 1700px) {
  627. .nodata .container {
  628. width:1010px !important
  629. }
  630. .nodata .container main {
  631. width: 1010px
  632. }
  633. .nodata .container main #pcCommentBox pre >ol.hljs-ln {
  634. width: 690px !important
  635. }
  636. .nodata .container main .articleConDownSource {
  637. width: 560px
  638. }
  639. }
  640. `);
  641. },
  642. /**
  643. * 添加前往评论的按钮,在返回顶部的下面
  644. */
  645. addGotoRecommandButton() {
  646. log.info("添加前往评论的按钮,在返回顶部的上面");
  647. let gotoRecommandNode = document.createElement("a");
  648. gotoRecommandNode.className = "option-box";
  649. gotoRecommandNode.setAttribute("data-type", "gorecommand");
  650. gotoRecommandNode.innerHTML = `<span class="show-txt" style="display:flex;opacity:100;">前往<br>评论</span>`;
  651. gotoRecommandNode.addEventListener("click", function () {
  652. log.info("滚动到评论");
  653. let toolbarBoxElement = document.querySelector("#toolBarBox");
  654. let toolbarBoxOffsetTop =
  655. toolbarBoxElement.getBoundingClientRect().top + window.scrollY;
  656. let csdnToolBarElement = document.querySelector("#csdn-toolbar");
  657. let csdnToolBarStyles = window.getComputedStyle(csdnToolBarElement);
  658. let csdnToolBarHeight =
  659. csdnToolBarElement.clientHeight -
  660. parseFloat(csdnToolBarStyles.paddingTop) -
  661. parseFloat(csdnToolBarStyles.paddingBottom);
  662. window.scrollTo({
  663. top: toolbarBoxOffsetTop - csdnToolBarHeight - 8,
  664. left: 0,
  665. behavior: "smooth"
  666. })
  667. });
  668. utils.waitNode(".csdn-side-toolbar").then(() => {
  669. let targetElement = document.querySelector(
  670. ".csdn-side-toolbar a:nth-last-child(2)"
  671. );
  672. targetElement.parentElement.insertBefore(
  673. gotoRecommandNode,
  674. targetElement.nextSibling
  675. );
  676. });
  677. },
  678. /**
  679. * 屏蔽登录弹窗
  680. */
  681. shieldLoginDialog() {
  682. if (GM_Menu.get("shieldLoginDialog")) {
  683. log.info("屏蔽登录弹窗");
  684. window.GM_CSS_GM_shieldLoginDialog = [
  685. GM_addStyle(
  686. `.passport-login-container{display: none !important;}`
  687. ),
  688. ];
  689. }
  690. },
  691. /**
  692. * 自动展开内容块
  693. */
  694. autoExpandContent() {
  695. if (!GM_Menu.get("autoExpandContent")) {
  696. return;
  697. }
  698. log.info("自动展开内容块");
  699. GM_addStyle(`
  700. pre.set-code-hide{
  701. height: auto !important;
  702. }
  703. pre.set-code-hide .hide-preCode-box{
  704. display: none !important;
  705. }
  706. `);
  707. },
  708. /**
  709. * 屏蔽右侧悬浮按钮
  710. */
  711. csdnShieldfloatingButton() {
  712. if (!GM_Menu.get("csdnShieldfloatingButton")) {
  713. return;
  714. }
  715. log.info("屏蔽右侧悬浮按钮");
  716. GM_addStyle(`
  717. div.csdn-side-toolbar{
  718. display: none !important;
  719. }
  720. `);
  721. },
  722. /**
  723. * 屏蔽底部推荐文章
  724. */
  725. csdnShieldBottomRecommendArticle() {
  726. if (!GM_Menu.get("csdnShieldBottomRecommendArticle")) {
  727. return;
  728. }
  729. log.info("屏蔽底部推荐文章");
  730. GM_addStyle(`
  731. main > div.recommend-box {
  732. display: none !important;
  733. }
  734. `);
  735. },
  736. /**
  737. * 屏蔽底部悬浮工具栏
  738. */
  739. csdnShieldBottomFloatingToolbar() {
  740. if (!GM_Menu.get("csdnShieldBottomFloatingToolbar")) {
  741. return;
  742. }
  743. log.info("屏蔽底部悬浮工具栏");
  744. GM_addStyle(`
  745. #toolBarBox {
  746. display: none !important;
  747. }
  748. `);
  749. },
  750. /**
  751. * 显示/隐藏目录
  752. */
  753. showOrHideDirectory() {
  754. if (GM_Menu.get("showOrHideDirectory")) {
  755. log.info("显示目录");
  756. GM_addStyle(`
  757. aside.blog_container_aside{
  758. display: none !important;
  759. }
  760. `);
  761. } else {
  762. log.info("隐藏目录");
  763. GM_addStyle(`
  764. aside.blog_container_aside{
  765. display: block !important;
  766. }
  767. `);
  768. }
  769. },
  770. /**
  771. * 显示/隐藏侧边栏
  772. */
  773. showOrHideSidebar() {
  774. if (GM_Menu.get("showOrHideSidebar")) {
  775. log.info("显示侧边栏");
  776. GM_addStyle(`
  777. #rightAsideConcision{
  778. display: none !important;
  779. }
  780. `);
  781. } else {
  782. log.info("隐藏侧边栏");
  783. GM_addStyle(`
  784. #rightAsideConcision{
  785. display: block !important;
  786. }
  787. `);
  788. }
  789. },
  790. /**
  791. * 去除CSDN拦截其它网址的url并自动跳转
  792. */
  793. jumpRedirect() {
  794. /* https://link.csdn.net/?target=https%3A%2F%2Fjaist.dl.sourceforge.net%2Fproject%2Fportecle%2Fv1.11%2Fportecle-1.11.zip */
  795. if (
  796. window.location.hostname === "link.csdn.net" &&
  797. window.location.search.startsWith("?target")
  798. ) {
  799. /* 禁止CSDN拦截跳转 */
  800. window.stop();
  801. let search = window.location.search.replace(/^\?target=/gi, "");
  802. search = decodeURIComponent(search);
  803. let newURL = search;
  804. log.success(`跳转链接 ${newURL}`);
  805. window.location.href = newURL;
  806. }
  807. },
  808. /**
  809. * C知道
  810. */
  811. cKnow() {
  812. if (!window.location.href.startsWith("https://so.csdn.net/so/ai")) {
  813. return;
  814. }
  815. GM_Menu.add({
  816. key: "csdn_pc_cknow",
  817. text: "【屏蔽】C知道的背景水印",
  818. });
  819. if (GM_Menu.get("csdn_pc_cknow")) {
  820. log.success(GM_Menu.getText("csdn_pc_cknow"));
  821. GM_addStyle(`
  822. div.username_mask_cover{
  823. background-image: none !important;
  824. }
  825. `);
  826. }
  827. },
  828. run() {
  829. this.addCSS();
  830. this.cKnow();
  831. this.articleCenter();
  832. this.shieldLoginDialog();
  833. this.autoExpandContent();
  834. this.csdnShieldfloatingButton();
  835. this.csdnShieldBottomRecommendArticle();
  836. this.csdnShieldBottomFloatingToolbar();
  837. this.showOrHideDirectory();
  838. this.showOrHideSidebar();
  839. let that = this;
  840. let readyCallBack = function () {
  841. that.removeClipboardHijacking();
  842. that.unBlockCopy();
  843. that.identityCSDNDownload();
  844. that.clickPreCodeAutomatically();
  845. that.restoreComments();
  846. that.addGotoRecommandButton();
  847. };
  848. DOMUtils.ready(readyCallBack);
  849. if (window.location.hostname === "wenku.csdn.net") {
  850. this.addWenKuCSS();
  851. }
  852. },
  853. },
  854. Mobile: {
  855. addCSS() {
  856. GM_addStyle(`
  857. #mainBox{
  858. width: auto;
  859. }
  860. .user-desc.user-desc-fix{
  861. height: auto !important;
  862. overflow: auto !important;
  863. }
  864. #operate,.feed-Sign-span,
  865. .view_comment_box,
  866. .weixin-shadowbox.wap-shadowbox,
  867. .feed-Sign-span,
  868. .user-desc.user-desc-fix,
  869. .comment_read_more_box,
  870. #content_views pre.set-code-hide .hide-preCode-box,
  871. .passport-login-container,
  872. .hljs-button[data-title='登录后复制'],
  873. .article-show-more,
  874. #treeSkill,
  875. div.btn_open_app_prompt_div,
  876. div.readall_box,
  877. div.aside-header-fixed,
  878. div.feed-Sign-weixin,
  879. div.ios-shadowbox{
  880. display:none !important;
  881. }
  882. .component-box .praise {
  883. background: #ff5722;
  884. border-radius: 5px;
  885. padding: 0px 8px;
  886. height: auto;
  887. }
  888. .component-box .praise,.component-box .share {
  889. color: #fff;
  890. }
  891. .component-box a {
  892. display: inline-block;
  893. font-size:xx-small;
  894. }
  895. .component-box {
  896. display: inline;
  897. margin: 0;
  898. position: relative;
  899. white-space:nowrap;
  900. }
  901. .csdn-edu-title{
  902. background: #4d6de1;
  903. border-radius: 5px;
  904. padding: 0px 8px;
  905. height: auto;
  906. color: #fff !important;
  907. }
  908. #comment{
  909. max-height: none !important;
  910. }
  911. #content_views pre,
  912. #content_views pre code{
  913. webkit-touch-callout: text !important;
  914. -webkit-user-select: text !important;
  915. -khtml-user-select: text !important;
  916. -moz-user-select: text !important;
  917. -ms-user-select: text !important;
  918. user-select: text !important;
  919. }
  920. #content_views pre.set-code-hide,
  921. .article_content{
  922. height: 100% !important;
  923. overflow: auto !important;
  924. }`);
  925. GM_addStyle(`
  926. .GM-csdn-dl{
  927. padding: .24rem .32rem;
  928. width: 100%;
  929. justify-content: space-between;
  930. -webkit-box-pack: justify;
  931. border-bottom: 1px solid #F5F6F7!important;
  932. }
  933. .GM-csdn-title{
  934. font-size: .3rem;
  935. color: #222226;
  936. letter-spacing: 0;
  937. line-height: .44rem;
  938. font-weight: 600;
  939. //max-height: .88rem;
  940. word-break: break-all;
  941. overflow: hidden;
  942. display: -webkit-box;
  943. -webkit-box-orient: vertical;
  944. -webkit-line-clamp: 2
  945. }
  946. .GM-csdn-title a{
  947. word-break: break-all;
  948. color: #222226;
  949. font-weight: 600;
  950. }
  951. .GM-csdn-title em,.GM-csdn-content em{
  952. font-style: normal;
  953. color: #fc5531
  954. }
  955. .GM-csdn-content{
  956. //max-width: 5.58rem;
  957. overflow: hidden;
  958. text-overflow: ellipsis;
  959. display: -webkit-box;
  960. -webkit-line-clamp: 1;
  961. -webkit-box-orient: vertical;
  962. color: #555666;
  963. font-size: .24rem;
  964. line-height: .34rem;
  965. max-height: .34rem;
  966. word-break: break-all;
  967. -webkit-box-flex: 1;
  968. -ms-flex: 1;
  969. flex: 1;
  970. margin-top: .16rem;
  971. }
  972. .GM-csdn-img img{
  973. width: 2.18rem;
  974. height: 1.58rem;
  975. //margin-left: .16rem
  976. }`);
  977. },
  978. /**
  979. * 重构底部推荐
  980. */
  981. refactoringRecommendation() {
  982. function refactoring() {
  983. /* 反复执行的重构函数 */
  984. log.success("重构底部推荐");
  985. document.querySelectorAll(".container-fluid").forEach((item) => {
  986. /* 链接 */
  987. let url = "";
  988. /* 标题 */
  989. let title = "";
  990. /* 内容 */
  991. let content = "";
  992. /* 图片 */
  993. let img = "";
  994. /* 判断是否是CSDN资源下载 */
  995. let isCSDNDownload = false;
  996. /* 判断是否是CSDN-学院资源下载 */
  997. let isCSDNEduDownload = false;
  998. if (item.hasAttribute("data-url")) {
  999. /* 存在真正的URL */
  1000. url = item.getAttribute("data-url");
  1001. title = item.querySelector(
  1002. ".recommend_title div.left"
  1003. ).innerHTML;
  1004. content = item.querySelector(".text").innerHTML;
  1005. if (item.querySelectorAll(".recommend-img").length) {
  1006. /* 如果有图片就加进去 */
  1007. item.querySelectorAll(".recommend-img").forEach((item2) => {
  1008. img += item2.innerHTML;
  1009. });
  1010. }
  1011. } else {
  1012. log.info("节点上无data-url");
  1013. url = item.querySelector("a[data-type]").getAttribute("href");
  1014. title = item.querySelector(
  1015. ".recommend_title div.left"
  1016. ).innerHTML;
  1017. content = item.querySelector(".text").innerHTML;
  1018. }
  1019. var _URL_ = new URL(url);
  1020. if (
  1021. _URL_.host === "download.csdn.net" ||
  1022. (_URL_.host === "www.iteye.com" &&
  1023. _URL_.pathname.match(/^\/resource/gi))
  1024. ) {
  1025. /* 该链接为csdn资源下载 */
  1026. log.info("该链接为csdn资源下载");
  1027. isCSDNDownload = true;
  1028. title =
  1029. `<div class="component-box"><a class="praise" href="javascript:;">CSDN下载</a></div>` +
  1030. title;
  1031. } else if (_URL_.origin.match(/edu.csdn.net/gi)) {
  1032. /* 该链接为csdn学院下载 */
  1033. isCSDNEduDownload = true;
  1034. log.info("该链接为csdn学院下载");
  1035. title =
  1036. `<div class="component-box"><a class="csdn-edu-title" href="javascript:;">CSDN学院</a></div>` +
  1037. title;
  1038. }
  1039. item.setAttribute("class", "GM-csdn-dl");
  1040. item.setAttribute("data-url", url);
  1041. item.innerHTML = `<div class="GM-csdn-title"><div class="left">${title}</div></div><div class="GM-csdn-content">${content}</div><div class="GM-csdn-img">${img}</div>`;
  1042. item.addEventListener("click", function () {
  1043. if (GM_Menu.get("openNewTab")) {
  1044. window.open(url, "_blank");
  1045. } else {
  1046. window.location.href = url;
  1047. }
  1048. });
  1049. if (
  1050. (isCSDNDownload || isCSDNEduDownload) &&
  1051. GM_Menu.get("removeCSDNDownloadMobile")
  1052. ) {
  1053. item.remove();
  1054. }
  1055. });
  1056. }
  1057. let lockFunction = new utils.LockFunction(refactoring, this, 50);
  1058. utils.waitNode("#recommend").then((element) => {
  1059. lockFunction.run();
  1060. utils.mutationObserver(element, {
  1061. callback: lockFunction.run,
  1062. config: { childList: true, subtree: true, attributes: true },
  1063. });
  1064. });
  1065. },
  1066. /**
  1067. * 去除广告
  1068. */
  1069. removeAds() {
  1070. log.info("去除广告");
  1071. /* 登录窗口 */
  1072. waitForElementToRemove(".passport-login-container");
  1073. /* 打开APP */
  1074. waitForElementToRemove(
  1075. ".btn_open_app_prompt_box.detail-open-removed"
  1076. );
  1077. /* 广告 */
  1078. waitForElementToRemove(".add-firstAd");
  1079. /* 打开CSDN APP 小程序看全文 */
  1080. waitForElementToRemove("div.feed-Sign-weixin");
  1081. /* ios版本提示 */
  1082. waitForElementToRemove("div.ios-shadowbox");
  1083. },
  1084. /**
  1085. * C知道
  1086. */
  1087. cKnow() {
  1088. if (!window.location.href.startsWith("https://so.csdn.net/so/ai")) {
  1089. return;
  1090. }
  1091. GM_Menu.add({
  1092. key: "csdn_mobile_cknow",
  1093. text: "【屏蔽】C知道的背景水印",
  1094. });
  1095. if (GM_Menu.get("csdn_mobile_cknow")) {
  1096. log.success(GM_Menu.getText("csdn_mobile_cknow"));
  1097. GM_addStyle(`
  1098. div.username_mask_cover{
  1099. background-image: none !important;
  1100. }
  1101. `);
  1102. }
  1103. },
  1104. run() {
  1105. this.addCSS();
  1106. this.cKnow();
  1107. let that = this;
  1108. let readyCallBack = function () {
  1109. that.removeAds();
  1110. that.refactoringRecommendation();
  1111. };
  1112. DOMUtils.ready(readyCallBack);
  1113. },
  1114. },
  1115. /**
  1116. * 函数入口
  1117. */
  1118. run() {
  1119. Optimization.csdn.PC.jumpRedirect();
  1120. if (utils.isPhone()) {
  1121. log.success("移动端模式");
  1122. this.Mobile.run();
  1123. } else {
  1124. log.success("桌面端模式");
  1125. this.PC.run();
  1126. }
  1127. },
  1128. },
  1129. huaWeiCSDN: {
  1130. /**
  1131. * 判断是否是CSDN
  1132. */
  1133. locationMatch() {
  1134. return Boolean(/huaweicloud.csdn.net/i.test(window.location.origin));
  1135. },
  1136. PC: {
  1137. addCSS() {
  1138. GM_addStyle(`
  1139. /* 底部免费抽xxx奖品广告 */
  1140. div.siderbar-box,
  1141. /* 华为开发者联盟加入社区 */
  1142. div.user-desc.user-desc-fix,
  1143. /* 点击阅读全文 */
  1144. div.article-show-more{
  1145. display: none !important;
  1146. }
  1147.  
  1148. /* 自动展开全文 */
  1149. .main-content .user-article{
  1150. height: auto !important;
  1151. overflow: auto !important;
  1152. }
  1153. `);
  1154. },
  1155. run() {
  1156. this.addCSS();
  1157. this.huaweiCSDNShieldCloudDeveloperTaskChallengeEvent();
  1158. this.huaweiCSDNShieldLeftFloatingButton();
  1159. this.huaweiCSDNBlockRightColumn();
  1160. this.huaweiCSDNBlockRecommendedContentAtTheBottom();
  1161. this.huaweiCSDNShieldTheBottomForMoreRecommendations();
  1162. },
  1163. /**
  1164. * 屏蔽云开发者任务挑战活动
  1165. */
  1166. huaweiCSDNShieldCloudDeveloperTaskChallengeEvent() {
  1167. let GM_cookie = new utils.GM_Cookie();
  1168. GM_cookie.set({ name: "show_join_group_index", value: 1 });
  1169. log.success("屏蔽云开发者任务挑战活动");
  1170. },
  1171. /**
  1172. * 屏蔽左侧悬浮按钮
  1173. */
  1174. huaweiCSDNShieldLeftFloatingButton() {
  1175. if (!GM_Menu.get("huaweiCSDNShieldLeftFloatingButton")) {
  1176. return;
  1177. }
  1178. log.success(
  1179. "屏蔽左侧悬浮按钮,包括当前阅读量、点赞按钮、评论按钮、分享按钮"
  1180. );
  1181. GM_addStyle(`
  1182. div.toolbar-wrapper.article-interact-bar{
  1183. display: none !important;
  1184. }`);
  1185. },
  1186. /**
  1187. * 屏蔽右侧栏
  1188. */
  1189. huaweiCSDNBlockRightColumn() {
  1190. if (!GM_Menu.get("huaweiCSDNBlockRightColumn")) {
  1191. return;
  1192. }
  1193. log.success("屏蔽右侧栏,包括相关产品-活动日历-运营活动-热门标签");
  1194. GM_addStyle(`
  1195. div.page-home-right.dp-aside-right{
  1196. display: none !important;
  1197. }
  1198. `);
  1199. },
  1200. /**
  1201. * 屏蔽底部推荐内容
  1202. */
  1203. huaweiCSDNBlockRecommendedContentAtTheBottom() {
  1204. if (!GM_Menu.get("huaweiCSDNBlockRecommendedContentAtTheBottom")) {
  1205. return;
  1206. }
  1207. log.success("屏蔽底部推荐内容");
  1208. GM_addStyle(`
  1209. div.recommend-card-box{
  1210. display: none !important;
  1211. }`);
  1212. },
  1213. /**
  1214. * 屏蔽底部更多推荐
  1215. */
  1216. huaweiCSDNShieldTheBottomForMoreRecommendations() {
  1217. if (!GM_Menu.get("huaweiCSDNShieldTheBottomForMoreRecommendations")) {
  1218. return;
  1219. }
  1220. log.success("屏蔽底部更多推荐");
  1221. GM_addStyle(`
  1222. div.more-article{
  1223. display: none !important;
  1224. }`);
  1225. },
  1226. },
  1227. },
  1228. };
  1229. if (Optimization.huaWeiCSDN.locationMatch()) {
  1230. GM_Menu.add([
  1231. {
  1232. key: "huaweiCSDNShieldCloudDeveloperTaskChallengeEvent",
  1233. text: "电脑-屏蔽云开发者任务挑战活动",
  1234. enable: true,
  1235. },
  1236. {
  1237. key: "huaweiCSDNShieldLeftFloatingButton",
  1238. text: "电脑-屏蔽左侧悬浮按钮",
  1239. title: "包括当前阅读量、点赞按钮、评论按钮、分享按钮",
  1240. },
  1241. {
  1242. key: "huaweiCSDNBlockRightColumn",
  1243. text: "电脑-屏蔽右侧",
  1244. title: "包括相关产品-活动日历-运营活动-热门标签",
  1245. },
  1246. {
  1247. key: "huaweiCSDNBlockRecommendedContentAtTheBottom",
  1248. text: "电脑-屏蔽底部推荐内容",
  1249. },
  1250. {
  1251. key: "huaweiCSDNShieldTheBottomForMoreRecommendations",
  1252. text: "电脑-屏蔽底部更多推荐",
  1253. },
  1254. ]);
  1255. Optimization.huaWeiCSDN.PC.run();
  1256. } else if (Optimization.csdn.locationMatch()) {
  1257. if (utils.isPhone()) {
  1258. GM_Menu.add([
  1259. {
  1260. key: "openNewTab",
  1261. text: "手机-底部推荐文章新标签页打开",
  1262. enable: true,
  1263. },
  1264. {
  1265. key: "removeCSDNDownloadMobile",
  1266. text: "手机-移除文章底部的CSDN下载",
  1267. },
  1268. ]);
  1269. } else {
  1270. GM_Menu.add([
  1271. {
  1272. key: "removeCSDNDownloadPC",
  1273. text: "电脑-屏蔽底部推荐文章的CSDN下载",
  1274. },
  1275. {
  1276. key: "shieldLoginDialog",
  1277. text: "电脑-屏蔽登录弹窗",
  1278. enable: true,
  1279. callback(data) {
  1280. if (!data.enable) {
  1281. window.GM_CSS_GM_shieldLoginDialog?.forEach((item) => {
  1282. item.remove();
  1283. });
  1284. } else {
  1285. if (typeof window.GM_CSS_GM_shieldLoginDialog !== "undefined") {
  1286. window.GM_CSS_GM_shieldLoginDialog = [
  1287. ...window.GM_CSS_GM_shieldLoginDialog,
  1288. GM_addStyle(
  1289. `.passport-login-container{display: none !important;}`
  1290. ),
  1291. ];
  1292. } else {
  1293. window.GM_CSS_GM_shieldLoginDialog = [
  1294. GM_addStyle(
  1295. `.passport-login-container{display: none !important;}`
  1296. ),
  1297. ];
  1298. }
  1299. }
  1300. },
  1301. },
  1302. {
  1303. key: "csdnShieldfloatingButton",
  1304. text: "电脑-屏蔽右侧悬浮按钮",
  1305. },
  1306. {
  1307. key: "csdnShieldBottomRecommendArticle",
  1308. text: "电脑-屏蔽底部推荐文章",
  1309. },
  1310. {
  1311. key: "csdnShieldBottomFloatingToolbar",
  1312. text: "电脑-屏蔽底部悬浮工具栏",
  1313. },
  1314. {
  1315. key: "articleCenter",
  1316. text: "电脑-全文居中",
  1317. enable: true,
  1318. },
  1319. {
  1320. key: "autoExpandContent",
  1321. text: "电脑-自动展开内容块",
  1322. },
  1323. {
  1324. key: "showOrHideDirectory",
  1325. text: "电脑-显示目录",
  1326. },
  1327. {
  1328. key: "showOrHideSidebar",
  1329. text: "电脑-显示侧边栏",
  1330. },
  1331. ]);
  1332. }
  1333.  
  1334. GM_Menu.add({
  1335. key: "gotoCSDNCKnow",
  1336. text: "⚙ 前往C知道",
  1337. autoReload: false,
  1338. showText(text) {
  1339. return text;
  1340. },
  1341. callback() {
  1342. window.open("https://so.csdn.net/so/ai?", "_blank");
  1343. },
  1344. });
  1345. Optimization.csdn.run();
  1346. } else if (Optimization.jianshu.locationMatch()) {
  1347. if (utils.isPhone()) {
  1348. GM_Menu.add([
  1349. {
  1350. key: "JianShuremoveFooterRecommendRead",
  1351. text: "手机-移除底部推荐阅读",
  1352. },
  1353. {
  1354. key: "JianShuShieldUserComments",
  1355. text: "手机-屏蔽评论区",
  1356. },
  1357. ]);
  1358. } else {
  1359. GM_Menu.add([
  1360. {
  1361. key: "JianShuArticleCenter",
  1362. text: "电脑-全文居中",
  1363. enable: true,
  1364. },
  1365. {
  1366. key: "JianShuShieldRelatedArticles",
  1367. text: "电脑-屏蔽相关文章",
  1368. },
  1369. {
  1370. key: "JianShuShieldUserComments",
  1371. text: "电脑-屏蔽评论区",
  1372. },
  1373. {
  1374. key: "JianShuShieldRecommendedReading",
  1375. text: "电脑-屏蔽推荐阅读",
  1376. },
  1377. ]);
  1378. }
  1379.  
  1380. Optimization.jianshu.run();
  1381. }
  1382. })();