Greasy Fork

Greasy Fork is available in English.

文章导出成pdf

将一些主流的网站的文章,去除掉一些无关部分直接启动浏览器自带打印功能

当前为 2023-03-28 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         文章导出成pdf
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  将一些主流的网站的文章,去除掉一些无关部分直接启动浏览器自带打印功能
// @author       Vanisper
// @match        https://zhuanlan.zhihu.com/p/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=zhihu.com
// @require      https://unpkg.com/[email protected]/dist/jspdf.umd.min.js
// @require      https://cdn.bootcdn.net/ajax/libs/html2canvas/1.4.1/html2canvas.min.js
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';
    // Your code here...
    // window.jsPDF = window.jspdf.jsPDF;
    function smoothScrollToBottom() {
        window.scrollTo(0, 0);
        var scrollHeight = document.body.scrollHeight;
        var scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
        var clientHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;

        if (scrollTop + clientHeight >= scrollHeight) {
            // 到达底部,停止滚动
            window.print();
            return;
        } else {
            // 滚动到下一页
            window.scrollTo({
                top: scrollTop + clientHeight,
                behavior: 'auto'
            });
            // 延时执行,等待页面加载完成
            setTimeout(smoothScrollToBottom, 200);
        }
    }
    const urls = [{
        r: "zhuanlan.zhihu.com/p/",
        n: "zhihu",
        s: "article",
        class: ".ColumnPageHeader-Wrapper, .RichContent-actions, .RichContent-actions, .Post-NormalSub, .Post-SideActions, .setpdf, .complementary, .CornerAnimayedFlex",
        style: "Button CornerButton Button--red",
    }];
    var currentUrl = window.location.href;
    var flag = false;
    var curr = {r: "", n: "", s: "",class:"",style:""};
    urls.some((e, _i)=>{
        if(currentUrl.includes(e.r)){
            curr.r = currentUrl;
            curr.n = window.location.hostname.split(".")[window.location.hostname.split(".").length - 2];
            curr.s = e.s;
            curr.class = e.class;
            curr.style = e.style;
            flag = true;
            return true;
        }
    })
    if(flag){
        window.addEventListener('load', function() {
            var style = document.createElement('style');
            style.innerHTML = `@media print {${curr.class} {display: none;}}`;
            document.head.appendChild(style);
            var dom = document.querySelector(curr.s);
            const btn = document.createElement("button");
            btn.appendChild(document.createTextNode("导出pdf"));
            btn.setAttribute('style', 'position: fixed;top: 50%;right: 200px;');
            btn.setAttribute('class', `setpdf ${curr.style}`);
            dom.appendChild(btn);
            btn.onclick = ()=>{
                smoothScrollToBottom();
            }
        });

    }

})();