// ==UserScript==
// @name 文章导出成pdf
// @namespace http://tampermonkey.net/
// @version 0.4
// @description 将一些主流的网站的文章,去除掉一些无关部分直接启动浏览器自带打印功能
// @author Vanisper
// @match https://zhuanlan.zhihu.com/p/*
// @match https://blog.csdn.net/*/article/details/*
// @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';
// ------------------------------------------
// 构建遮罩层
const mod = document.createElement('div');
mod.setAttribute("style","height:100vh;width:100vw;position: fixed;top: 0;left: 0;background-color: #8ae79d82;pointer-events: auto;z-index: 99999;");
mod.setAttribute("class","mod");
//-------------------------------------------
// 创建 loading 元素
const loading = document.createElement('div');
loading.id = 'loading';
loading.style.position = 'fixed';
loading.style.display = 'flex';
loading.style.top = '0';
loading.style.left = '0';
loading.style.width = '100%';
loading.style.height = '100%';
loading.style.backgroundColor = 'rgba(0, 0, 0, 0.5)';
loading.style.zIndex = '9999';
// 创建 spinner 元素
const spinner = document.createElement('div');
spinner.className = 'spinner';
spinner.style.margin = 'auto';
spinner.style.width = '40px';
spinner.style.height = '40px';
spinner.style.borderRadius = '50%';
spinner.style.border = '3px solid transparent';
spinner.style.borderTopColor = '#fff';
spinner.style.animation = 'spin 0.8s ease infinite';
// 创建 keyframes
const keyframes = document.createElement('style');
keyframes.innerHTML = `@keyframes spin {to {transform: rotate(360deg);}}`;
// 将 spinner 和 keyframes 添加到 loading 元素中
loading.appendChild(spinner);
document.head.appendChild(keyframes);
// 显示 Loading
function showLoading() {
document.body.appendChild(loading);
}
// 隐藏 Loading
function hideLoading() {
document.body.removeChild(loading);
}
// ------------------------------------------
// 将loading挂载到遮罩层上
mod.appendChild(loading);
// ------------------------------------------
function preventDefault(e) {
e.preventDefault();
}
// ------------------------------------------
// 循环滚动
function smoothScrollToBottom() {
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) {
// 到达底部,停止滚动
mod.remove(); // 移除遮罩层
window.removeEventListener('wheel', preventDefault, { passive: false }); // 恢复鼠标滚动事件
window.print(); // 调用浏览器打印事件
return;
} else {
// 滚动到下一页
window.scrollTo({
top: scrollTop + clientHeight,
behavior: 'auto' // 可以设置成光滑的
});
// 延时执行,等待页面加载完成
setTimeout(smoothScrollToBottom, 1000);
}
}
// ------------------------------------------
const urls = [{
r: "https?://zhuanlan.zhihu.com/p/\\w+",
n: "zhihu",
s: "article",
class: ".Catalog, .ColumnPageHeader-Wrapper, .RichContent-actions, .RichContent-actions, .Post-NormalSub, .Post-SideActions, .setpdf, .complementary, .CornerAnimayedFlex",
style: "Button CornerButton Button--red",
},{
r: "https?://blog\\.csdn\\.net/\\w+/article/details/\\w+",
n: "csdn",
s: ".csdn-side-toolbar",
class: "#mainBox > aside, #csdn-toolbar, body > div:nth-child(49) > div, #toolBarBox,#mainBox > main > div.recommend-box, #recommendNps,#copyright-box, #treeSkill > div",
style: "option-box",
}];
var currentUrl = window.location.href;
var flag = false;
var curr = {r: "", n: "", s: "", class: "", style: ""};
// ------------------------------------------
// 检测当前url是否是预设的
urls.some((e, _i)=>{
var reg = new RegExp(e.r);
if(reg.test(currentUrl)){
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');
if(curr.n=="csdn"){
style.innerHTML = `@media print {
${curr.class}, .mod { display: none; }
#mainBox > main > div.blog-content-box{ position: absolute; top: 0; left: 0; max-width: 1080px; z-index: 999;}
main div.blog-content-box pre.set-code-hide { height: auto; overflow-y: auto; }
main div.blog-content-box pre.set-code-hide .hide-preCode-box { display: none; }
#article_content .markdown_views pre.prettyprint * { white-space: pre-wrap; word-break: break-word; word-wrap: normal; }
}`;
}else {
style.innerHTML = `@media print {${curr.class}, .mod {display: none;}}`;
}
document.head.appendChild(style);
var dom = document.querySelector(curr.s);
const btn = document.createElement("a");
btn.appendChild(document.createTextNode("导出pdf"));
btn.setAttribute('style', 'position: fixed;top: 50%;right: 200px;background: none;border: 1px solid;border-radius: 3px;cursor: pointer;display: inline-block;font-size: 14px;line-height: 32px;padding: 0 16px;text-align: center;color: blueviolet;');
btn.setAttribute('class', `setpdf`);
// btn.setAttribute('class', `setpdf ${curr.style}`);
dom.appendChild(btn);
btn.onclick = ()=>{
document.body.appendChild(mod);
// 禁用滚动条的滚轮事件
window.addEventListener('wheel', preventDefault, { passive: false });
window.scrollTo(0, 0);
smoothScrollToBottom();
}
});
return;
}
})();