AI正在绞尽脑汁想思路ING···
BiのAI摘要
HunYuan-Lite

在网上查找博客美化过程时发现基本上都有“随便逛逛”的功能,于是自己也添加一个。
比较简单只有js代码,但是先要安装插件

前置条件

1
npm install hexo-generator-sitemap --save

在站点配置文件 _config.yaml中添加:

1
2
3
4
5
6
7
sitemap:
path:
- sitemap.xml
- sitemap.txt
rel: false
tags: true
categories: true

添加js

在主题目录下添加·\themes\butterfly\source\js\random.js

1
2
3
4
5
6
7
8
9
10
11
12
13
function randomPost() {
fetch('/sitemap.xml').then(res => res.text()).then(str => (new window.DOMParser()).parseFromString(str, "text/xml")).then(data => {
let ls = data.querySelectorAll('url loc');
let locationHref,locSplit;
do {
locationHref = ls[Math.floor(Math.random() * ls.length)].innerHTML
locSplit = locationHref.split('/')[3] || ''
} while (locSplit == '' || locSplit == 'tags');
//若所有文章都如 https://…….com/posts/2022/07/…… 格式,主域名后字符是 posts,则循环条件改为:
//while (locSplit !== 'posts');
location.href = locationHref
})
}

使用

使用,在菜单栏上添加

1
<a href="javascript:;" onclick="randomPost()" title="随机访问一篇文章">随机</a>

参考