-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcnblogs-customfunc.js
262 lines (246 loc) · 10.6 KB
/
cnblogs-customfunc.js
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/**
功能: 为博客自定义功能,比如推荐此文,Scrollbar;
TODO: 新的自定义功能写在此文件
参考: https://www.cnblogs.com/marvin/p/ExtendWizNoteAutoNnavigation.html
https://www.cnblogs.com/asxinyu/p/Bolg_Category_For_BlogBeauty.html
文章顶部添加版权信息:https://www.cnblogs.com/gzdaijie/p/5187171.html
TODO markdown代码显示行号:https://blog.bluerain.io/p/markdown-code-block-line-number.html
*/
//程序执行入口,当文档加载完成
$(document).ready(function () {
/*$(window).resize(function(){
if (document.getElementById("cnblogs_post_body")){
var maxWidth = $(document.body).width();
console.log("window.resize:"+$('#main').width()+",bodyWidth:"+maxWidth);
$('#mainContent').css('width',maxWidth);
}
});*/
var md = new MobileDetect(window.navigator.userAgent);
if (md.mobile() == false || md.mobile() == null) {
setArticleCopyright();
consoleInfo();
}
setCodeRow();
setMyInfo();
});
jQuery.fn.wait = function (func, times, interval) {
var _times = times || -1, //100次
_interval = interval || 20, //20毫秒每次
_self = this,
_selector = this.selector,
//选择器
_iIntervalID; //定时器id
if (this.length) { //如果已经获取到了,就直接执行函数
func && func.call(this);
} else {
_iIntervalID = setInterval(function query() {
if (!_times) { //是0就退出
clearInterval(_iIntervalID);
}
_times <= 0 || _times--; //如果是正数就 --
_self = $(_selector); //再次选择
if (_self.length) { //判断是否取到
func && func.call(_self);
clearInterval(_iIntervalID);
}
}, _interval);
}
return this;
}
function focusFunction() {
var _targetTop = $('#comment_form_container').offset().top; //获取位置
jQuery("html,body").animate({
scrollTop: _targetTop
}, 300); //跳转
}
function focusFollow() {
var _targetTop = $('#profile_block').offset().top; //获取位置
jQuery("html,body").animate({
scrollTop: _targetTop
}, 300); //跳转
}
//TODO 设置子级div的宽度,最佳解决方案:让它撑满父DIV
var calcPencent = '110%';
function getContentWidth(compareWidth) {
//在1920的分辨率中,foxmail订阅默认大小<1440
var screePix = new Array(800, 1024, 1280, 1440, 1600, 1920, 2560);
var screePencent = new Array(1.28, 1.26, 1.24, 1.22, 1.2, 1.18, 1.1);
var isGetVal = false;
$.each(screePix, function (idx, obj) {
//console.log(idx,parseInt(obj),parseInt(compareWidth));
if (parseInt(obj) >= parseInt(compareWidth) && !isGetVal) {
var calcWidth = parseInt(screePencent[idx] * compareWidth);
var tmpPencent = (screePencent[idx] * 100);
calcPencent = (tmpPencent.toString() + "%");
console.log('arrary:', idx, obj, compareWidth, calcPencent, calcWidth);
isGetVal = true;
return calcPencent;
}
});
}
function consoleInfo() {
try {
if (window.console && window.console.log) {
console.log("\n")
console.log("一位新人,要经历怎样的成长,才能站在技术之巅?\n想探寻这里面蕴含的秘密?赶快把我的博客加入收藏,添加到书签,你,可以影响世界。\n");
console.log("如果你想和我取得联系,你可以发送邮件至 %c 569032731@qq.com", "color:blue");
console.log("IM联系方式: %c QQ/Wechat: 569032731( 请附上验证信息)", "color:blue");
console.log("\n")
console.log("博客中有关的示例代码:%c https://github.com/zhaoqingqing/blog_samplecode", "color:green")
console.log("博客的主题的源码托管:%c https://github.com/zhaoqingqing/cnblogs", "color:green")
console.log("\n")
}
} catch (e) { }
}
function setMyInfo() {
$("<div id='toTop' title='回顶部'></div>").appendTo($("body"));
$("#toTop").bind("click", function () {
$("body,html").animate({
scrollTop: '0px'
}, 200);
});
$("<div id='toBottom' title='到底部'></div>").appendTo($("body"));
$("#toBottom").bind("click", function () {
//文章页下拉到评论区,其它页面拉到顶部
if (document.getElementById("blog_post_info_block")) {
$("body,html").animate({
scrollTop: $('#blog_post_info_block').offset().top
}, 150);
console.log("page is article");
} else {
$("body,html").animate({
scrollTop: $('#footer').offset().top
}, 150);
console.log("page not article");
}
});
//版权信息
var signatureHtml = "";
signatureHtml += '作者:赵青青 出处:<a href="https://www.cnblogs.com/zhaoqingqing/">https://www.cnblogs.com/zhaoqingqing/</a><br />';
signatureHtml += '关于我:乐于学习未知技术和知识,擅长Unity3D,游戏开发,.NET等领域。<br />';
signatureHtml += '本文版权归作者和博客园共有欢迎转载,转载之后请务必在文章明显位置标出原文链接和作者,谢谢。<br />';
signatureHtml += '如果本文对您有帮助,请点击<a id="recommendme",href="javascript:void(0);">【推荐】</a>您的赞赏将鼓励我继续创作!想跟我一起进步么?那就<a id="followme" href="javascript:void(0);">【关注】</a>我吧。';
signatureHtml += '<div id="signatureTips"></div>';
//BUG 在360极速浏览器8.7(chrome50)下,偶现签名栏无法显示
if ($("#cnblogs_post_body").next().attr("id") == "MySignature") {
$("#cnblogs_post_body #MySignature").hide();
$("#cnblogs_post_body").next().show().html(signatureHtml);
}
else {
$("#cnblogs_post_body #MySignature").show().html(signatureHtml);
}
//fix:把click函数写在Html代码里在渲染之后莫名多了个'='
$('#recommendme').on('click', function () {
votePost(cb_entryId, "Digg")
// console.log("recommend success");
$("#signatureTips").html("感谢您的推荐!");
});
$('#followme').on('click', function () {
cnblogs.UserManager.FollowBlogger("a7e65336-5c2c-e111-b988-842b2b196315");
// console.log("follow success");
$("#signatureTips").html("谢谢您的关注!");
});
}
/*浮动工具条*/
$("#div_digg").wait(function () {
var html = '<div style="padding-bottom: 5px;">';
if ($("#p_b_follow") != null) {
if ($("#p_b_follow").text().indexOf("加关注") != -1) {
html += '<a onclick="javascript:focusFollow();" href="javascript:void(0);">关注我</a> | ';
}
}
html += '<a onclick="javascript:focusFunction();" href="javascript:void(0);">快速评论</a>';
html += '</div>';
$(html).appendTo('#div_digg');
//console.log('wait digg');
});
$("#mainContent").wait(function () {
/* //列表页:非全屏 显示侧边内容; 正文页:全屏隐藏侧边内容
if (document.getElementById("cnblogs_post_body")){
//博客内容页面全屏
//NOTE 在IE下不用设置width,隐藏同级div,它就是全屏 v~v,but chrome并不是!
$('#sideBar').css('width','0px');
$('#sideBar').css('display','none');
var maxWidth = $(document.body).width();
console.log("#main.Width:"+$('#main').width()+",bodyWidth:"+maxWidth);
var contentWidth = $('#main').width() - 20;
getContentWidth(maxWidth);
$('#mainContent').css('width',calcPencent);
//$('#mainContent').css('position','relative');
console.log('this page fullscreen->', calcPencent.toString(),$('#mainContent').width()+"px");
}else{
//常显示右侧div
$('#sideBar').css('display','block');
$('#mainContent').css('width','100%');
console.log('this page normalsize');
}*/
$('#sideBar').css('display', 'block');
$('#mainContent').css('width', '100%');
});
//========== 文章顶部添加版权信息 start ==========
//参考:https://www.cnblogs.com/gzdaijie/p/5187171.html
function setArticleCopyright() {
//设置版权信息,转载出处自动根据页面url生成,不需要加线(hr)
var info_str = '<p>作者:<a target="_blank">@qingqing-zhao</a><br>' +
'本文为作者原创,转载请注明出处:<a class="uri"></a></p>',
info = $(info_str),
info_a = info.find("a"),
url = window.location.href;
$(info_a[0]).attr("href", "https://www.cnblogs.com/zhaoqingqing/");
$(info_a[1]).attr("href", url).text(url);
$("#cnblogs_post_body").prepend(info);
}
function setCodeRow() {
// 代码行号显示
var pre = $("pre.hljs"); //选中需要更改的部分
if (pre && pre.length) {
pre.each(function () {
var item = $(this);
var lang = item.attr("class").split(" ")[1]; //判断高亮的语言
item.html(item.html().replace(/<[^>]+>/g, "")); //将<pre>标签中的html标签去掉
item.removeClass().addClass("brush: " + lang + ";"); //根据语言添加笔刷
if (SyntaxHighlighter != null) {
SyntaxHighlighter.all();
} else {
console.log("SyntaxHighlighter 插件找不到")
}
})
}
}
//并不是所有的链接都需要在新窗口打开,比如目录锚点反而有影响
function setAtarget() {
// 博客内的链接在新窗口打开
$("#cnblogs_post_body a").each(function () {
this.target = "_blank";
})
}
//========== 文章顶部添加版权信息 end ==========
/**
* js截取字符串,中英文都能用
* 如果给定的字符串大于指定长度,截取指定长度返回,否者返回源字符串。
* @param str:需要截取的字符串
* @param len: 需要截取的长度
*/
function cutstr(str, len) {
var str_length = 0;
var str_len = 0;
str_cut = new String();
str_len = str.length;
for (var i = 0; i < str_len; i++) {
a = str.charAt(i);
str_length++;
if (escape(a).length > 4) {
//中文字符的长度经编码之后大于4
str_length++;
}
str_cut = str_cut.concat(a);
if (str_length >= len) {
str_cut = str_cut.concat("...");
return str_cut;
}
}
//如果给定字符串小于指定长度,则返回源字符串;
if (str_length < len) {
return str;
}
}