油猴脚本打印博客文章

写了一个简单的油猴脚本,打印平常阅读的博客文章

目前支持的网站如下:

  • CSDN
  • 知乎
  • 简书
  • SegmentFault

博客园由于主题太多,无法使用统一方法打印。

脚本内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// ==UserScript==
// @name 打印CSDN、知乎、简书、SF文章
// @namespace http://tampermonkey.net/
// @version 0.1
// @description CSDN、知乎、简书、SegmentFault
// @author kervias
// @match https://zhuanlan.zhihu.com/p/*
// @match https://blog.csdn.net/*/article/details/*
// @match https://csdnnews.blog.csdn.net/article/details/*
// @match https://www.jianshu.com/p/*
// @match https://segmentfault.com/a/*
// @require https://cdn.bootcss.com/jquery/3.5.1/jquery.min.js

// @grant unsafeWindow
// @grant GM_openInTab
// @grant GM.openInTab
// @grant GM_getValue
// @grant GM.getValue
// @grant GM_setValue
// @grant GM.setValue
// @grant GM_xmlhttpRequest
// @grant GM.xmlHttpRequest
// @grant GM_registerMenuCommand

// ==/UserScript==

(function() {
'use strict';

var $ = $ || window.$;
var CSDN_PRINT = function (){
$("#side").remove();
$("#comment_title, #comment_list, #comment_bar, #comment_form, .announce, #ad_cen, #ad_bot").remove();
$(".nav_top_2011, #header, #navigator").remove();
$(".csdn-side-toolbar,.template-box,.reward-user-box").remove();
$(".p4course_target, .comment-box, .recommend-box, #csdn-toolbar, #tool-box,#dmp_ad_58").remove();
$("aside").remove();
$(".tool-box").remove();
$("main").css('display','content');
$(".left-toolbox").remove();
$("#mainBox").removeClass();
$("#mainBox").parent().removeClass();
$('body').attr("style", "position: absolute;left: 0px;right: 0px;background-color: white")
$('main').attr("style", "position: absolute;left: 0px;right: 0px;margin-bottom: 0px");
$('main div.blog-content-box').attr("style", "padding-bottom: 0px");
$(".blog-footer-bottom").remove();
//$('.htmledit_views p').remove();
window.print();
};

var ZHIHU_PRINT = function (){
$(".ColumnPageHeader").remove();
//$(".PostIndex-Contributions").remove();
$(".Recommendations-Main").remove();
$(".Comments-container").remove();
$(".ContentItem-actions").parent().remove();
$(".Zi--BackToTop").remove();
window.print();
};

var JIANSHU_PRINT = function(){
$("header").remove();
$("aside").remove();
$("footer, footer+div").remove();
$("#note-page-comment, section:gt(0), [role='button'], .ant-back-top, .anticon-caret-up").remove();
$("svg").remove();
$("section").attr("style", "position: absolute;left: 0px;right: 0px;margin-bottom: 0px");
$("article").nextAll().remove();
window.print();
};

var SF_PRINT = function(){
$("#sf-header, #comment-area, footer, .text-center, #fixedTools").remove();
$("article").nextAll().remove();
$("div.row").children().eq(1).remove();
$("div.row > div > div").eq(0).nextAll().remove();
$("#sf-article_title h5").remove();
$("div.row").attr("style", "position: absolute;left: 0px;right: 0px;margin-bottom: 0px");
window.print();
};


var domain = document.domain;
if(domain.endsWith('zhihu.com')){ // 双击标题打印文章
$(".Post-Title").on('dblclick',function(e){
ZHIHU_PRINT();
});
}
else if(domain.endsWith('csdn.net')){ // 双击标题打印文章
$("#articleContentId").on('dblclick',function(e){
CSDN_PRINT();
});
}
else if(domain.endsWith('jianshu.com')){ // 双击标题打印文章
$("section > h1").on('dblclick', function(e){
JIANSHU_PRINT();
});
}
else if(domain.endsWith('segmentfault.com')){ // 点击标题后按钮打印文章
var cont = $('<h5 style="display:inline"><button id="tm_print">打印</button></h5>');
$("#sf-article_title").append(cont);
$("#sf-article_title h5").on('click', function(e){
SF_PRINT();
});
}

})();