Repository navigation
Expand file tree
/
Copy pathforum.html
More file actions
399 lines (375 loc) · 15.4 KB
/
Copy pathforum.html
File metadata and controls
399 lines (375 loc) · 15.4 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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>论坛 - 留言区</title>
<!-- 引入Markdown渲染依赖marked.js -->
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
<style>
* {box-sizing: border-box; margin: 0; padding: 0;}
body {
background: #fff;
color: #000;
font-family: system-ui, Arial;
padding: 30px;
max-width: 900px;
margin: 0 auto;
}
.nav-bar {
font-size: 24px;
margin-bottom: 30px;
padding-bottom: 10px;
border-bottom: 1px solid #eee;
}
.nav-bar a {
color: #007bff;
text-decoration: none;
margin-right: 20px;
}
.wrap {
margin-bottom: 30px;
}
.item {
margin-bottom: 15px;
}
label {
display: block;
margin-bottom: 5px;
}
input, textarea {
width: 100%;
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
}
textarea {
/* 提示用户可以用Markdown语法 */
placeholder: "输入留言内容,支持Markdown语法:# 标题、**加粗**、- 列表、`代码`、[链接](地址)、*斜体*,以及颜色设置:==背景色==、^文字颜色^"
}
#sendBtn {
padding: 10px 30px;
background: #007bff;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
}
.msg-card {
margin-top: 15px;
padding: 12px;
border: 1px solid #ddd;
border-radius: 6px;
}
.msg-head {
display: flex;
justify-content: space-between;
margin-bottom: 8px;
}
.gold {
color: #ffd700;
font-weight: bold;
}
.tip {
margin-top: 20px;
color: #666;
}
/* 新增Markdown渲染的默认样式,让渲染后的内容更美观 */
.markdown-content h1, .markdown-content h2, .markdown-content h3 {
margin: 12px 0 8px 0;
line-height: 1.5;
border-bottom: 1px solid #eee;
padding-bottom: 4px;
}
.markdown-content p {
line-height: 1.6;
margin: 8px 0;
}
.markdown-content ul, .markdown-content ol {
padding-left: 20px;
margin: 8px 0;
}
.markdown-content blockquote {
border-left: 4px solid #007bff;
padding: 8px 12px;
background: #f8f9fa;
color: #666;
margin: 10px 0;
}
.markdown-content pre {
background: #f6f8fa;
padding: 10px;
border-radius: 4px;
overflow-x: auto;
margin: 10px 0;
}
.markdown-content code {
background: #f1f3f4;
padding: 2px 4px;
border-radius: 3px;
font-family: monospace;
}
.markdown-content pre code {
background: transparent;
padding: 0;
}
.markdown-content a {
color: #007bff;
text-decoration: none;
}
.markdown-content a:hover {
text-decoration: underline;
}
.markdown-content img {
max-width: 100%;
border-radius: 4px;
margin: 10px 0;
}
/* 颜色选择工具栏 */
.color-toolbar {
display: flex;
align-items: center;
margin-bottom: 10px;
gap: 8px;
}
.color-btn {
padding: 4px 8px;
border: 1px solid #ccc;
border-radius: 3px;
cursor: pointer;
font-size: 12px;
}
.color-btn:hover {
opacity: 0.8;
}
</style>
</head>
<body>
<div class="nav-bar">
<a href="forum.html">论坛</a> | <a href="register.html">注册</a>
</div>
<div class="wrap">
<h3>发布留言</h3>
<!-- 新增颜色选择工具栏 -->
<div class="color-toolbar">
<span>文字颜色:</span>
<button class="color-btn" onclick="setInlineStyle('color', 'red')">红色</button>
<button class="color-btn" onclick="setInlineStyle('color', 'green')">绿色</button>
<button class="color-btn" onclick="setInlineStyle('color', 'blue')">蓝色</button>
<button class="color-btn" onclick="setInlineStyle('color', 'yellow')">黄色</button>
<button class="color-btn" onclick="setInlineStyle('color', 'purple')">紫色</button>
<button class="color-btn" onclick="setInlineStyle('color', 'black')">黑色</button>
<span style="margin-left: 15px;">背景颜色:</span>
<button class="color-btn" onclick="setInlineStyle('background-color', 'yellow')">黄色</button>
<button class="color-btn" onclick="setInlineStyle('background-color', 'lightblue')">浅蓝色</button>
<button class="color-btn" onclick="setInlineStyle('background-color', 'lightgreen')">浅绿色</button>
<button class="color-btn" onclick="setInlineStyle('background-color', 'white')">白色</button>
</div>
<div class="item">
<label>用户名</label>
<input type="text" id="uInput" placeholder="输入注册的用户名">
</div>
<div class="item">
<label>密码</label>
<input type="password" id="pInput" placeholder="输入账号密码">
</div>
<div class="item">
<label>留言</label>
<textarea id="mInput" rows="4" placeholder="输入留言内容,支持Markdown语法:# 标题、**加粗**、- 列表、`代码`、[链接](地址)、*斜体*,以及颜色设置:==背景色==、^文字颜色^"></textarea>
</div>
<!-- 增加防误触发防护:按钮初始禁用,页面加载完成后才启用 -->
<button id="sendBtn" disabled="disabled">发送留言</button>
</div>
<div class="tip" id="statusTip">页面初始化中...</div>
<div id="listBox"></div>
kxyjs提供支持!(v1.3.5 plus Markdown版)
<script src="https://cdn.jsdelivr.net/npm/@supabase/supabase-js@2"></script>
<script>
// 全局就绪开关,防止自动误触发
let pageReady = false;
const SUPABASE_URL = "https://aonoebeajppbziyzucsm.supabase.co";
const SUPABASE_KEY = "sb_publishable_AKUMJeqzjC-LPEng6iWR_A_hMg2GT_J";
const sb = window.supabase.createClient(SUPABASE_URL, SUPABASE_KEY);
let myIp = "";
// 超简单加密
function easyEnc(pass) {
return window.btoa(encodeURIComponent(pass));
}
// 核心IP获取逻辑:缓存+10次重试+local标识持久化
async function getMyIpSmart() {
const cachedIp = localStorage.getItem("cached_forum_ip");
const cachedLocalId = localStorage.getItem("forum_fallback_local_id");
const failedCount = Number(localStorage.getItem("ip_fetch_failed_count")) || 0;
if (cachedLocalId && failedCount >= 10) {
myIp = cachedLocalId;
return;
}
const ipApis = [
"https://api.ipify.org?format=json",
"https://api.my-ip.io/v2/ip.json",
"https://ipapi.co/ip/",
"https://api-ipv4.ip.sb/jsonip",
"https://l2.io/ip.json",
"https://ip.seeip.org/jsonip",
"https://ipv4.icanhazip.com/",
"https://ip.42.pl/raw",
"https://v4.ident.me/.json",
"https://ipaddr.site/json"
];
alert(`我们在尝试获取ip……(最多20s!)`);
for(let i = 0; i < 10; i++) {
try {
let res = await fetch(ipApis[i], {signal: AbortSignal.timeout(2000)});
if(!res.ok) continue;
let data = await res.json();
if(data.ip) {
myIp = data.ip;
localStorage.setItem("cached_forum_ip", myIp);
localStorage.setItem("ip_fetch_failed_count", "0");
return;
}
} catch(e) {
continue;
}
}
const newFailedCount = failedCount + 1;
localStorage.setItem("ip_fetch_failed_count", newFailedCount.toString());
if(newFailedCount >= 10 && !cachedLocalId) {
const newLocalId = "local_" + Math.random().toString(36).substring(2, 20);
localStorage.setItem("forum_fallback_local_id", newLocalId);
myIp = newLocalId;
return;
} else if (cachedLocalId) {
myIp = cachedLocalId;
return;
} else {
myIp = "temp_local_" + new Date().getTime();
}
}
// 发布留言按钮逻辑,增加真实事件校验防误触发
window.userClickPost = async function(e) {
// 双重校验:必须页面就绪 + 真实用户手动点击
if(!pageReady || !e.isTrusted) {
console.warn("拦截了非手动触发的发布事件");
return;
}
let u = document.getElementById("uInput").value.trim();
let p = document.getElementById("pInput").value.trim();
let m = document.getElementById("mInput").value.trim();
if(!u || !p || !m) {
alert("❌ 请填写完整用户名、密码和留言");
return;
}
let encPwd = easyEnc(p);
let userRes = await sb.from("lt_users").select("username").eq("username", u).eq("password", encPwd);
if(userRes.error) {
alert("❌ 账号校验出错:" + userRes.error.message);
return;
}
if(!userRes.data || userRes.data.length === 0) {
alert("❌ 账号或密码不存在");
return;
}
let lastMsgRes = await sb.from("lt_message").select("time").eq("username", u).order("time", {ascending: false}).limit(1);
if(lastMsgRes.data && lastMsgRes.data.length > 0) {
let lastT = new Date(lastMsgRes.data[0].time);
let nowT = new Date();
let diff = (nowT - lastT) / 1000;
if(diff < 60) {
alert(`⏱️ 冷却中,还需等${Math.ceil(60 - diff)}秒`);
return;
}
}
let insertRes = await sb.from("lt_message").insert({
ip: myIp,
username: u,
time: new Date().toISOString(),
// 存入原始Markdown文本,不处理,后续渲染时再解析
message: m
});
if(insertRes.error) {
alert("❌ 写入留言失败:" + insertRes.error.message);
return;
}
alert("🎉 发布完全成功!");
document.getElementById("mInput").value = "";
loadAllMsg();
}
// 加载留言列表,渲染Markdown内容
async function loadAllMsg() {
document.getElementById("statusTip").innerText = "正在加载留言列表...";
let res = await sb.from("lt_message").select("*").order("time", {ascending: false});
let listBox = document.getElementById("listBox");
listBox.innerHTML = "";
if(res.error) {
document.getElementById("statusTip").innerText = "加载失败:" + res.error.message;
return;
}
if(!res.data || res.data.length === 0) {
document.getElementById("statusTip").innerText = "暂无留言";
return;
}
res.data.forEach(item => {
let card = document.createElement("div");
card.className = "msg-card";
let head = document.createElement("div");
head.className = "msg-head";
let unameSpan = document.createElement("span");
if(item.username === "kxyjs") {
unameSpan.innerText = "kxyjs-V官方账号(真的!!!!)";
unameSpan.className = "gold";
} else {
unameSpan.innerText = item.username;
}
let timeSpan = document.createElement("span");
timeSpan.innerText = new Date(item.time).toLocaleString("zh-CN");
head.appendChild(unameSpan);
head.appendChild(timeSpan);
let contentDiv = document.createElement("div");
contentDiv.className = "markdown-content";
// 这里把原始Markdown文本解析成HTML渲染出来
contentDiv.innerHTML = marked.parse(item.message);
card.appendChild(head);
card.appendChild(contentDiv);
listBox.appendChild(card);
});
document.getElementById("statusTip").innerText = `共${res.data.length}条留言,当前使用标识:${myIp}`;
}
// 页面完全加载后初始化,动态绑定事件,防自动误触发
document.addEventListener('DOMContentLoaded', async function() {
await getMyIpSmart();
// 动态绑定点击事件,替换原来的静态onclick
document.getElementById('sendBtn').addEventListener('click', userClickPost);
// 解除按钮禁用
document.getElementById('sendBtn').disabled = false;
// 开启就绪开关
pageReady = true;
await loadAllMsg();
// 每30秒自动刷新留言
setInterval(loadAllMsg, 30000);
});
// 颜色设置函数:选中当前光标位置,设置inline style
function setInlineStyle(styleName, color) {
const textarea = document.getElementById('mInput');
const start = textarea.selectionStart;
const end = textarea.selectionEnd;
const selectedText = textarea.value.substring(start, end);
// 如果没有选中内容,就插入一个span包裹
if (start === end) {
const newText = `<span style="${styleName}: ${color}">${color === 'white' ? ' ' : ''}</span>`;
textarea.value = textarea.value.substring(0, start) + newText + textarea.value.substring(end);
textarea.focus();
// 移动光标到新插入内容的末尾
textarea.setSelectionRange(start + newText.length, start + newText.length);
} else {
// 如果有选中内容,直接包裹选中内容
const newText = `<span style="${styleName}: ${color}">${selectedText}</span>`;
textarea.value = textarea.value.substring(0, start) + newText + textarea.value.substring(end);
textarea.focus();
// 移动光标到新插入内容的末尾
textarea.setSelectionRange(start + newText.length, start + newText.length);
}
}
</script>
</body>
</html>