CSDN|简书优化

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

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

  1. // ==UserScript==
  2. // @name CSDN|简书优化
  3. // @icon https://www.csdn.net/favicon.ico
  4. // @namespace https://greasyfork.org/zh-CN/scripts/406136-csdn-简书优化
  5. // @supportURL https://greasyfork.org/zh-CN/scripts/406136-csdn-简书优化/feedback
  6. // @version 2023.9.26.18.20
  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://greasyfork.org/scripts/449471-viewer/code/Viewer.js?version=1249086
  22. // @require https://greasyfork.org/scripts/455186-whitesevsutils/code/WhiteSevsUtils.js?version=1256917
  23. // @require https://greasyfork.org/scripts/465772-domutils/code/DOMUtils.js?version=1256298
  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.waitNode(selectorText).then((dom) => {
  59. dom.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.waitNode("div._gp-ck").then((dom) => {
  141. dom.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((nodeList) => {
  164. nodeList[0].style["visibility"] = "hidden";
  165. utils.mutationObserver(nodeList[0], {
  166. callback: (mutations) => {
  167. if (mutations.length == 0) {
  168. return;
  169. }
  170. if (mutations[0].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. max-height: none !important;
  372. }
  373. .blog_container_aside,
  374. #nav{
  375. margin-left: -45px;
  376. }
  377. .recommend-right.align-items-stretch.clearfix,.dl_right_fixed{
  378. margin-left: 45px;
  379. }
  380. #content_views pre,
  381. #content_views pre code{
  382. user-select: text !important;
  383. }
  384. #article_content,
  385. .user-article.user-article-hide{
  386. height: auto !important;
  387. overflow: auto !important;
  388. }
  389. `);
  390. },
  391. /**
  392. * 去除剪贴板劫持
  393. */
  394. removeClipboardHijacking() {
  395. log.info("去除剪贴板劫持");
  396. document.querySelector(".article-copyright")?.remove();
  397. if (unsafeWindow.articleType) {
  398. unsafeWindow.articleType = 0;
  399. }
  400. if (
  401. unsafeWindow.csdn &&
  402. unsafeWindow.csdn.copyright &&
  403. unsafeWindow.csdn.copyright.textData
  404. ) {
  405. unsafeWindow.csdn.copyright.textData = "";
  406. }
  407. if (
  408. unsafeWindow.csdn &&
  409. unsafeWindow.csdn.copyright &&
  410. unsafeWindow.csdn.copyright.htmlData
  411. ) {
  412. unsafeWindow.csdn.copyright.htmlData = "";
  413. }
  414. },
  415. /**
  416. * 取消禁止复制
  417. */
  418. unBlockCopy() {
  419. log.info("取消禁止复制");
  420. document.addEventListener("click", function (event) {
  421. let target = event.target;
  422. if (!target.classList.contains("hljs-button")) {
  423. return;
  424. }
  425. utils.preventEvent(event);
  426. /* 需要复制的文本 */
  427. let copyText = target.parentElement.textContent;
  428. utils.setClip(copyText);
  429. log.success("点击复制 复制成功~");
  430. target.setAttribute("data-title", "复制成功");
  431. });
  432. let changeDataTitle = new utils.LockFunction(function (event) {
  433. let target = event.target;
  434. if (!target.localName === "pre") {
  435. return;
  436. }
  437. target
  438. .querySelector(".hljs-button")
  439. ?.setAttribute("data-title", "复制");
  440. });
  441.  
  442. document.addEventListener("mouseenter", changeDataTitle.run, true);
  443. document.addEventListener("mouseleave", changeDataTitle.run, true);
  444. /* 取消Ctrl+C的禁止 */
  445. utils.waitNode("#content_views").then((nodeList) => {
  446. unsafeWindow?.$("#content_views")?.unbind("copy");
  447. nodeList[0].addEventListener("copy", function (event) {
  448. utils.preventEvent(event);
  449. utils.setClip(unsafeWindow.getSelection().toString());
  450. log.success("Ctrl+C 复制成功~");
  451. return false;
  452. });
  453. });
  454. /* 删除所有复制按钮的原有的复制事件 */
  455. document.querySelectorAll(".hljs-button").forEach((item) => {
  456. item.removeAttribute("onclick");
  457. });
  458. },
  459. /**
  460. * 点击代码块自动展开
  461. */
  462. clickPreCodeAutomatically() {
  463. if (!GM_Menu.get("autoExpandContent")) {
  464. return;
  465. }
  466. log.info("点击代码块自动展开");
  467. document.addEventListener("click", function (event) {
  468. let target = event.target;
  469. if (target.localName !== "pre") {
  470. return;
  471. }
  472. target.style.setProperty("height", "auto");
  473. target.querySelector(".hide-preCode-box")?.remove();
  474. });
  475. },
  476. /**
  477. * 恢复评论到正确位置
  478. */
  479. restoreComments() {
  480. /* 第一条评论 */
  481. log.info("恢复评论到正确位置-第一条评论");
  482. utils.waitNode(".first-recommend-box").then((nodeList) => {
  483. let recommendBoxElement = document.querySelector(
  484. ".recommend-box.insert-baidu-box.recommend-box-style"
  485. );
  486. recommendBoxElement.insertBefore(
  487. nodeList[0],
  488. recommendBoxElement.firstChild
  489. );
  490. });
  491. log.info("恢复评论到正确位置-第二条评论");
  492. /* 第二条评论 */
  493. utils.waitNode(".second-recommend-box").then((nodeList) => {
  494. let recommendBoxElement = document.querySelector(
  495. ".recommend-box.insert-baidu-box.recommend-box-style"
  496. );
  497. recommendBoxElement.insertBefore(
  498. nodeList[0],
  499. recommendBoxElement.firstChild
  500. );
  501. });
  502. },
  503. /**
  504. * 标识CSDN下载的链接
  505. */
  506. identityCSDNDownload() {
  507. log.info("标识CSDN下载的链接");
  508. document
  509. .querySelectorAll(
  510. ".recommend-item-box[data-url*='https://download.csdn.net/']"
  511. )
  512. .forEach((item) => {
  513. if (GM_Menu.get("removeCSDNDownloadPC")) {
  514. item.remove();
  515. } else {
  516. item
  517. .querySelector(".content-box")
  518. .style.setProperty("border", "2px solid red");
  519. }
  520. });
  521. },
  522. /**
  523. * 全文居中
  524. */
  525. articleCenter() {
  526. if (!GM_Menu.get("articleCenter")) {
  527. return;
  528. }
  529. log.info("全文居中");
  530. GM_addStyle(`
  531. aside.blog_container_aside{
  532. display:none !important;
  533. }
  534. #mainBox main{
  535. width: inherit !important;
  536. }
  537. `);
  538. GM_addStyle(`
  539. @media (min-width: 1320px) and (max-width:1380px) {
  540. .nodata .container {
  541. width:900px !important
  542. }
  543.  
  544. .nodata .container main {
  545. width: 900px
  546. }
  547. .nodata .container main #pcCommentBox pre >ol.hljs-ln {
  548. width: 490px !important
  549. }
  550. .nodata .container main .articleConDownSource {
  551. width: 500px
  552. }
  553. }
  554. @media screen and (max-width: 1320px) {
  555. .nodata .container {
  556. width:760px !important
  557. }
  558. .nodata .container main {
  559. width: 760px
  560. }
  561. .nodata .container main #pcCommentBox pre >ol.hljs-ln {
  562. width: 490px !important
  563. }
  564. .nodata .container main .toolbox-list .tool-reward {
  565. display: none
  566. }
  567. .nodata .container main .more-toolbox-new .toolbox-left .profile-box .profile-name {
  568. max-width: 128px
  569. }
  570. .nodata .container main .articleConDownSource {
  571. width: 420px
  572. }
  573. }
  574. @media screen and (min-width: 1380px) {
  575. .nodata .container {
  576. width:1010px !important
  577. }
  578. .nodata .container main {
  579. width: 1010px
  580. }
  581. .nodata .container main #pcCommentBox pre >ol.hljs-ln {
  582. width: 490px !important
  583. }
  584. .nodata .container main .articleConDownSource {
  585. width: 560px
  586. }
  587. }
  588. @media (min-width: 1550px) and (max-width:1700px) {
  589. .nodata .container {
  590. width:820px !important
  591. }
  592. .nodata .container main {
  593. width: 820px
  594. }
  595. .nodata .container main #pcCommentBox pre >ol.hljs-ln {
  596. width: 690px !important
  597. }
  598. .nodata .container main .articleConDownSource {
  599. width: 500px
  600. }
  601. }
  602. @media screen and (min-width: 1700px) {
  603. .nodata .container {
  604. width:1010px !important
  605. }
  606. .nodata .container main {
  607. width: 1010px
  608. }
  609. .nodata .container main #pcCommentBox pre >ol.hljs-ln {
  610. width: 690px !important
  611. }
  612. .nodata .container main .articleConDownSource {
  613. width: 560px
  614. }
  615. }
  616. `);
  617. },
  618. /**
  619. * 添加前往评论的按钮,在返回顶部的下面
  620. */
  621. addGotoRecommandButton() {
  622. log.info("添加前往评论的按钮,在返回顶部的上面");
  623. let gotoRecommandNode = document.createElement("a");
  624. gotoRecommandNode.className = "option-box";
  625. gotoRecommandNode.setAttribute("data-type", "gorecommand");
  626. gotoRecommandNode.innerHTML = `<span class="show-txt" style="display:flex;opacity:100;">前往<br>评论</span>`;
  627. gotoRecommandNode.addEventListener("click", function () {
  628. log.info("滚动到评论");
  629. let toolbarBoxElement = document.querySelector("#toolBarBox");
  630. let toolbarBoxOffsetTop =
  631. toolbarBoxElement.getBoundingClientRect().top + window.scrollY;
  632. let csdnToolBarElement = document.querySelector("#csdn-toolbar");
  633. let csdnToolBarStyles = window.getComputedStyle(csdnToolBarElement);
  634. let csdnToolBarHeight =
  635. csdnToolBarElement.clientHeight -
  636. parseFloat(csdnToolBarStyles.paddingTop) -
  637. parseFloat(csdnToolBarStyles.paddingBottom);
  638. unsafeWindow.$("html, body").animate(
  639. {
  640. scrollTop: toolbarBoxOffsetTop - csdnToolBarHeight - 8,
  641. },
  642. 1000
  643. );
  644. });
  645. utils.waitNode(".csdn-side-toolbar").then(() => {
  646. let targetElement = document.querySelector(
  647. ".csdn-side-toolbar a:nth-last-child(2)"
  648. );
  649. targetElement.parentElement.insertBefore(
  650. gotoRecommandNode,
  651. targetElement.nextSibling
  652. );
  653. });
  654. },
  655. /**
  656. * 屏蔽登录弹窗
  657. */
  658. shieldLoginDialog() {
  659. if (GM_Menu.get("shieldLoginDialog")) {
  660. log.info("屏蔽登录弹窗");
  661. window.GM_CSS_GM_shieldLoginDialog = [
  662. GM_addStyle(
  663. `.passport-login-container{display: none !important;}`
  664. ),
  665. ];
  666. }
  667. },
  668. /**
  669. * 自动展开内容块
  670. */
  671. autoExpandContent() {
  672. if (!GM_Menu.get("autoExpandContent")) {
  673. return;
  674. }
  675. log.info("自动展开内容块");
  676. GM_addStyle(`
  677. pre.set-code-hide{
  678. height: auto !important;
  679. }
  680. pre.set-code-hide .hide-preCode-box{
  681. display: none !important;
  682. }
  683. `);
  684. },
  685. /**
  686. * 屏蔽右侧悬浮按钮
  687. */
  688. csdnShieldfloatingButton() {
  689. if (!GM_Menu.get("csdnShieldfloatingButton")) {
  690. return;
  691. }
  692. log.info("屏蔽右侧悬浮按钮");
  693. GM_addStyle(`
  694. div.csdn-side-toolbar{
  695. display: none !important;
  696. }
  697. `);
  698. },
  699. /**
  700. * 屏蔽底部推荐文章
  701. */
  702. csdnShieldBottomRecommendArticle() {
  703. if (!GM_Menu.get("csdnShieldBottomRecommendArticle")) {
  704. return;
  705. }
  706. log.info("屏蔽底部推荐文章");
  707. GM_addStyle(`
  708. main > div.recommend-box {
  709. display: none !important;
  710. }
  711. `);
  712. },
  713. /**
  714. * 屏蔽底部悬浮工具栏
  715. */
  716. csdnShieldBottomFloatingToolbar() {
  717. if (!GM_Menu.get("csdnShieldBottomFloatingToolbar")) {
  718. return;
  719. }
  720. log.info("屏蔽底部悬浮工具栏");
  721. GM_addStyle(`
  722. #toolBarBox {
  723. display: none !important;
  724. }
  725. `);
  726. },
  727. /**
  728. * 显示/隐藏目录
  729. */
  730. showOrHideDirectory() {
  731. if (GM_Menu.get("showOrHideDirectory")) {
  732. log.info("显示目录");
  733. GM_addStyle(`
  734. aside.blog_container_aside{
  735. display: none !important;
  736. }
  737. `);
  738. } else {
  739. log.info("隐藏目录");
  740. GM_addStyle(`
  741. aside.blog_container_aside{
  742. display: block !important;
  743. }
  744. `);
  745. }
  746. },
  747. /**
  748. * 显示/隐藏侧边栏
  749. */
  750. showOrHideSidebar() {
  751. if (GM_Menu.get("showOrHideSidebar")) {
  752. log.info("显示侧边栏");
  753. GM_addStyle(`
  754. #rightAsideConcision{
  755. display: none !important;
  756. }
  757. `);
  758. } else {
  759. log.info("隐藏侧边栏");
  760. GM_addStyle(`
  761. #rightAsideConcision{
  762. display: block !important;
  763. }
  764. `);
  765. }
  766. },
  767. /**
  768. * 去除CSDN拦截其它网址的url并自动跳转
  769. */
  770. jumpRedirect() {
  771. /* https://link.csdn.net/?target=https%3A%2F%2Fjaist.dl.sourceforge.net%2Fproject%2Fportecle%2Fv1.11%2Fportecle-1.11.zip */
  772. if (
  773. window.location.hostname === "link.csdn.net" &&
  774. window.location.search.startsWith("?target")
  775. ) {
  776. /* 禁止CSDN拦截跳转 */
  777. window.stop();
  778. let search = window.location.search.replace(/^\?target=/gi, "");
  779. search = decodeURIComponent(search);
  780. let newURL = search;
  781. log.success(`跳转链接 ${newURL}`);
  782. window.location.href = newURL;
  783. }
  784. },
  785. run() {
  786. this.addCSS();
  787. this.articleCenter();
  788. this.shieldLoginDialog();
  789. this.autoExpandContent();
  790. this.csdnShieldfloatingButton();
  791. this.csdnShieldBottomRecommendArticle();
  792. this.csdnShieldBottomFloatingToolbar();
  793. this.showOrHideDirectory();
  794. this.showOrHideSidebar();
  795. let that = this;
  796. let readyCallBack = function () {
  797. that.removeClipboardHijacking();
  798. that.unBlockCopy();
  799. that.identityCSDNDownload();
  800. that.clickPreCodeAutomatically();
  801. that.restoreComments();
  802. that.addGotoRecommandButton();
  803. };
  804. if (document.readyState !== "loading") {
  805. readyCallBack();
  806. } else {
  807. document.addEventListener("DOMContentLoaded", readyCallBack);
  808. }
  809. },
  810. },
  811. Mobile: {
  812. addCSS() {
  813. GM_addStyle(`
  814. #mainBox{
  815. width: auto;
  816. }
  817. .user-desc.user-desc-fix{
  818. height: auto !important;
  819. overflow: auto !important;
  820. }
  821. #operate,.feed-Sign-span,
  822. .view_comment_box,
  823. .weixin-shadowbox.wap-shadowbox,
  824. .feed-Sign-span,
  825. .user-desc.user-desc-fix,
  826. .comment_read_more_box,
  827. #content_views pre.set-code-hide .hide-preCode-box,
  828. .passport-login-container,
  829. .hljs-button[data-title='登录后复制'],
  830. .article-show-more,
  831. #treeSkill,
  832. div.btn_open_app_prompt_div,
  833. div.readall_box,
  834. div.aside-header-fixed,
  835. div.feed-Sign-weixin,
  836. div.ios-shadowbox{
  837. display:none !important;
  838. }
  839. .component-box .praise {
  840. background: #ff5722;
  841. border-radius: 5px;
  842. padding: 0px 8px;
  843. height: auto;
  844. }
  845. .component-box .praise,.component-box .share {
  846. color: #fff;
  847. }
  848. .component-box a {
  849. display: inline-block;
  850. font-size:xx-small;
  851. }
  852. .component-box {
  853. display: inline;
  854. margin: 0;
  855. position: relative;
  856. white-space:nowrap;
  857. }
  858. .csdn-edu-title{
  859. background: #4d6de1;
  860. border-radius: 5px;
  861. padding: 0px 8px;
  862. height: auto;
  863. color: #fff !important;
  864. }
  865. #comment{
  866. max-height: none !important;
  867. }
  868. #content_views pre,
  869. #content_views pre code{
  870. webkit-touch-callout: text !important;
  871. -webkit-user-select: text !important;
  872. -khtml-user-select: text !important;
  873. -moz-user-select: text !important;
  874. -ms-user-select: text !important;
  875. user-select: text !important;
  876. }
  877. #content_views pre.set-code-hide,
  878. .article_content{
  879. height: 100% !important;
  880. overflow: auto !important;
  881. }`);
  882. GM_addStyle(`
  883. .GM-csdn-dl{
  884. padding: .24rem .32rem;
  885. width: 100%;
  886. justify-content: space-between;
  887. -webkit-box-pack: justify;
  888. border-bottom: 1px solid #F5F6F7!important;
  889. }
  890. .GM-csdn-title{
  891. font-size: .3rem;
  892. color: #222226;
  893. letter-spacing: 0;
  894. line-height: .44rem;
  895. font-weight: 600;
  896. //max-height: .88rem;
  897. word-break: break-all;
  898. overflow: hidden;
  899. display: -webkit-box;
  900. -webkit-box-orient: vertical;
  901. -webkit-line-clamp: 2
  902. }
  903. .GM-csdn-title a{
  904. word-break: break-all;
  905. color: #222226;
  906. font-weight: 600;
  907. }
  908. .GM-csdn-title em,.GM-csdn-content em{
  909. font-style: normal;
  910. color: #fc5531
  911. }
  912. .GM-csdn-content{
  913. //max-width: 5.58rem;
  914. overflow: hidden;
  915. text-overflow: ellipsis;
  916. display: -webkit-box;
  917. -webkit-line-clamp: 1;
  918. -webkit-box-orient: vertical;
  919. color: #555666;
  920. font-size: .24rem;
  921. line-height: .34rem;
  922. max-height: .34rem;
  923. word-break: break-all;
  924. -webkit-box-flex: 1;
  925. -ms-flex: 1;
  926. flex: 1;
  927. margin-top: .16rem;
  928. }
  929. .GM-csdn-img img{
  930. width: 2.18rem;
  931. height: 1.58rem;
  932. //margin-left: .16rem
  933. }
  934. .GM-csdn-Redirect{
  935. color: #fff;
  936. background-color: #f90707;
  937. font-family: sans-serif;
  938. margin: auto 2px;
  939. border: 1px solid #ccc;
  940. border-radius: 4px;
  941. padding: 0px 3px;
  942. font-size: xx-small;
  943. display: inline;
  944. white-space: nowrap;
  945. }`);
  946. },
  947. /**
  948. * 重构底部推荐
  949. */
  950. refactoringRecommendation() {
  951. log.info("重构底部推荐");
  952. function refactoring() {
  953. /* 反复执行的重构函数 */
  954. document.querySelectorAll(".container-fluid").forEach((item) => {
  955. var url = ""; /* 链接 */
  956. var title = ""; /* 标题 */
  957. var content = ""; /* 内容 */
  958. var img = ""; /* 图片 */
  959. var isCSDNDownload = false; /* 判断是否是CSDN资源下载 */
  960. var isCSDNEduDownload = false; /* 判断是否是CSDN-学院资源下载 */
  961. if (item.hasAttribute("data-url")) {
  962. /* 存在真正的URL */
  963. url = item.getAttribute("data-url");
  964. title = item.querySelector(
  965. ".recommend_title div.left"
  966. ).innerHTML;
  967. content = item.querySelector(".text").innerHTML;
  968. if (item.querySelectorAll(".recommend-img").length) {
  969. /* 如果有图片就加进去 */
  970. item.querySelectorAll(".recommend-img").forEach((item2) => {
  971. img += item2.innerHTML;
  972. });
  973. }
  974. } else {
  975. log.info("节点上无data-url");
  976. url = item.querySelector("a[data-type]").getAttribute("href");
  977. title = item.querySelector(
  978. ".recommend_title div.left"
  979. ).innerHTML;
  980. content = item.querySelector(".text").innerHTML;
  981. }
  982. if (GM_Menu.get("showDirect")) {
  983. /* 开启就添加 */
  984. title += `<div class="GM-csdn-Redirect">Redirect</div>`;
  985. }
  986. var _URL_ = new URL(url);
  987. if (
  988. _URL_.host === "download.csdn.net" ||
  989. (_URL_.host === "www.iteye.com" &&
  990. _URL_.pathname.match(/^\/resource/gi))
  991. ) {
  992. /* 该链接为csdn资源下载 */
  993. log.info("该链接为csdn资源下载");
  994. isCSDNDownload = true;
  995. title += `<div class="component-box"><a class="praise" href="javascript:;">CSDN下载</a></div>`;
  996. } else if (_URL_.origin.match(/edu.csdn.net/gi)) {
  997. /* 该链接为csdn学院下载 */
  998. isCSDNEduDownload = true;
  999. log.info("该链接为csdn学院下载");
  1000. title += `<div class="component-box"><a class="csdn-edu-title" href="javascript:;">CSDN学院</a></div>`;
  1001. }
  1002. item.setAttribute("class", "GM-csdn-dl");
  1003. item.setAttribute("data-url", url);
  1004. 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>`;
  1005. item.addEventListener("click", function () {
  1006. if (GM_Menu.get("openNewTab")) {
  1007. window.open(url, "_blank");
  1008. } else {
  1009. window.location.href = url;
  1010. }
  1011. });
  1012. if (
  1013. (isCSDNDownload || isCSDNEduDownload) &&
  1014. GM_Menu.get("removeCSDNDownloadMobile")
  1015. ) {
  1016. item.remove();
  1017. }
  1018. });
  1019. }
  1020. utils.waitNode("#recommend").then((nodeList) => {
  1021. utils.mutationObserver(nodeList[0], {
  1022. callback: () => {
  1023. setTimeout(() => {
  1024. refactoring();
  1025. }, 300);
  1026. },
  1027. config: { childList: true, subtree: true, attributes: true },
  1028. });
  1029. });
  1030. },
  1031. /**
  1032. * 去除广告
  1033. */
  1034. removeAds() {
  1035. log.info("去除广告");
  1036. /* 登录窗口 */
  1037. waitForElementToRemove(".passport-login-container");
  1038. /* 打开APP */
  1039. waitForElementToRemove(
  1040. ".btn_open_app_prompt_box.detail-open-removed"
  1041. );
  1042. /* 广告 */
  1043. waitForElementToRemove(".add-firstAd");
  1044. /* 打开CSDN APP 小程序看全文 */
  1045. waitForElementToRemove("div.feed-Sign-weixin");
  1046. /* ios版本提示 */
  1047. waitForElementToRemove("div.ios-shadowbox");
  1048. },
  1049. run() {
  1050. this.addCSS();
  1051. let that = this;
  1052. let readyCallBack = function () {
  1053. that.removeAds();
  1054. that.refactoringRecommendation();
  1055. };
  1056. if (document.readyState !== "loading") {
  1057. readyCallBack();
  1058. } else {
  1059. document.addEventListener("DOMContentLoaded", readyCallBack);
  1060. }
  1061. },
  1062. },
  1063. /**
  1064. * 函数入口
  1065. */
  1066. run() {
  1067. Optimization.csdn.PC.jumpRedirect();
  1068. if (utils.isPhone()) {
  1069. log.success("移动端模式");
  1070. this.Mobile.run();
  1071. } else {
  1072. log.success("桌面端模式");
  1073. this.PC.run();
  1074. }
  1075. },
  1076. },
  1077. huaWeiCSDN: {
  1078. /**
  1079. * 判断是否是CSDN
  1080. */
  1081. locationMatch() {
  1082. return Boolean(/huaweicloud.csdn.net/i.test(window.location.origin));
  1083. },
  1084. PC: {
  1085. addCSS() {
  1086. GM_addStyle(`
  1087. /* 底部免费抽xxx奖品广告 */
  1088. div.siderbar-box,
  1089. /* 华为开发者联盟加入社区 */
  1090. div.user-desc.user-desc-fix,
  1091. /* 点击阅读全文 */
  1092. div.article-show-more{
  1093. display: none !important;
  1094. }
  1095.  
  1096. /* 自动展开全文 */
  1097. .main-content .user-article{
  1098. height: auto !important;
  1099. overflow: auto !important;
  1100. }
  1101. `);
  1102. },
  1103. run() {
  1104. this.addCSS();
  1105. this.huaweiCSDNShieldCloudDeveloperTaskChallengeEvent();
  1106. this.huaweiCSDNShieldLeftFloatingButton();
  1107. this.huaweiCSDNBlockRightColumn();
  1108. this.huaweiCSDNBlockRecommendedContentAtTheBottom();
  1109. this.huaweiCSDNShieldTheBottomForMoreRecommendations();
  1110. },
  1111. /**
  1112. * 屏蔽云开发者任务挑战活动
  1113. */
  1114. huaweiCSDNShieldCloudDeveloperTaskChallengeEvent() {
  1115. let GM_cookie = new utils.GM_Cookie();
  1116. GM_cookie.set({ name: "show_join_group_index", value: 1 });
  1117. log.success("屏蔽云开发者任务挑战活动");
  1118. },
  1119. /**
  1120. * 屏蔽左侧悬浮按钮
  1121. */
  1122. huaweiCSDNShieldLeftFloatingButton() {
  1123. if (!GM_Menu.get("huaweiCSDNShieldLeftFloatingButton")) {
  1124. return;
  1125. }
  1126. log.success(
  1127. "屏蔽左侧悬浮按钮,包括当前阅读量、点赞按钮、评论按钮、分享按钮"
  1128. );
  1129. GM_addStyle(`
  1130. div.toolbar-wrapper.article-interact-bar{
  1131. display: none !important;
  1132. }`);
  1133. },
  1134. /**
  1135. * 屏蔽右侧栏
  1136. */
  1137. huaweiCSDNBlockRightColumn() {
  1138. if (!GM_Menu.get("huaweiCSDNBlockRightColumn")) {
  1139. return;
  1140. }
  1141. log.success("屏蔽右侧栏,包括相关产品-活动日历-运营活动-热门标签");
  1142. GM_addStyle(`
  1143. div.page-home-right.dp-aside-right{
  1144. display: none !important;
  1145. }
  1146. `);
  1147. },
  1148. /**
  1149. * 屏蔽底部推荐内容
  1150. */
  1151. huaweiCSDNBlockRecommendedContentAtTheBottom() {
  1152. if (!GM_Menu.get("huaweiCSDNBlockRecommendedContentAtTheBottom")) {
  1153. return;
  1154. }
  1155. log.success("屏蔽底部推荐内容");
  1156. GM_addStyle(`
  1157. div.recommend-card-box{
  1158. display: none !important;
  1159. }`);
  1160. },
  1161. /**
  1162. * 屏蔽底部更多推荐
  1163. */
  1164. huaweiCSDNShieldTheBottomForMoreRecommendations() {
  1165. if (!GM_Menu.get("huaweiCSDNShieldTheBottomForMoreRecommendations")) {
  1166. return;
  1167. }
  1168. log.success("屏蔽底部更多推荐");
  1169. GM_addStyle(`
  1170. div.more-article{
  1171. display: none !important;
  1172. }`);
  1173. },
  1174. },
  1175. },
  1176. };
  1177. if (Optimization.huaWeiCSDN.locationMatch()) {
  1178. GM_Menu.add([
  1179. {
  1180. key: "huaweiCSDNShieldCloudDeveloperTaskChallengeEvent",
  1181. text: "电脑-屏蔽云开发者任务挑战活动",
  1182. enable: true,
  1183. },
  1184. {
  1185. key: "huaweiCSDNShieldLeftFloatingButton",
  1186. text: "电脑-屏蔽左侧悬浮按钮",
  1187. title: "包括当前阅读量、点赞按钮、评论按钮、分享按钮",
  1188. },
  1189. {
  1190. key: "huaweiCSDNBlockRightColumn",
  1191. text: "电脑-屏蔽右侧",
  1192. title: "包括相关产品-活动日历-运营活动-热门标签",
  1193. },
  1194. {
  1195. key: "huaweiCSDNBlockRecommendedContentAtTheBottom",
  1196. text: "电脑-屏蔽底部推荐内容",
  1197. },
  1198. {
  1199. key: "huaweiCSDNShieldTheBottomForMoreRecommendations",
  1200. text: "电脑-屏蔽底部更多推荐",
  1201. },
  1202. ]);
  1203. Optimization.huaWeiCSDN.PC.run();
  1204. } else if (Optimization.csdn.locationMatch()) {
  1205. if (utils.isPhone()) {
  1206. GM_Menu.add([
  1207. {
  1208. key: "showDirect",
  1209. text: "手机-标识处理过的底部推荐文章",
  1210. enable: true,
  1211. },
  1212. {
  1213. key: "openNewTab",
  1214. text: "手机-底部推荐文章新标签页打开",
  1215. enable: true,
  1216. },
  1217. {
  1218. key: "removeCSDNDownloadMobile",
  1219. text: "手机-移除文章底部的CSDN下载",
  1220. },
  1221. ]);
  1222. } else {
  1223. GM_Menu.add([
  1224. {
  1225. key: "removeCSDNDownloadPC",
  1226. text: "电脑-屏蔽底部推荐文章的CSDN下载",
  1227. },
  1228. {
  1229. key: "shieldLoginDialog",
  1230. text: "电脑-屏蔽登录弹窗",
  1231. enable: true,
  1232. callback(data) {
  1233. if (!data.enable) {
  1234. window.GM_CSS_GM_shieldLoginDialog?.forEach((item) => {
  1235. item.remove();
  1236. });
  1237. } else {
  1238. if (typeof window.GM_CSS_GM_shieldLoginDialog !== "undefined") {
  1239. window.GM_CSS_GM_shieldLoginDialog = [
  1240. ...window.GM_CSS_GM_shieldLoginDialog,
  1241. GM_addStyle(
  1242. `.passport-login-container{display: none !important;}`
  1243. ),
  1244. ];
  1245. } else {
  1246. window.GM_CSS_GM_shieldLoginDialog = [
  1247. GM_addStyle(
  1248. `.passport-login-container{display: none !important;}`
  1249. ),
  1250. ];
  1251. }
  1252. }
  1253. },
  1254. },
  1255. {
  1256. key: "csdnShieldfloatingButton",
  1257. text: "电脑-屏蔽右侧悬浮按钮",
  1258. },
  1259. {
  1260. key: "csdnShieldBottomRecommendArticle",
  1261. text: "电脑-屏蔽底部推荐文章",
  1262. },
  1263. {
  1264. key: "csdnShieldBottomFloatingToolbar",
  1265. text: "电脑-屏蔽底部悬浮工具栏",
  1266. },
  1267. {
  1268. key: "articleCenter",
  1269. text: "电脑-全文居中",
  1270. enable: true,
  1271. },
  1272. {
  1273. key: "autoExpandContent",
  1274. text: "电脑-自动展开内容块",
  1275. },
  1276. {
  1277. key: "showOrHideDirectory",
  1278. text: "电脑-显示目录",
  1279. },
  1280. {
  1281. key: "showOrHideSidebar",
  1282. text: "电脑-显示侧边栏",
  1283. },
  1284. ]);
  1285.  
  1286. GM_Menu.add({
  1287. key: "gotoCSDNCKnow",
  1288. text: "⚙ 前往C知道",
  1289. autoReload: false,
  1290. showText(text) {
  1291. return text;
  1292. },
  1293. callback() {
  1294. window.open("https://so.csdn.net/so/ai?", "_blank");
  1295. },
  1296. });
  1297. }
  1298.  
  1299. Optimization.csdn.run();
  1300. } else if (Optimization.jianshu.locationMatch()) {
  1301. if (utils.isPhone()) {
  1302. GM_Menu.add([
  1303. {
  1304. key: "JianShuremoveFooterRecommendRead",
  1305. text: "手机-移除底部推荐阅读",
  1306. },
  1307. {
  1308. key: "JianShuShieldUserComments",
  1309. text: "手机-屏蔽评论区",
  1310. },
  1311. ]);
  1312. } else {
  1313. GM_Menu.add([
  1314. {
  1315. key: "JianShuArticleCenter",
  1316. text: "电脑-全文居中",
  1317. enable: true,
  1318. },
  1319. {
  1320. key: "JianShuShieldRelatedArticles",
  1321. text: "电脑-屏蔽相关文章",
  1322. },
  1323. {
  1324. key: "JianShuShieldUserComments",
  1325. text: "电脑-屏蔽评论区",
  1326. },
  1327. {
  1328. key: "JianShuShieldRecommendedReading",
  1329. text: "电脑-屏蔽推荐阅读",
  1330. },
  1331. ]);
  1332. }
  1333.  
  1334. Optimization.jianshu.run();
  1335. }
  1336. })();