前言

目前已有一个简便的解决方式,网址为:https://github.com/bojieyang/indexnow-action

本文仅适用于通过hexo创建的Node.js项目,是在我知道前面所说方法前写的,过程与前面的方法相比多了些步骤,且有所限制,仅供参考。

步骤

生成API Key

https://www.bing.com/indexnow/getstarted生成一个API key,并下载key文件放到相应位置。

写一个js文件,发起post请求提交URL

在项目根目录下,创建一个indexnow.js文件,并在package.json文件scripts对象中,添加下面内容:

1
"indexnow": "node indexnow.js"

indexnow.js文件中,创建一个hexo对象,通过hexo.locals.get(“posts”).data获得所有博客信息,再用正则表达式,选出要提交的URL。最后发起HTTP请求,提交URL。

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

import Hexo from "hexo";
var hexo = new Hexo(process.cwd(), {})
await hexo.init()
hexo.load().then(async () => {
// 改成需要的网址
const website = "https://www.hookind.space/"

// 使用正则表示式,选出要提交的URL。这里是根据_config.yml文件里的permalink属性设置。
const exp = /posts\/[0-9]*.html/
const urlList = hexo.route.list().filter(item => item.match(exp)).map(item => website + item).reverse()

console.log(urlList)

const headers = {
'Content-Type': 'application/json; charset=utf-8'
}

// 根据需要修改host、key、keyLocaiton
const body = {
"host": "www.hookind.space",
"key": "API key",
"keyLocation": "API Key文件地址",
"urlList": urlList
}

const res = await fetch ("https://www.bing.com/indexnow", {
method: "POST",
headers: headers,
body: JSON.stringify(body)
})

// 打印相应结果,可以在github aciton的日志查看
console.log(res)
})

设置github工作流文件

在.github/workflows目录下,创建文件indexnow.yml,内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
name: indexnow

on:
workflow_dispatch:
schedule:
- cron: 10 16 * * *

jobs:
run:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm i
- run: npm run indexnow

参考

1、https://hexo.io/zh-cn/api/

2、https://hexo.io/zh-cn/api/router

3、https://www.bing.com/indexnow/getstarted