-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-editor.html
More file actions
373 lines (330 loc) · 13.7 KB
/
test-editor.html
File metadata and controls
373 lines (330 loc) · 13.7 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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>编辑器测试页面</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Microsoft YaHei', sans-serif;
max-width: 800px;
margin: 50px auto;
padding: 20px;
background: #f5f5f5;
}
.container {
background: white;
padding: 30px;
border-radius: 8px;
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}
h1 {
color: #1e40af;
margin-bottom: 20px;
}
.info-box {
background: #f0f9ff;
border-left: 4px solid #1e40af;
padding: 15px;
margin: 20px 0;
border-radius: 4px;
}
.test-section {
margin: 30px 0;
padding: 20px;
background: #f9fafb;
border-radius: 4px;
}
button {
background: #1e40af;
color: white;
border: none;
padding: 10px 20px;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
margin: 10px 5px 10px 0;
}
button:hover {
background: #0c4a6e;
}
input[type="password"] {
width: 100%;
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
font-family: monospace;
box-sizing: border-box;
}
.output {
background: #1e1e1e;
color: #00ff00;
padding: 15px;
border-radius: 4px;
font-family: 'Courier New', monospace;
font-size: 12px;
max-height: 400px;
overflow-y: auto;
margin-top: 10px;
}
.success {
color: #00ff00;
}
.error {
color: #ff6b6b;
}
.warning {
color: #ffd700;
}
#tokenStatus {
margin-top: 10px;
padding: 10px;
background: #fef3c7;
border-radius: 4px;
}
</style>
</head>
<body>
<div class="container">
<h1>🧪 编辑器测试页面</h1>
<div class="info-box">
<p><strong>说明:</strong>这是一个用于测试 GitHub Token 和编辑器功能的页面。</p>
<p>首先输入你的 GitHub Personal Access Token,然后点击下面的按钮来测试各个功能。所有日志会显示在下方的输出区域。</p>
</div>
<div class="test-section">
<h2>⚙️ 设置 Token</h2>
<p>输入你的 GitHub Personal Access Token(需要 repo 权限)</p>
<input type="password" id="tokenInput" placeholder="输入 Token (ghp_...)" />
<button onclick="setToken()" style="margin-top: 10px;">💾 保存 Token</button>
<button onclick="clearToken()" style="background: #dc2626;">🗑️ 清除 Token</button>
<div id="tokenStatus"></div>
</div>
<div class="test-section">
<h2>测试 1: 验证 Token</h2>
<p>检查 Token 是否有效</p>
<button onclick="testVerifyToken()">验证 Token</button>
<div id="output1" class="output"></div>
</div>
<div class="test-section">
<h2>测试 2: 获取文件列表</h2>
<p>列出仓库中的所有 HTML 文件</p>
<button onclick="testListFiles()">获取文件列表</button>
<div id="output2" class="output"></div>
</div>
<div class="test-section">
<h2>测试 3: 获取单个文件</h2>
<p>获取 ai-agent-rag.html 文件的内容和 SHA</p>
<button onclick="testGetFile()">获取文件</button>
<div id="output3" class="output"></div>
</div>
<div class="test-section">
<h2>测试 4: 提取 .ai-section</h2>
<p>从文件中提取 .ai-section 内容</p>
<button onclick="testExtractSection()">提取内容</button>
<div id="output4" class="output"></div>
</div>
</div>
<script>
// Token 应该从 localStorage 获取,或者在测试时手动输入
let TOKEN = localStorage.getItem('github_editor_token') || '';
const GITHUB_API_BASE = 'https://api.github.com/repos/BobWang21/learning/contents';
function updateTokenStatus() {
const statusDiv = document.getElementById('tokenStatus');
if (TOKEN) {
statusDiv.innerHTML = `✓ Token 已设置: ${TOKEN.substring(0, 10)}...${TOKEN.substring(TOKEN.length - 5)}`;
statusDiv.style.background = '#d1fae5';
} else {
statusDiv.innerHTML = '⚠️ 请先输入 Token';
statusDiv.style.background = '#fef3c7';
}
}
function setToken() {
const input = document.getElementById('tokenInput');
const token = input.value.trim();
if (!token) {
alert('请输入 Token');
return;
}
TOKEN = token;
localStorage.setItem('github_editor_token', token);
input.value = '';
updateTokenStatus();
}
function clearToken() {
TOKEN = '';
localStorage.removeItem('github_editor_token');
document.getElementById('tokenInput').value = '';
updateTokenStatus();
}
function log(outputId, message, type = 'normal') {
const output = document.getElementById(outputId);
const timestamp = new Date().toLocaleTimeString();
let className = '';
if (type === 'success') className = 'success';
else if (type === 'error') className = 'error';
else if (type === 'warning') className = 'warning';
const line = `<div class="${className}">[${timestamp}] ${message}</div>`;
output.innerHTML += line;
output.scrollTop = output.scrollHeight;
}
async function testVerifyToken() {
const outputId = 'output1';
document.getElementById(outputId).innerHTML = '';
if (!TOKEN) {
log(outputId, '❌ 请先设置 Token', 'error');
return;
}
log(outputId, '正在验证 Token...');
try {
const response = await fetch('https://api.github.com/user', {
headers: {
'Authorization': `token ${TOKEN}`,
'Accept': 'application/json',
'User-Agent': 'Mozilla/5.0'
}
});
log(outputId, `响应状态: ${response.status} ${response.statusText}`);
if (!response.ok) {
log(outputId, `❌ Token 无效: ${response.statusText}`, 'error');
return;
}
const data = await response.json();
log(outputId, `✓ Token 有效`, 'success');
log(outputId, `用户: ${data.login}`, 'success');
log(outputId, `公开仓库数: ${data.public_repos}`, 'success');
} catch (error) {
log(outputId, `❌ 错误: ${error.message}`, 'error');
}
}
async function testListFiles() {
const outputId = 'output2';
document.getElementById(outputId).innerHTML = '';
if (!TOKEN) {
log(outputId, '❌ 请先设置 Token', 'error');
return;
}
log(outputId, '正在获取文件列表...');
try {
const response = await fetch(GITHUB_API_BASE, {
headers: {
'Authorization': `token ${TOKEN}`,
'Accept': 'application/json',
'User-Agent': 'Mozilla/5.0'
}
});
if (!response.ok) {
log(outputId, `❌ 请求失败: ${response.status}`, 'error');
return;
}
const data = await response.json();
const htmlFiles = data.filter(f => f.name.endsWith('.html'));
log(outputId, `✓ 获取成功,共 ${htmlFiles.length} 个 HTML 文件`, 'success');
htmlFiles.slice(0, 10).forEach(f => {
log(outputId, ` - ${f.name} (${f.size} 字节)`);
});
if (htmlFiles.length > 10) {
log(outputId, ` ... 还有 ${htmlFiles.length - 10} 个文件`);
}
} catch (error) {
log(outputId, `❌ 错误: ${error.message}`, 'error');
}
}
async function testGetFile() {
const outputId = 'output3';
document.getElementById(outputId).innerHTML = '';
if (!TOKEN) {
log(outputId, '❌ 请先设置 Token', 'error');
return;
}
log(outputId, '正在获取 ai-agent-rag.html...');
// 添加随机参数以绕过缓存
const url = `${GITHUB_API_BASE}/ai-agent-rag.html?t=${Date.now()}`;
log(outputId, `URL: ${url}`);
log(outputId, `Token: ${TOKEN.substring(0, 10)}...`);
try {
const response = await fetch(url, {
method: 'GET',
mode: 'cors',
cache: 'no-store',
headers: {
'Authorization': `token ${TOKEN}`,
'Accept': 'application/json',
'User-Agent': 'Mozilla/5.0',
'X-GitHub-Api-Version': '2022-11-28'
}
});
log(outputId, `响应状态: ${response.status} ${response.statusText}`);
log(outputId, `Content-Type: ${response.headers.get('content-type')}`);
if (!response.ok) {
const errorText = await response.text();
log(outputId, `❌ 请求失败: ${response.status}`, 'error');
log(outputId, `错误响应 (前 500 字符): ${errorText.substring(0, 500)}`, 'error');
return;
}
const responseText = await response.text();
log(outputId, `响应文本长度: ${responseText.length}`);
log(outputId, `响应文本前 200 字符: ${responseText.substring(0, 200)}`);
try {
const data = JSON.parse(responseText);
log(outputId, `✓ 文件获取成功`, 'success');
log(outputId, `文件名: ${data.name}`, 'success');
log(outputId, `大小: ${data.size} 字节`, 'success');
log(outputId, `SHA: ${data.sha}`, 'success');
log(outputId, `类型: ${data.type}`, 'success');
} catch (parseError) {
log(outputId, `❌ JSON 解析失败: ${parseError.message}`, 'error');
// 尝试判断响应内容
if (responseText.startsWith('<!DOCTYPE')) {
log(outputId, `响应是 HTML,可能是 GitHub 返回了错误页面`, 'error');
log(outputId, `这通常表示 Token 无效或 API 限制`, 'warning');
} else if (responseText.startsWith('{')) {
log(outputId, `响应看起来像 JSON,但解析失败`, 'error');
}
}
} catch (error) {
log(outputId, `❌ 网络错误: ${error.message}`, 'error');
}
}
async function testExtractSection() {
const outputId = 'output4';
document.getElementById(outputId).innerHTML = '';
if (!TOKEN) {
log(outputId, '❌ 请先设置 Token', 'error');
return;
}
log(outputId, '正在获取文件内容...');
try {
const response = await fetch(`${GITHUB_API_BASE}/ai-agent-rag.html`, {
headers: {
'Authorization': `token ${TOKEN}`,
'Accept': 'application/vnd.github.v3.raw',
'User-Agent': 'Mozilla/5.0'
}
});
if (!response.ok) {
log(outputId, `❌ 请求失败: ${response.status}`, 'error');
return;
}
const content = await response.text();
log(outputId, `✓ 文件内容获取成功,长度: ${content.length}`, 'success');
// 提取 .ai-section
const regex = /<section[^>]*class="[^"]*\bai-section\b[^"]*"[^>]*>([\s\S]*?)<\/section>/;
const match = content.match(regex);
if (match) {
log(outputId, `✓ 成功提取 .ai-section`, 'success');
log(outputId, `内容长度: ${match[1].length} 字符`, 'success');
log(outputId, `前 200 字符: ${match[1].substring(0, 200)}...`, 'success');
} else {
log(outputId, `❌ 未找到 .ai-section`, 'error');
}
} catch (error) {
log(outputId, `❌ 错误: ${error.message}`, 'error');
}
}
// 页面加载时初始化
window.addEventListener('load', () => {
updateTokenStatus();
});
</script>
</body>
</html>