Hexo+NexT 自定义样式博文加密
介绍一种自定义博文加密方式,不需要插件,极简模式,相对安全。
先看一下效果:

代码
1、在目录 /themes/next/layout/ 下的 _layout.swig 中,找到main标签添加自定义的 swig
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| <main id="main" class="main"> <div class="main-inner"> <div class="content-wrap"> <div id="content" class="content"> {% block content %}{% endblock %} </div> {% include '_third-party/duoshuo-hot-articles.swig' %} {% include '_partials/comments.swig' %} </div> {% if theme.sidebar.display !== 'remove' %} {% block sidebar %}{% endblock %} {% endif %} </div>
<!-- 此处为新加的代码 --> {% include 'password.swig' %}
</main>
|
2、在目录 /themes/next/layout/ 下创建 password.swig ,内容如下:
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
| <script src="https://cdn.staticfile.org/jquery/3.1.1/jquery.min.js"></script> <script> var div = $('.post-body'); var toc=$('.post-toc') function password() { if('{{ page.password }}'){ div.remove(); toc.remove(); $('.post-header').after( '<span class="description" value="请输入密码,然后按 Enter 键阅读" style="font-style: oblique;font-weight: bold;border: none;display: block;'+ 'width: 60%;margin: 0 auto;text-align: center;outline: none;margin-bottom: 40px;resize:none ">'+ '<i class="fa fa-heartbeat" id="myheartbeat"></i>'+ '请输入密码,然后按 Enter 键阅读' + '<i class="fa fa-heartbeat" id="myheartbeat"></i>'+ '</span>' + '<div class="qiang" style="height: 100px;width: 60%;margin:0 auto">' + '<input class="password" type="password" autocomplete="new-password" autofocus="autofocus" value="" style="border-radius: 5px;height: 30px;border: none;display: block;border-bottom: 1px solid #ccc;' + 'margin: 0 auto;outline: none;width:95%"/>' + '</div>') document.onclick = function (event) { var e = event || window.event; var elem = e.srcElement || e.target;
while (elem) { if (elem != document) { if (elem.className == "password") { return; } elem = elem.parentNode; } else { return; } } } $(document).keyup(function(event){ if(event.keyCode ==13&&$('.password').length>0){ if ($('.password').val() == '{{ page.password }}') { (div).appendTo($(".post-header")) toc.appendTo($(".sidebar-inner")) $(".description").remove(); $(".qiang").remove(); $(".password").remove();
$('img').lazyload({ placeholder: '/images/loading.gif', effect: 'fadeIn', threshold : 100, failure_limit : 20, skip_invisible : false }); $(".post-block").css({opacity:1}); $(".post-header").css({opacity:1}); $(".post-body").css({opacity:1}); $(".pagination").css({opacity:1}); }else { alert("对不起,密码输入错误。") } } }); } } password(); </script>
|
使用
新建一个 test.md ,内容如下
1 2 3 4 5 6 7 8
| title: 测试 date: 2019-03-30 21:18:02 password: aaa --- # aaaaaa 我就很反感大家老是那么说我, ## bbbbbb 除了有才,就只剩下那无可比拟的颜值。
|
上面的 password 后面的值自定义。
注意
如果自己的博客源码中的这篇文章上传到 github ,密码也就公诸于世了,可以在 push 到 github 的时候将这篇文章忽略。