hexo魔改教程(一)
博客搭建与魔改系列教程导航🚥🚥🚥
- hexo基础教程(一)
- hexo魔改教程(一)
- hexo魔改教程(二)
- hexo魔改教程(三)
适用版本:Hexo 8.1.2+Butterfly 5.7.2。每章 10 个魔改。
新年灯笼
新年灯笼
新建 [Blogroot]\source\js\denglong.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(function () {
var src = document.currentScript.src;
var text = new URLSearchParams(src.split('?')[1] || '').get('text') || '新年快乐';
var css = `
.deng-container{position:relative;top:10px;z-index:9999;pointer-events:none;}
.deng-box{position:fixed;top:15px;}
.deng-box1{left:20px}.deng-box2{left:130px}.deng-box3{right:110px}.deng-box4{right:20px}
.deng{position:relative;width:120px;height:90px;background:rgba(216,0,15,.8);
border-radius:50%;animation:swing 3s infinite ease-in-out;
box-shadow:-5px 5px 50px 4px #fa6c00;}
.deng-a{width:100px;height:90px;margin-left:7px;border:2px solid #dc8f03;
border-radius:50%;display:flex;justify-content:center;}
.deng-b{width:65px;height:83px;border:2px solid #dc8f03;border-radius:60%;}
.xian{position:absolute;top:-20px;left:60px;width:2px;height:20px;background:#dc8f03;}
.deng-t{font-family:'华文行楷',Arial,sans-serif;font-size:3.2rem;color:#dc8f03;
font-weight:700;line-height:85px;text-align:center;}
.shui-a{position:relative;width:5px;height:20px;margin:-5px 0 0 59px;background:orange;
border-radius:0 0 5px 5px;animation:swing 4s infinite ease-in-out;}
.shui-b{position:absolute;top:14px;left:-2px;width:10px;height:10px;
background:#dc8f03;border-radius:50%;}
.shui-c{position:absolute;top:18px;left:-2px;width:10px;height:35px;
background:orange;border-radius:0 0 0 5px;}
.deng:before,.deng:after{content:" ";display:block;position:absolute;
border-radius:5px;border:1px solid #dc8f03;
background:linear-gradient(to right,#dc8f03,orange,#dc8f03,orange,#dc8f03);}
.deng:before{top:-7px;left:29px;height:12px;width:60px;z-index:999;}
.deng:after{bottom:-7px;left:10px;height:12px;width:60px;margin-left:20px;}
@media(max-width:768px){.deng-t{font-size:2.5rem}.deng-box{transform:scale(.5);top:-15px}
.deng-box1{left:-22%}.deng-box2{left:0}.deng-box3{right:50px}.deng-box4{right:-10px}}
@keyframes swing{0%{transform:rotate(-10deg)}50%{transform:rotate(10deg)}100%{transform:rotate(-10deg)}}
`;
var style = document.createElement('style');
style.textContent = css;
document.head.appendChild(style);
var wrap = document.createElement('div');
wrap.className = 'deng-container';
wrap.innerHTML = Array.from(text).map(function (t, i) {
return '<div class="deng-box deng-box' + (i % 4 + 1) + '">' +
'<div class="deng"><div class="xian"></div>' +
'<div class="deng-a"><div class="deng-b"><div class="deng-t">' + t + '</div></div></div>' +
'<div class="shui-a"><div class="shui-c"></div><div class="shui-b"></div></div>' +
'</div></div>';
}).join('');
document.body.appendChild(wrap);
})();
在 [Blogroot]\_config.butterfly.yml 的 inject.bottom 添加:1
2
3inject:
bottom:
- <script defer src="/js/denglong.js?text=新年快乐"></script> #新春灯笼
自定义标题
自定义标题
新建 [Blogroot]\source\js\title.js,写入:1
2
3
4
5
6
7
8
9
10
11
12
13var OriginTitile = document.title;
var titleTime;
document.addEventListener('visibilitychange', function () {
if (document.hidden) {
document.title = '👀跑哪里去了~';
clearTimeout(titleTime);
} else {
document.title = '🐖抓到你啦~';
titleTime = setTimeout(function () {
document.title = OriginTitile;
}, 2000);
}
});
在 [Blogroot]\_config.butterfly.yml 的 inject.bottom 添加:1
2
3inject:
bottom:
- <script async src="/js/title.js"></script> #网站标题改变
星空背景(仅暗色模式)
星空背景
新建 [Blogroot]\source\css\universe.css,写入:1
2
3
4
5
6
7
8
9
10
11
12
13
14#universe {
display: block;
position: fixed;
margin: 0;
padding: 0;
border: 0;
left: 0;
top: 0;
width: 100%;
height: 100%;
pointer-events: none;
z-index: -1;
}
[data-theme="light"] #universe { display: none; }
新建 [Blogroot]\source\js\universe.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(function () {
var canvas = document.getElementById('universe');
if (!canvas) return;
var ctx = canvas.getContext('2d');
var stars = [];
var raf = 0;
function resize() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
stars = Array.from({ length: 160 }, function () {
return {
x: Math.random() * canvas.width,
y: Math.random() * canvas.height,
r: Math.random() * 1.2 + 0.3,
s: Math.random() * 0.25 + 0.05
};
});
}
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = 'rgba(255,255,255,.8)';
stars.forEach(function (p) {
ctx.beginPath();
ctx.arc(p.x, p.y, p.r, 0, Math.PI * 2);
ctx.fill();
p.y -= p.s;
if (p.y < 0) p.y = canvas.height;
});
raf = requestAnimationFrame(draw);
}
function sync() {
var dark = document.documentElement.getAttribute('data-theme') === 'dark';
cancelAnimationFrame(raf);
if (dark && !document.hidden) draw();
else ctx.clearRect(0, 0, canvas.width, canvas.height);
}
resize();
window.addEventListener('resize', function () { resize(); sync(); });
document.addEventListener('visibilitychange', sync);
new MutationObserver(sync).observe(document.documentElement, {
attributes: true, attributeFilter: ['data-theme']
});
sync();
})();
在 [Blogroot]\_config.butterfly.yml 的 inject 添加:1
2
3
4
5
6inject:
head:
- <link rel="stylesheet" href="/css/universe.css" media="only x" onload="this.onload=null;this.media='all'"> #星空(dark模式)
bottom:
- <canvas id="universe"></canvas> #星空画板(dark模式)
- <script defer src="/js/universe.js"></script> #星空(dark模式)
猫猫挂件
猫猫挂件
新建 [Blogroot]\source\css\cat.css,写入:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16.neko {
position: fixed;
right: 20px;
bottom: 0;
width: 70px;
height: 100px;
background-repeat: no-repeat;
background-position: bottom;
background-size: contain;
z-index: 100;
cursor: pointer;
pointer-events: auto;
transition: transform .25s ease;
}
.neko:hover { transform: scale(1.08); }
@media screen and (max-width: 768px) { .neko { display: none ; } }
新建 [Blogroot]\source\js\cat.js,写入:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18(function ($) {
if (document.body.clientWidth <= 992) return;
if ($('.neko').length) return;
var neko = $('<div class="neko" id="neko1" title="喵~"></div>');
neko.css('background-image', 'url(/img/cat.png)');
$('body').append(neko);
$(window).on('scroll', function () {
neko.css('display', $(window).scrollTop() > 10 ? 'block' : 'none');
});
neko.on('mouseover', function () {
neko.css('background-image', 'url(/img/cat-hover.png)');
}).on('mouseout', function () {
neko.css('background-image', 'url(/img/cat.png)');
});
$(window).on('pjax:complete', function () {
if (!$('.neko').length) $('body').append(neko);
});
})(jQuery);
把猫猫图片放到 [Blogroot]\source\img\cat.png、[Blogroot]\source\img\cat-hover.png。
在 [Blogroot]\_config.butterfly.yml 的 inject 添加:1
2
3
4
5
6inject:
head:
- <link rel="stylesheet" href="/css/cat.css" media="only x" onload="this.onload=null;this.media='all'"> #猫猫
bottom:
- <script defer src="/js/jquery.min.js"></script> #猫猫/右键菜单依赖
- <script defer data-pjax src="/js/cat.js"></script> #猫猫jquery.min.js 用官方 jQuery 压缩版放入 [Blogroot]\source\js\。
自定义鼠标样式
自定义鼠标样式
新建 [Blogroot]\source\js\cursor.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(function () {
if (window.matchMedia('(pointer: coarse)').matches) return;
var dot = document.createElement('div');
dot.id = 'cursor';
document.body.appendChild(dot);
var x = 0, y = 0, raf = 0;
document.addEventListener('mousemove', function (e) {
x = e.clientX;
y = e.clientY;
if (raf) return;
raf = requestAnimationFrame(function () {
dot.style.left = x + 'px';
dot.style.top = y + 'px';
raf = 0;
});
});
document.addEventListener('mouseover', function (e) {
dot.classList.toggle('hover', !!e.target.closest('a, button, .site-page'));
});
document.addEventListener('mousedown', function () { dot.classList.add('active'); });
document.addEventListener('mouseup', function () { dot.classList.remove('active'); });
document.addEventListener('mouseleave', function () { dot.classList.add('hidden'); });
document.addEventListener('mouseenter', function () { dot.classList.remove('hidden'); });
})();
在 [Blogroot]\source\css\custom.css 末尾添加:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#cursor {
position: fixed;
top: 0;
left: 0;
margin: -8px 0 0 -8px;
width: 16px;
height: 16px;
background: rgb(57,197,187);
border-radius: 8px;
opacity: .25;
z-index: 10086;
pointer-events: none;
transition: .2s ease-in-out;
transition-property: background, opacity, transform;
}
#cursor.hidden { opacity: 0; }
#cursor.hover { opacity: .1; transform: scale(2.5); }
#cursor.active { opacity: .5; transform: scale(.5); }
@media (pointer: coarse) { #cursor { display: none; } }
在 [Blogroot]\_config.butterfly.yml 的 inject.bottom 添加:1
2
3inject:
bottom:
- <script defer src="/js/cursor.js"></script> #鼠标样式
右键菜单
右键菜单
新建 [Blogroot]\themes\butterfly\layout\includes\rightmenu.pug,写入:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#rightMenu.js-pjax
.rightMenu-group.rightMenu-small
a.rightMenu-item(href="javascript:window.history.back();")
i.fa.fa-arrow-left
a.rightMenu-item(href="javascript:window.history.forward();")
i.fa.fa-arrow-right
a.rightMenu-item(href="javascript:window.location.reload();")
i.fa.fa-refresh
a.rightMenu-item(href="javascript:window.scrollTo(0,0);")
i.fa.fa-arrow-up
.rightMenu-group.rightMenu-line.hide#menu-text
a.rightMenu-item(href="javascript:rmf.copySelect();")
i.fa.fa-copy
span='复制'
a.rightMenu-item(href="javascript:window.open('https://www.baidu.com/s?wd='+window.getSelection().toString());")
i.fa.fa-search
span='百度搜索'
a.rightMenu-item(href="javascript:rmf.copyWordsLink();")
i.fa.fa-link
span='复制本文地址'
新建 [Blogroot]\source\js\rightmenu.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
28var rmf = {};
var rightMenu = document.getElementById('rightMenu');
var mouseMode = false;
rmf.showRightMenu = function (show, y, x) {
if (!rightMenu) return;
rightMenu.style.display = show ? 'block' : 'none';
if (show) {
rightMenu.style.top = y + 'px';
rightMenu.style.left = x + 'px';
}
};
rmf.copySelect = function () {
var text = window.getSelection().toString();
if (text) navigator.clipboard.writeText(text);
};
rmf.copyWordsLink = function () {
navigator.clipboard.writeText(window.location.href);
};
function changeMouseMode() {
mouseMode = !mouseMode;
document.body.classList.toggle('mouse-mode', mouseMode);
}
document.addEventListener('contextmenu', function (e) {
e.preventDefault();
rmf.showRightMenu(true, e.clientY, e.clientX);
});
document.addEventListener('click', function () { rmf.showRightMenu(false); });
在 [Blogroot]\themes\butterfly\layout\includes\layout.pug 的 include ./rightside.pug 后面添加一行:1
2
3include ./rightside.pug
!=partial('includes/rightmenu',{}, {cache:true})
include ./additional-js.pug
在 [Blogroot]\source\css\custom.css 末尾添加:1
2
3
4
5
6
7
8
9
10
11
12#rightMenu {
display: none;
position: fixed;
z-index: 10087;
padding: 6px;
border-radius: 8px;
background: rgba(255,255,255,.95);
box-shadow: 0 4px 16px rgba(0,0,0,.18);
}
[data-theme="dark"] #rightMenu { background: rgba(30,35,45,.95); }
#rightMenu .rightMenu-item { display: block; padding: 6px 12px; cursor: pointer; }
.rightMenu-line.hide { display: none; }
在 [Blogroot]\_config.butterfly.yml 的 inject.bottom 添加:1
2
3inject:
bottom:
- <script async type="text/javascript" src="/js/rightmenu.js"></script> #右键菜单
禁止打开开发者工具
禁止 F12
在 [Blogroot]\themes\butterfly\layout\includes\layout.pug 的 html 标签下、head 之前插入:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19doctype html
html(lang=config.language data-theme=theme.display_mode class=htmlClassHideAside)
//-魔改禁F12开始
script.
((function() {var callbacks = [],timeLimit = 50,open = false;setInterval(loop, 500);return {addListener: function(fn) {callbacks.push(fn);},cancleListenr: function(fn) {callbacks = callbacks.filter(function(v) {return v !== fn;});}}
function loop() {var startTime = new Date();debugger;if (new Date() - startTime > timeLimit) {if (!open) {callbacks.forEach(function(fn) {fn.call(null);});}open = true;window.stop();alert('你真坏,请关闭控制台!');document.body.innerHTML = "";} else {open = false;}}})()).addListener(function() {window.location.reload();});
script.
function toDevtools(){
let num = 0;
let devtools = new Date();
devtools.toString = function() {
num++;
if (num > 1) { alert('你真坏,请关闭控制台!'); window.location.href = "about:blank"; blast(); }
}
console.log('', devtools);
}
toDevtools();
//-魔改禁F12结束
head
新建 [Blogroot]\source\js\f12.js,写入:1
2
3
4
5
6
7
8
9document.onkeydown = function (e) {
if (123 == e.keyCode ||
(e.ctrlKey && e.shiftKey && (74 === e.keyCode || 73 === e.keyCode || 67 === e.keyCode)) ||
(e.ctrlKey && 85 === e.keyCode)) {
e.keyCode = 0;
e.returnValue = false;
return false;
}
};
在 [Blogroot]\_config.butterfly.yml 的 inject.bottom 添加:1
2
3inject:
bottom:
- <script async src="/js/f12.js"></script> #禁f12
帧率检测
帧率检测
新建 [Blogroot]\source\js\fps.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(function () {
var box = document.getElementById('fps');
if (!box) return;
var frames = 0, last = performance.now(), lowStart = 0, shown = -1;
function tick(now) {
frames++;
if (now - last >= 1000) {
var fps = Math.round(frames * 1000 / (now - last));
if (fps !== shown) { box.textContent = 'FPS ' + fps; shown = fps; }
if (fps < 35) {
if (!lowStart) lowStart = now;
if (now - lowStart >= 2000) document.documentElement.classList.add('low-perf');
} else {
lowStart = 0;
document.documentElement.classList.remove('low-perf');
}
frames = 0;
last = now;
}
requestAnimationFrame(tick);
}
requestAnimationFrame(tick);
})();
在 [Blogroot]\source\css\custom.css 末尾添加:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#fps {
position: fixed;
right: 12px;
bottom: 12px;
z-index: 10088;
padding: 2px 8px;
border-radius: 10px;
font-size: 12px;
background: rgba(0,0,0,.35);
color: #fff;
pointer-events: none;
}
html.low-perf .parallax > use {
animation: none ;
-webkit-animation: none ;
}
在 [Blogroot]\_config.butterfly.yml 的 inject 添加:1
2
3
4
5inject:
head:
- <span id="fps" media="only x" onload="this.onload=null;this.media='all'"></span> # 帧率检测
bottom:
- <script async src="/js/fps.js"></script> # 帧率检测
控制台字符画
控制台字符画
新建 [Blogroot]\source\js\console.js,写入:1
2
3
4
5
6
7
8(function () {
var start = new Date('<建站日期>');
var days = Math.floor((Date.now() - start) / 86400000);
var style = 'color:#5ea6e5;font-size:14px;font-weight:bold;';
console.log('%c<站点名>', style);
console.log('%c小站已经苟活 ' + days + ' 天啦', style);
console.log('%c© <你的网名>', style);
})();
在 [Blogroot]\_config.butterfly.yml 的 inject.bottom 添加:1
2
3inject:
bottom:
- <script async src="/js/console.js"></script> #控制台字符画
顶部渐变加载条
顶部渐变加载条
把 pace.js 官方压缩版放到 [Blogroot]\source\js\pace.min.js。
新建 [Blogroot]\source\css\progress_bar.css,写入: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.pace {
pointer-events: none;
user-select: none;
z-index: 2000;
position: fixed;
margin: auto;
top: 10px;
left: 0;
right: 0;
height: 8px;
border-radius: 8px;
width: 4rem;
background: #eaecf2;
overflow: hidden;
}
.pace .pace-progress {
box-sizing: border-box;
transform: translate3d(0,0,0);
max-width: 200px;
position: absolute;
z-index: 2000;
display: block;
top: 0;
right: 100%;
height: 100%;
width: 100%;
background: linear-gradient(-45deg, #ee7752, #e73c7e, #23a6d5, #23d5ab);
animation: gradient 1.5s ease infinite;
background-size: 200%;
}
.pace.pace-inactive { opacity: 0; transition: .3s; top: -8px; }
@keyframes gradient {
0% { background-position: 0% 50%; }
50% { background-position: 100% 50%; }
100% { background-position: 0% 50%; }
}
把 [Blogroot]\themes\butterfly\layout\includes\loading\index.pug 改成:1
2
3
4
5
6
7
8if theme.preloader.enable
if theme.preloader.source === 1
include ./fullpage-loading.pug
else if theme.preloader.source === 2
include ./pace.pug
else
include ./fullpage-loading.pug
include ./pace.pug
在 [Blogroot]\_config.butterfly.yml 修改:1
2
3
4preloader:
enable: true
source: 3
pace_css_url: /css/progress_bar.css
在 [Blogroot]\_config.butterfly.yml 的 inject.bottom 添加:1
2
3inject:
bottom:
- <script async src="/js/pace.min.js"></script> #顶部渐变加载条
