CSDN|简书优化

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

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

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