This commit is contained in:
2025-08-13 11:07:50 +08:00
parent b8692eebea
commit 81c536e65d
9 changed files with 37 additions and 63 deletions

View File

@@ -9,15 +9,17 @@ summary: >-
以下是你提供的步骤的详细解释:
date: 2025-08-12 15:15:32
---
{% series webcustom %}
在网上查找博客美化过程时发现基本上都有“随便逛逛”的功能,于是自己也添加一个。
比较简单只有js代码但是先要安装插件
# 前置条件
```bash
npm install hexo-generator-sitemap --save
```
在站点配置文件`_config.yaml`中添加:
在站点配置文件 `_config.yaml`中添加:
```yml
sitemap:
path:
@@ -27,8 +29,11 @@ sitemap:
tags: true
categories: true
```
# 添加js
在主题目录下添加·`\themes\butterfly\source\js\random.js`
```js
function randomPost() {
fetch('/sitemap.xml').then(res => res.text()).then(str => (new window.DOMParser()).parseFromString(str, "text/xml")).then(data => {
@@ -44,10 +49,15 @@ function randomPost() {
})
}
```
# 使用
使用,在菜单栏上添加
```html
<a href="javascript:;" onclick="randomPost()" title="随机访问一篇文章">随机</a>
```
# 参考
{% link 木木木木木的博客,木木木木木,https://immmmm.com/randompost-by-sitemap/ %}
{% link 木木木木木的博客,木木木木木,https://immmmm.com/randompost-by-sitemap/ %}