打印助手

适用于知乎和CSDN打印工具

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         打印助手
// @namespace    http://tampermonkey.net/
// @namespace    https://greasyfork.org/zh-CN/scripts/458485
// @version      0.1.2
// @description  适用于知乎和CSDN打印工具
// @author       Snape-max
// @match        https://zhuanlan.zhihu.com/p/*
// @match        https://blog.csdn.net/*/article/details/*
// @icon         https://www.google.com/s2/favicons?sz=64&domin=zhuanlan.zhihu.com
// @license MIT
// ==/UserScript==


(function() {
    'use strict';

    // 知乎
    if (window.location.href.includes('zhuanlan.zhihu.com')) {
        var floatingButton = document.createElement('button');
        floatingButton.innerHTML = '保存为PDF';
        // 设置按钮样式
        floatingButton.style.position = 'fixed';
        floatingButton.style.top = '5px';
        floatingButton.style.left = '20px';
        floatingButton.style.zIndex = '9999';
        floatingButton.style.padding = '10px';
        floatingButton.style.backgroundColor = '#3498db'; // 背景色
        floatingButton.style.color = '#fff'; // 文字颜色
        floatingButton.style.border = 'none'; // 去除边框
        floatingButton.style.borderRadius = '5px'; // 圆角
        document.body.appendChild(floatingButton);
        floatingButton.addEventListener('click', function() {
            var titlebar = document.querySelector('.ColumnPageHeader-Wrapper');
            var sidebar = document.querySelector('.Post-SideActions');
            var buttombar = document.querySelector('.Sticky.RichContent-actions.is-bottom');
            var loginber = document.querySelector('.css-woosw9');
            var topicbar = document.querySelector('.Post-Sub.Post-NormalSub');
            var connerbar = document.querySelector('.CornerButtons');
            if (titlebar){
                titlebar.remove();
            }
            if (sidebar){
                sidebar.remove();
            }
            if (buttombar){
                buttombar.remove();
            }
            if (loginber){
                loginber.remove();
            }
            if (topicbar){
                topicbar.remove();
            }
            if (connerbar){
                connerbar.remove();
            }
            window.print();
            window.addEventListener('afterprint', function () {
                location.reload();
            });
        });

    }

    // csdn
    if (window.location.href.includes('blog.csdn.net')) {
        var floatingButton = document.createElement('button');
        floatingButton.innerHTML = '保存为PDF';
        // 设置按钮样式
        floatingButton.style.position = 'fixed';
        floatingButton.style.top = '5px';
        floatingButton.style.left = '20px';
        floatingButton.style.zIndex = '9999';
        floatingButton.style.padding = '10px';
        floatingButton.style.backgroundColor = '#fc5531'; // 背景色
        floatingButton.style.color = '#fff'; // 文字颜色
        floatingButton.style.border = 'none'; // 去除边框
        floatingButton.style.borderRadius = '5px'; // 圆角
        document.body.appendChild(floatingButton);
        floatingButton.addEventListener('click', function() {
            var selectedElement = document.getElementsByClassName('blog-content-box'); // 替换为实际元素的ID

            var allElements = document.body.getElementsByTagName('*');
            for (var j = 0; j < allElements.length; j++) {
                if (!selectedElement[0].contains(allElements[j])) {
                    allElements[j].style.display = 'none';
                }
            }



            for (var i = 0; i < selectedElement.length; i++) {
                selectedElement[i].style.display = "block";

                var currenteleme  = selectedElement[i];
                while (currenteleme != null) {
                    currenteleme.style.display='block';
                    currenteleme = currenteleme.parentElement;
                }
            }

            window.print();
            window.addEventListener('afterprint', function () {
                location.reload();
            });
        });

    }

})();