Hexo NexT主题侧边栏添加一言

萌新向教程 一起为侧边栏添加点击刷新+Copy+爱心特效的一言模块吧( ̄▽ ̄)

0%

大致流程

添加侧边栏 -> 添加CSS -> 激活配置 -> 同步完工

演示效果

添加一言插件

编辑themes/next/layout/_custom/sidebar.swig文件, 若无则创建.

<!-- hitokoto -->

<strong>
<div id="hitokoto_wrap">
<div id="hitokoto_text">&nbsp;&nbsp;&nbsp;&nbsp;抬头望天,低头走路。</div>
<div id="hitokoto_from">——无名</div>
</div>
</strong>

<script>
let hitokoto_wrap = document.getElementById("hitokoto_wrap");
let hitokoto_text = document.getElementById('hitokoto_text')
let hitokoto_from = document.getElementById('hitokoto_from');
async function loadHitokoto(){
// Fetch API
// 调用一言接口 根据需求配置参数 默认即可
// fetch('https://v1.hitokoto.cn?min_length=12')
fetch('https://international.v1.hitokoto.cn?min_length=12')
.then(response => response.json())
.then(data => {
hitokoto_text.innerText = '\xa0\xa0\xa0\xa0\xa0\xa0\xa0' + data.hitokoto;
hitokoto_from.innerText = "——" + data.from + '\xa0';
})
.catch(console.error)
}
loadHitokoto();

hitokoto_wrap.addEventListener("click", async function(e){
loadHitokoto();

// 复制一言内容到剪贴板
// 去除字符串前后空格
let content = (hitokoto_text.innerText + hitokoto_from.innerText).trim()
// Clipboard API
navigator.clipboard.writeText(content)
.then(console.log(content))
.catch(console.error)
});
</script>

添加点击爱心特效

<script>
// 添加鼠标点击爱心特效
!function(e, t, a) {
function r() {
for (var e = 0; e < s.length; e++) s[e].alpha <= 0 ? (t.body.removeChild(s[e].el), s.splice(e, 1)) : (s[e].y--, s[e].scale += .004, s[e].alpha -= .013, s[e].el.style.cssText = "left:" + s[e].x + "px;top:" + s[e].y + "px;opacity:" + s[e].alpha + ";transform:scale(" + s[e].scale + "," + s[e].scale + ") rotate(45deg);background:" + s[e].color + ";z-index:99999");
requestAnimationFrame(r)
}
function n() {
var t = "function" == typeof e.onclick && e.onclick;
// 绑定点击事件到一言窗口
hitokoto_wrap.addEventListener("click", function(e) {
t && t(),
o(e)
});
}
function c() {
return "rgb(" + ~~ (255 * Math.random()) + "," + ~~ (255 * Math.random()) + "," + ~~ (255 * Math.random()) + ")"
}
function o(e) {
var a = t.createElement("div");
a.className = "heart",
s.push({
el: a,
x: e.clientX - 5,
y: e.clientY - 5,
scale: 1,
alpha: 1,
color: c()
}),
t.body.appendChild(a)
}
function i(e) {
var a = t.createElement("style");
a.type = "text/css";
try {
a.appendChild(t.createTextNode(e))
} catch(t) {
a.styleSheet.cssText = e
}
t.getElementsByTagName("head")[0].appendChild(a)
}

var s = [];
e.requestAnimationFrame = e.requestAnimationFrame || e.webkitRequestAnimationFrame || e.mozRequestAnimationFrame || e.oRequestAnimationFrame || e.msRequestAnimationFrame ||
function(e) {
setTimeout(e, 1e3 / 60)
},
i(".heart{width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: fixed;}.heart:after{top: -5px;}.heart:before{left: -5px;}"),
n(),
r()
} (window, document);
</script>

激活一言窗口

编辑themes/next/_config.yml文件

找到custom_file_path修改sidebarthemes/next/layout/_custom/sidebar.swig, 才会加载定制的侧边栏内容.

添加一言CSS

编辑themes/next/source/css/_custom/custom.styl文件

//hitokoto

#hitokoto_wrap {
margin-top: 15px;
cursor: pointer;
display: flex;
flex-direction: column;
flex-wrap: nowrap;
}

#hitokoto_text {
text-align: left;
font-size: 0.8125em;
}

#hitokoto_from {
text-align: right;
font-size: 0.7em;
color: #999;
}

导入一言CSS

编辑themes/next/source/css/main.styl文件

在末尾添加@import "_custom/custom";, 这样css才会生效.


测试效果 -> 同步配置 -> 大功告成🎉

参考文档

语句接口 | 一言开发者中心 (hitokoto.cn)

为您的Hexo博客添加Hitokoto一言功能 | Bill Yang's Blog

JS实现鼠标点击爱心&绘制多边形&每日一言功能 - 掘金 (juejin.cn)

------------ 已触及底线了 感谢您的阅读 ------------
  • 本文作者: OWQ
  • 本文链接: https://www.owq.world/5313cb7/
  • 版权声明: 本站所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处( ̄︶ ̄)↗