Greasy Fork

Greasy Fork is available in English.

SaveToPDF

52pojie、CSDN、简书、Myitmx、博客园文章一键保存为PDF

当前为 2022-03-31 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         SaveToPDF
// @namespace    https://www.null119.cn
// @version      0.8
// @description  52pojie、CSDN、简书、Myitmx、博客园文章一键保存为PDF
// @author       null119
// @match        https://www.52pojie.cn/*
// @match        https://blog.csdn.net/*
// @match        https://www.myitmx.com/*
// @match        https://www.jianshu.com/*
// @match        https://www.cnblogs.com/*
// @icon         https://www.null119.cn/usr/uploads/img/202202140807071.png
// @grant        none
// ==/UserScript==

(function() {
    "use strict";
    var host,url,wtype,imgurl
    imgurl='https://gitee.com/null119/mypic/raw/master/img/202202150530227.png';
    window.onload=function(){
        var up,nxg1,newa,nimg,ndiv,allshow;
        url=window.location.href;
        up="//.*?\\.(.*?)\\."
        host=url.match(up)[1];
        if(host==="52pojie"){
            wtype=0
        }
        if(host==="csdn"){
            wtype=1
        }
        if(host==="myitmx"){
            wtype=2
        }
        if(host==="jianshu"){
            wtype=3
        }
        if(host==="cnblogs"){
            wtype=4
        }
        switch(wtype) {
            case 0:
                nxg1 = document.getElementById("post_reply");
                if (nxg1) {
                    nimg=document.createElement("img");
                    nimg.src="https://gitee.com/null119/mypic/raw/master/img/202202140612986.png";
                    newa = document.createElement("a");
                    newa.id="HtmToPDF";
                    newa.href="javascript:SaveToPDF();";
                    newa.title="收藏";
                    newa.appendChild(nimg);
                    nxg1.parentNode.insertBefore(newa,nxg1.nextSibling);
                    var doc=document.getElementsByClassName("zoom")
                    for(var i=0;i<doc.length;i++){
                        doc[i].setAttribute("src",doc[i].getAttribute("file"));
                    }
                }
                break;
            case 1:
                setTimeout(1500);
                nxg1 = document.getElementsByClassName("opt-letter-watch-box");
                if (nxg1) {
                    newa = document.createElement("a");
                    newa.setAttribute("class","personal-watch bt-button");
                    newa.id="HtmToPDF";
                    newa.href="javascript:SaveToPDF();";
                    newa.text="收藏";
                    ndiv=document.createElement("div");
                    ndiv.setAttribute("class","opt-letter-watch-box");
                    ndiv.appendChild(newa);
                    nxg1[1].parentNode.insertBefore(ndiv,nxg1[1].nextSibling);
                    allshow=document.getElementById("btn-readmore-zk");
                    if(allshow){
                        allshow.click()
                    };
                    var zk=document.getElementsByClassName("look-more-preCode contentImg-no-view");
                    if(zk.length>0){
                        for (var j = 0; j<zk.length; j++) {
                            (function (j) {
                                setTimeout(function () {
                                    zk[j].click();
                                },j * 100);
                            })(j);
                        }
                    }
                }
                break;
            case 2:
                nxg1 = document.getElementById("navbarCollapse");
                if (nxg1) {
                    nimg=document.createElement("img");
                    nimg.src=imgurl;
                    newa = document.createElement("a");
                    newa.id="HtmToPDF";
                    newa.setAttribute("class","search-form-input");
                    newa.href="javascript:SaveToPDF();";
                    newa.title="收藏";
                    newa.appendChild(nimg);
                    nxg1.parentNode.insertBefore(newa,nxg1.nextSibling);
                }
                break;
            case 3:
                nxg1 = document.getElementsByClassName("_1pUUKr")[2];
                if (nxg1) {
                    nimg=document.createElement("img");
                    nimg.src=imgurl;
                    newa = document.createElement("a");
                    newa.id="HtmToPDF";
                    newa.setAttribute("class","search-form-input");
                    newa.href="javascript:SaveToPDF();";
                    newa.title="收藏";
                    newa.appendChild(nimg);
                    ndiv=document.createElement("div");
                    ndiv.setAttribute("class","_1pUUKr");
                    ndiv.setAttribute("align","center");
                    ndiv.appendChild(newa);
                    nxg1.parentNode.insertBefore(ndiv,nxg1.nextSibling);
                }
                break;
            case 4:
                nxg1 = document.getElementById("blog_nav_admin");
                if (nxg1) {
                    newa = document.createElement("a");
                    newa.id="HtmToPDF";
                    newa.href="javascript:SaveToPDF();";
                    newa.title="收藏";
                    newa.text="收藏";
                    ndiv=document.createElement("li");
                    ndiv.setAttribute("id","blog_save");
                    ndiv.setAttribute("align","center");
                    ndiv.appendChild(newa);
                    nxg1.parentNode.insertBefore(ndiv,nxg1.nextSibling);
                }
                break;
        }
    };
    window.SaveToPDF=function(){
        var htmldoc,test,idstr,sprnstr,eprnstr,instart,s_a,e_a,bdhtml,prnhtml,title
        sprnstr="LS1zdGFydHByaW50LS0=";
        eprnstr="LS1lbmRwcmludC0t";
        htmldoc=document.documentElement.outerHTML;
        switch(wtype) {
            case 0:
                test="comment_(\\d+)";
                idstr="postmessage_"+htmldoc.match(test)[1];
                instart=document.getElementById(idstr);
                title=document.getElementById("thread_subject").innerText;
                break;
            case 1:
                instart=document.getElementById("article_content");
                title=document.getElementById("articleContentId").innerText;
                break
            case 2:
                instart=document.getElementsByClassName("post-content")[0];
                title=document.getElementsByClassName("blog-title")[0].innerText;
                break
            case 3:
                instart=document.getElementsByClassName("_2rhmJa")[0];
                title=document.getElementsByClassName("_1RuRku")[0].innerText;
                break
            case 4:
                instart=document.getElementById('cnblogs_post_body');
                title=document.getElementById("cb_post_title_url").innerText;
                console.log(title);
                break;
        }
        s_a=document.createElement("a");
        s_a.text=atob(sprnstr);
        s_a.style.display = "none";
        instart.parentNode.insertBefore(s_a,instart);
        e_a=document.createElement("a");
        e_a.text=atob(eprnstr);
        e_a.style.display = "none";
        instart.parentNode.insertBefore(e_a,instart.nextSibling);
        bdhtml=window.document.body.innerHTML;
        prnhtml=bdhtml.substr(bdhtml.indexOf(atob(sprnstr))+14);
        prnhtml=prnhtml.substring(0,prnhtml.indexOf(atob(eprnstr)));
        if(wtype===3){
            prnhtml=prnhtml.replace(/src="\/\/upload/g, 'src="https://upload');
            prnhtml=prnhtml.replace(/<div class="image-container-fill".*?\/div>/g,'');
        }
        const newWin = window.open("");
        newWin.document.body.innerHTML = '<html><title>'+title+'</title><h1><a href="'+url+'">'+title+"</a></h1>"+prnhtml+"</html>";
        newWin.document.close();
        newWin.focus();
        setTimeout(() => {
            newWin.print();
            newWin.close();
        }, 300);
    };
})();