-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
204 lines (171 loc) · 6.05 KB
/
content.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
const style = document.createElement('style');
style.textContent = `
#archive-content {
width: 50%;
margin: 0 auto;
}
#archive-content table {
border-collapse: collapse;
width: 100%;
margin-bottom: 1rem;
margin-top: 30px; // 添加这一行
}
#archive-content th,
#archive-content td {
padding: 8px;
text-align: left;
}
#archive-content th {
background-color: #f2f2f2;
font-weight: bold;
}
#archive-content tr:nth-child(even) {
background-color: #f2f2f2;
}
#archive-content tr:nth-child(odd) {
background-color: #fff;
}
#archive-content a {
color: inherit;
text-decoration: none;
}
#archive-content h2 {
text-align: center;
}
#archive-content a:hover {
color: blue;
cursor: pointer;
font-weight: bold;
}
`;
document.head.appendChild(style);
(function () {
const archiveTab = `
<li class="menu-item menu-item-archive">
<a class="menu-item-link" href="javascript:void(0)" id="archive-link">Archive</a>
</li>
`;
const navList = document.querySelector('.nav-list');
navList.insertAdjacentHTML('beforeend', archiveTab);
const archiveLink = document.getElementById('archive-link');
const loadMDFile = async () => {
const targetUrl = 'https://xargin-blog-crawler.pages.dev/xargin_blogs.md';
const response = await fetch(targetUrl);
if (response.ok) {
// console.log('MD file fetched successfully');
const mdText = await response.text();
return mdText;
} else {
console.log('Error fetching MD file:', response.status, response.statusText);
return '';
}
};
const convertMDToHTML = (mdText) => {
const converter = new showdown.Converter({
tables: true,
simplifiedAutoLink: true,
strikethrough: true,
tasklists: true,
});
const htmlContent = converter.makeHtml(mdText);
return htmlContent;
};
const createArchiveContent = (htmlContent) => {
const siteContent = document.querySelector('.site-content');
const archiveContainer = document.createElement('div');
archiveContainer.style.display = 'none';
archiveContainer.id = 'archive-content';
const title = '<h2 style="font-size: 30px; font-weight: 800; letter-spacing: -.01em; margin-bottom: 20px;">历史文章</h2>';
const darkModeStyle = `
<style>
.dark-mode #archive-content {
color: white;
}
// .dark-mode #archive-content tr:nth-child(even) {
// background-color: #2b2b2b;
// }
// .dark-mode #archive-content tr:nth-child(odd) {
// background-color: #383838;
// }
.dark-mode #archive-content tr:nth-child(even) {
background-color: #20232a;
}
.dark-mode #archive-content tr:nth-child(odd) {
background-color: #282c35;
}
.dark-mode #archive-content th {
background-color: #20232a;
}
#archive-content tr:hover {
background-color: #BEBEBE;
}
.dark-mode #archive-content tr:hover {
background-color: #666;
}
</style>
`;
archiveContainer.innerHTML = `${darkModeStyle}${title}${htmlContent}`;
siteContent.appendChild(archiveContainer);
return archiveContainer;
};
const updatePageContent = (archiveContent) => {
const siteContentElement = document.querySelector('.site-content');
const contentAreaElement = siteContentElement.querySelector('.content-area');
if (contentAreaElement) {
contentAreaElement.appendChild(archiveContent);
} else {
console.error('Could not find the content-area element to update');
}
};
const showArchive = () => {
const mainContent = document.querySelector('.site-main');
mainContent.style.display = 'none';
const archiveContent = document.querySelector('#archive-content');
archiveContent.style.display = 'block';
};
const hideArchive = () => {
const mainContent = document.querySelector('.site-main');
mainContent.style.display = 'block';
const archiveContent = document.querySelector('#archive-content');
archiveContent.style.display = 'none';
};
let isArchiveVisible = false;
let isContentLoaded = false;
archiveLink.addEventListener('click', async () => {
if (!isContentLoaded) {
const mdText = await loadMDFile();
const htmlContent = convertMDToHTML(mdText);
// console.log('HTML content:', htmlContent);
const archiveContent = createArchiveContent(htmlContent);
updatePageContent(archiveContent);
isContentLoaded = true;
}
if (!isArchiveVisible) {
showArchive();
isArchiveVisible = true;
} else {
hideArchive();
isArchiveVisible = false;
}
});
document.addEventListener('DOMContentLoaded', async () => {
console.log('Extension loaded');
const mdText = await loadMDFile();
if (mdText) {
// console.log('MD text:', mdText);
const htmlContent = convertMarkdownToHTML(mdText);
// console.log('HTML content:', htmlContent);
injectArchiveSection(htmlContent);
} else {
console.log('Failed to load MD file');
}
});
setInterval(async () => {
const mdText = await loadMDFile();
const htmlContent = convertMDToHTML(mdText);
const archiveContent = document.querySelector('#archive-content');
if (archiveContent) {
archiveContent.innerHTML = htmlContent;
}
}, 3600000);
})();