SurePrintCsdn

try to print to pdf of CSDN!

当前为 2024-03-29 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         SurePrintCsdn
// @namespace    http://surewong.com/SurePrint
// @version      0.2
// @description  try to print to pdf of CSDN!
// @author       SureWong
// @license      AGPL License
// @match        https://*.csdn.net/*
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant        none
// ==/UserScript==

/* jshint esversion: 11 */
(function() {
    'use strict';

    // Your code here...
    var surePrintBtn = document.createElement('button');
    surePrintBtn.innerHTML = "准备打印pdf";
    surePrintBtn.className = "sure-print-pdf";

    surePrintBtn.onclick = function (e) {
      document.querySelector("#side")?.remove();
      document.querySelector("#comment_title, #comment_list, #comment_bar, #comment_form, .announce, #ad_cen, #ad_bot")?.remove();
      document.querySelector(".nav_top_2011, #header, #navigator")?.remove();
      document.querySelector(".p4course_target, .comment-box, .recommend-box, #csdn-toolbar, #tool-box")?.remove();
      document.querySelector("aside")?.remove();
      document.querySelector("#toolbarBox")?.remove();
      document.querySelector(".tool-box")?.remove();
      document.querySelector(".more-toolbox-new")?.remove();
      document.querySelector(".csdn-side-toolbar")?.remove();
      document.querySelector(".more-toolbox")?.remove();
      document.querySelector(".template-box")?.remove();
      document.querySelector(".bottom-pub-footer")?.remove();
      document.querySelector(".pre-numbering")?.remove();
      document.querySelector("main").setAttribute('style', 'display: content;');
      document.querySelector("main").setAttribute('style', 'float: left;');

      // 打开代码折叠
      var elements = document.querySelectorAll('.set-code-hide');
      elements.forEach(function(element) {
      element.className = element.className.replace('set-code-hide', 'set-code-show');
      });

      var styleElements = document.getElementsByTagName("style");
      for(var i = 0; i < styleElements.length; i++) {
          if(styleElements[i].textContent.includes(".print_watermark") || styleElements[i].textContent.includes("@page")) {
              styleElements[i].parentNode.removeChild(styleElements[i]);
          }
      }


      // 新增代码:为打印增加@page属性
      var printStyle = document.createElement('style');
      var printStyleContent =`
    @media print {
        @page {
            margin: 20mm;
            size: portrait; /* 纵向打印 */
        }
        body, article {
            width: 100%;
            margin: 0;
            padding: 0;
        }
        .blog-content-box {
            width: 75%;
            padding: 0;
        }
        .main_father > #mainBox {
            width: unset
        }
        h1, h2, h3 {
            page-break-after: avoid; /* 避免标题后直接分页 */
        }
        tr {
            page-break-inside: avoid; /* 确保表格行不会跨页打断 */
        }
     }`;
      printStyle.innerHTML = printStyleContent;
      document.head.appendChild(printStyle);

    };

    var body = document.body;
    var style = document.createElement('style');
    style.id = "sure-print-pdf";
    var css = `.sure-print-pdf{
      position: fixed;
      bottom: 5%;
      right: 1%;
      width: 70px;
      height: 70px;
      background: #add8e640;
      color: cornflowerblue;
      border-radius: 50%;
      font-size: 10px;
      z-index: 999;
      cursor: pointer;
      font-size: 10px;
      overflow: hidden;
    }`;
    if (style.styleSheet) {
      style.styleSheet.cssText = css;
    } else {
      style.appendChild(document.createTextNode(css));
    }
    body.appendChild(surePrintBtn);
    body.appendChild(style);
})();