SurePrintCsdn

try to print to pdf of CSDN!

  1. // ==UserScript==
  2. // @name SurePrintCsdn
  3. // @namespace http://surewong.com/SurePrint
  4. // @version 0.3
  5. // @description try to print to pdf of CSDN!
  6. // @author SureWong
  7. // @license AGPL License
  8. // @match https://*.csdn.net/*
  9. // @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. /* jshint esversion: 11 */
  14. (function() {
  15. 'use strict';
  16.  
  17. // Your code here...
  18. var surePrintBtn = document.createElement('button');
  19. surePrintBtn.innerHTML = "准备打印pdf";
  20. surePrintBtn.className = "sure-print-pdf";
  21.  
  22. surePrintBtn.onclick = function (e) {
  23. document.querySelector("#side")?.remove();
  24. document.querySelector("#comment_title, #comment_list, #comment_bar, #comment_form, .announce, #ad_cen, #ad_bot")?.remove();
  25. document.querySelector(".nav_top_2011, #header, #navigator")?.remove();
  26. document.querySelector(".p4course_target, .comment-box, .recommend-box, #csdn-toolbar, #tool-box")?.remove();
  27. document.querySelector("aside")?.remove();
  28. document.querySelector("#toolbarBox")?.remove();
  29. document.querySelector(".tool-box")?.remove();
  30. document.querySelector(".more-toolbox-new")?.remove();
  31. document.querySelector(".csdn-side-toolbar")?.remove();
  32. document.querySelector(".more-toolbox")?.remove();
  33. document.querySelector(".template-box")?.remove();
  34. document.querySelector(".bottom-pub-footer")?.remove();
  35. document.querySelector(".pre-numbering")?.remove();
  36. document.querySelector("main").setAttribute('style', 'display: content;');
  37. document.querySelector("main").setAttribute('style', 'float: left;');
  38.  
  39. // 打开代码折叠
  40. var elements = document.querySelectorAll('.set-code-hide');
  41. elements.forEach(function(element) {
  42. element.className = element.className.replace('set-code-hide', 'set-code-show');
  43. });
  44.  
  45. var styleElements = document.getElementsByTagName("style");
  46. for(var i = 0; i < styleElements.length; i++) {
  47. if(styleElements[i].textContent.includes(".print_watermark") || styleElements[i].textContent.includes("@page")) {
  48. styleElements[i].parentNode.removeChild(styleElements[i]);
  49. }
  50. }
  51.  
  52.  
  53. // 新增代码:为打印增加@page属性
  54. var printStyle = document.createElement('style');
  55. var printStyleContent =`
  56. @media print {
  57. @page {
  58. margin-top:80px;
  59. margin-bottom: 80px;
  60. size: portrait; /* 纵向打印 */
  61. }
  62. .main_father > #mainBox {
  63. width: 100%;
  64. margin-left:200px;
  65. margin-right:200px;
  66. padding:0px;
  67. }
  68. .blog-content-box {
  69. width: 85%;
  70. }
  71. body, article {
  72. width: 80%;
  73. }
  74. /* 防止文字溢出打印区域 */
  75. p, h1, h2, h3, h4, h5, h6 {
  76. overflow-wrap: break-word;
  77. word-wrap: break-word;
  78. -ms-word-break: break-all;
  79. word-break: break-word;
  80. }
  81. h1, h2, h3 {
  82. page-break-after: avoid; /* 避免标题后直接分页 */
  83. }
  84. tr {
  85. page-break-inside: avoid; /* 确保表格行不会跨页打断 */
  86. }
  87. }`;
  88. printStyle.innerHTML = printStyleContent;
  89. document.head.appendChild(printStyle);
  90.  
  91. };
  92.  
  93. var body = document.body;
  94. var style = document.createElement('style');
  95. style.id = "sure-print-pdf";
  96. var css = `.sure-print-pdf{
  97. position: fixed;
  98. bottom: 5%;
  99. right: 1%;
  100. width: 70px;
  101. height: 70px;
  102. background: #add8e640;
  103. color: cornflowerblue;
  104. border-radius: 50%;
  105. font-size: 10px;
  106. z-index: 999;
  107. cursor: pointer;
  108. font-size: 10px;
  109. overflow: hidden;
  110. }`;
  111. if (style.styleSheet) {
  112. style.styleSheet.cssText = css;
  113. } else {
  114. style.appendChild(document.createTextNode(css));
  115. }
  116. body.appendChild(surePrintBtn);
  117. body.appendChild(style);
  118. })();