-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmetaKeywords.js
More file actions
32 lines (28 loc) · 1.16 KB
/
Copy pathmetaKeywords.js
File metadata and controls
32 lines (28 loc) · 1.16 KB
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
document.addEventListener('DOMContentLoaded', () => {
const params = new URLSearchParams(window.location.search);
const page = params.get('page');
fetch('posts.json')
.then(response => response.json())
.then(posts => {
let keywords = new Set();
if (page) {
// 找到對應的 post
const post = posts.find(p => p.link.includes(page));
if (post && post.keywords) {
post.keywords.forEach(keyword => keywords.add(keyword));
}
} else {
// 如果沒有指定 page,使用所有關鍵字
posts.forEach(post => {
if (post.keywords) {
post.keywords.forEach(keyword => keywords.add(keyword));
}
});
}
const metaKeywords = document.createElement('meta');
metaKeywords.name = 'keywords';
metaKeywords.content = Array.from(keywords).join(', ');
document.head.appendChild(metaKeywords);
})
.catch(error => console.error('Error loading posts.json:', error));
});