Hexo AI 摘要提示添加

🤖 hexo-ai-summary-liushen · AI 智能摘要插件 →

umami-view.js 写法

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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
{
// 从配置文件中获取 umami 的配置
const website_id = CONFIG.web_analytics.umami.website_id;
// 拼接请求地址
const request_url = `${CONFIG.web_analytics.umami.api_server}/api/websites/${website_id}/stats`;

const start_time = new Date(CONFIG.web_analytics.umami.start_time).getTime();
const end_time = new Date().getTime();
const token = CONFIG.web_analytics.umami.token;

// 检查配置是否为空
if (!website_id) {
throw new Error("Umami website_id is empty");
}
if (!request_url) {
throw new Error("Umami request_url is empty");
}
if (!start_time) {
throw new Error("Umami start_time is empty");
}
if (!token) {
throw new Error("Umami token is empty");
}

// 构造请求参数
const params = new URLSearchParams({
startAt: start_time,
endAt: end_time,
});
// 构造请求头
const request_header = {
method: "GET",
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer " + token,
},
};

// 获取站点统计数据
async function siteStats() {
try {
const response = await fetch(`${request_url}?${params}`, request_header);
const data = await response.json();
const uniqueVisitors = data.visitors.value; // 获取独立访客数
const pageViews = data.pageviews.value; // 获取页面浏览量

let pvCtn = document.querySelector("#umami-site-pv-container");
if (pvCtn) {
let ele = document.querySelector("#umami-site-pv");
if (ele) {
ele.textContent = pageViews; // 设置页面浏览量
pvCtn.style.display = "inline"; // 将元素显示出来
}
}

let uvCtn = document.querySelector("#umami-site-uv-container");
if (uvCtn) {
let ele = document.querySelector("#umami-site-uv");
if (ele) {
ele.textContent = uniqueVisitors;
uvCtn.style.display = "inline";
}
}
} catch (error) {
console.error(error);
return "-1";
}
}

// 获取页面浏览量
async function pageStats(path) {
try {
const response = await fetch(`${request_url}?${params}&url=${path}`, request_header);
const data = await response.json();
const pageViews = data.pageviews.value;

let viewCtn = document.querySelector("#umami-page-views-container");
if (viewCtn) {
let ele = document.querySelector("#umami-page-views");
if (ele) {
ele.textContent = pageViews;
viewCtn.style.display = "inline";
}
}
} catch (error) {
console.error(error);
return "-1";
}
}

siteStats();

// 获取页面容器
let viewCtn = document.querySelector("#umami-page-views-container");
// 如果页面容器存在,则获取页面浏览量
if (viewCtn) {
let path = window.location.pathname;
let target = path
.replace(/(\/[^/]+\.html)\/$/, "$1") // 如果是 '/xxxx.html/' 格式的路径,则去掉最后那个 '/'
.replace(/\/index\.html$/, "/"); // 如果是 '/index.html' 格式,则合并成 '/'
pageStats(target);
}

}

ai-summary.css 写法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
{
.ai-summary-typing {
margin: 0 0 1.2em 0;
padding: 0.8em 1em;
border-left: 3px solid #4a9eff;
background: rgba(74, 158, 255, 0.06);
border-radius: 4px;
font-size: 0.95em;
line-height: 1.7;
min-height: 1.7em;
}
.ai-summary-cursor {
display: inline-block;
width: 2px;
height: 1em;
margin-left: 2px;
vertical-align: -0.15em;
background: #4a9eff;
animation: ai-cursor-blink 0.8s steps(1) infinite;
}
@keyframes ai-cursor-blink {
50% { opacity: 0; }
}
}

scripts目录加js

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
{
hexo.extend.filter.register('after_post_render', function (data) {
if (data.layout !== 'post' || !data.excerpt) return data;
if (data.content && data.content.indexOf('ai-summary-typing') !== -1) return data;

var text = data.excerpt.trim().replace(/"/g, '"');

var css = '<style>'
+ '.ai-summary-box{margin:0 0 1.5em 0;border:1px solid #d8e6ff;border-radius:8px;overflow:hidden;font-size:0.95em;background:#fff}'
+ '.ai-summary-box-header{display:flex;align-items:center;gap:6px;padding:0.5em 1em;background:linear-gradient(90deg,rgba(74,158,255,0.12),rgba(74,158,255,0.03));border-bottom:1px solid #d8e6ff;font-weight:600;color:#2b7de9}'
+ '.ai-summary-box-header svg{width:1.1em;height:1.1em;flex:none}'
+ '.ai-summary-typing{padding:0.8em 1em;line-height:1.7;min-height:1.7em;color:#444}'
+ '.ai-summary-cursor{display:inline-block;width:2px;height:1em;margin-left:2px;vertical-align:-0.15em;background:#4a9eff;animation:ai-cursor-blink 0.8s steps(1) infinite}'
+ '@keyframes ai-cursor-blink{50%{opacity:0}}'
+ '<\/style>';

var icon = '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M12 2a4 4 0 0 1 4 4c0 1.1-.45 2.1-1.17 2.83L12 12l-2.83-3.17A4 4 0 0 1 12 2z"/><circle cx="12" cy="6" r="1"/><path d="M5 10v4l7 4 7-4v-4"/><path d="M12 18v4"/></svg>';

var block = '<div class="ai-summary-box">'
+ '<div class="ai-summary-box-header">' + icon + '<span>AI 摘要</span></div>'
+ '<div class="ai-summary-typing" data-text="' + text + '"><span class="ai-summary-text"></span><span class="ai-summary-cursor"></span></div>'
+ '<\/div>';

var js = '<script>(function(){'
+ 'function init(){var els=document.querySelectorAll(".ai-summary-typing");'
+ 'for(var k=0;k<els.length;k++){(function(el){'
+ 'if(el.getAttribute("data-done"))return;el.setAttribute("data-done","1");'
+ 'var text=el.getAttribute("data-text")||"";'
+ 'var target=el.querySelector(".ai-summary-text");'
+ 'var i=0;var timer=setInterval(function(){'
+ 'target.textContent=text.slice(0,++i);'
+ 'if(i>=text.length){clearInterval(timer);'
+ 'setTimeout(function(){var c=el.querySelector(".ai-summary-cursor");if(c)c.style.display="none";},2000);}'
+ '},45);})(els[k]);}}'
+ 'if(document.readyState==="loading"){document.addEventListener("DOMContentLoaded",init);}else{init();}'
+ '})();<\/script>';

data.content = css + block + js + (data.content || '');
return data;
});
}

Hexo AI 摘要提示添加
http://example.com/2026/09/30/AI 摘要提示/
作者
阿木
发布于
2026年10月1日
许可协议