forked from iim0663418/DB-Card
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathnfc-generator-bilingual.html
More file actions
1422 lines (1243 loc) · 62.2 KB
/
nfc-generator-bilingual.html
File metadata and controls
1422 lines (1243 loc) · 62.2 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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="zh-TW">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>🌐 雙語 NFC 數位名片生成器</title>
<script src="assets/qrcode.min.js"></script>
<script src="assets/security-utils.js"></script>
<script src="assets/bilingual-common.js"></script>
<link rel="stylesheet" href="assets/qrcode-style.css">
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'PingFang TC', 'Noto Sans TC', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
padding: 20px;
color: #333;
}
.container {
max-width: 800px;
margin: 0 auto;
background: white;
border-radius: 16px;
box-shadow: 0 10px 30px rgba(0,0,0,0.2);
overflow: hidden;
}
.header {
background: linear-gradient(135deg, #6868ac 0%, #4e4e81 100%);
color: white;
padding: 30px;
text-align: center;
}
.header h1 {
font-size: 2.2em;
margin-bottom: 10px;
font-weight: 700;
}
.header p {
font-size: 1.1em;
opacity: 0.9;
}
.form-container {
padding: 40px;
}
.form-group {
margin-bottom: 25px;
}
.form-group label {
display: block;
margin-bottom: 8px;
font-weight: 600;
color: #333;
font-size: 1.05em;
}
.required {
color: #e74c3c;
}
.input-group {
display: flex;
gap: 10px;
align-items: center;
}
.input-group input {
flex: 1;
}
.input-group .lang-label {
font-size: 0.9em;
color: #666;
min-width: 40px;
text-align: center;
}
input, select, textarea {
width: 100%;
padding: 12px 16px;
border: 2px solid #e1e5e9;
border-radius: 8px;
font-size: 1em;
transition: border-color 0.3s ease;
font-family: inherit;
}
input:focus, select:focus, textarea:focus {
outline: none;
border-color: #6868ac;
box-shadow: 0 0 0 3px rgba(104,104,172,0.1);
}
textarea {
resize: vertical;
min-height: 80px;
}
.capacity-monitor {
margin: 20px 0;
padding: 15px;
background: #f8f9fa;
border-radius: 8px;
border: 1px solid #dee2e6;
}
.capacity-bar {
width: 100%;
height: 10px;
background: #e9ecef;
border-radius: 5px;
overflow: hidden;
margin-bottom: 8px;
}
.capacity-fill {
height: 100%;
background: linear-gradient(90deg, #28a745, #ffc107, #dc3545);
transition: width 0.3s ease;
border-radius: 5px;
}
.capacity-text {
font-size: 14px;
color: #6c757d;
text-align: center;
font-weight: 500;
}
.generate-btn {
background: linear-gradient(135deg, #6868ac 0%, #4e4e81 100%);
color: white;
border: none;
padding: 16px 32px;
border-radius: 8px;
font-size: 1.1em;
font-weight: 600;
cursor: pointer;
transition: all 0.3s ease;
width: 100%;
margin-top: 20px;
}
.generate-btn:hover {
transform: translateY(-2px);
box-shadow: 0 8px 25px rgba(104,104,172,0.3);
}
.generate-btn:disabled {
background: #ccc;
cursor: not-allowed;
transform: none;
box-shadow: none;
}
.result-section {
margin-top: 30px;
padding: 25px;
background: #f8f9fa;
border-radius: 12px;
border: 1px solid #dee2e6;
display: none;
}
.result-section.show {
display: block;
animation: slideIn 0.5s ease-out;
}
@keyframes slideIn {
from {
opacity: 0;
transform: translateY(20px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.result-url {
background: white;
border: 2px solid #6868ac;
border-radius: 8px;
padding: 15px;
font-family: monospace;
font-size: 0.9em;
word-break: break-all;
margin: 15px 0;
max-height: 150px;
overflow-y: auto;
}
.copy-btn {
background: #28a745;
color: white;
border: none;
padding: 10px 20px;
border-radius: 6px;
cursor: pointer;
font-size: 0.9em;
margin-right: 10px;
transition: background 0.3s ease;
}
.copy-btn:hover {
background: #218838;
}
.warning {
background: #fff3cd;
border: 1px solid #ffeaa7;
color: #856404;
padding: 12px;
border-radius: 6px;
margin: 15px 0;
font-size: 0.9em;
}
.success {
background: #d4edda;
border: 1px solid #c3e6cb;
color: #155724;
padding: 12px;
border-radius: 6px;
margin: 15px 0;
font-size: 0.9em;
}
.help-text {
font-size: 0.9em;
color: #666;
margin-top: 5px;
line-height: 1.4;
}
@media (max-width: 768px) {
.form-container {
padding: 25px;
}
.input-group {
flex-direction: column;
gap: 8px;
}
.input-group .lang-label {
min-width: auto;
text-align: left;
}
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>🌐 雙語 NFC 數位名片生成器</h1>
<p>創建支援中英文切換的國際化數位名片</p>
</div>
<div class="form-container">
<form id="cardForm">
<div class="form-group">
<label>版面類型:</label>
<select id="layoutType">
<option value="official-yanping">機關版(延平大樓)</option>
<option value="official-xinyi">機關版(新光大樓)</option>
<option value="personal">個人版(無 Logo)</option>
</select>
</div>
<div class="form-group">
<label>姓名 / Name <span class="required">*</span>:</label>
<div class="input-group">
<div class="lang-label">🇹🇼</div>
<input type="text" id="nameZh" placeholder="中文姓名" required maxlength="20">
</div>
<div class="input-group" style="margin-top: 8px;">
<div class="lang-label">🇺🇸</div>
<input type="text" id="nameEn" placeholder="English Name" required maxlength="30">
</div>
<div class="help-text">將顯示為:王小明 / Wang Xiaoming</div>
</div>
<div class="form-group">
<label>職稱 / Title <span class="required" id="titleRequiredMark">*</span>:</label>
<div class="input-group">
<div class="lang-label">🇹🇼</div>
<input type="text" id="titleZh" placeholder="中文職稱" required maxlength="30">
</div>
<div class="input-group" style="margin-top: 8px;">
<div class="lang-label">🇺🇸</div>
<input type="text" id="titleEn" placeholder="English Title" required maxlength="40">
</div>
<div class="help-text">例如:科長 / Section Chief</div>
</div>
<div class="form-group">
<label>部門:</label>
<input type="text" id="department" placeholder="部門名稱(單語即可)" maxlength="50">
<div class="help-text">原始專案設計由前端對照數位發展部官方單位名稱翻譯,如 Fork 則不支援翻譯</div>
</div>
<div class="form-group">
<label>電子郵件 <span class="required" id="emailRequiredMark">*</span>:</label>
<input type="email" id="email" placeholder="your.email@moda.gov.tw" required>
</div>
<div class="form-group">
<label>電話:</label>
<input type="tel" id="phone" placeholder="02-2380-0411">
</div>
<div class="form-group">
<label>手機號碼:</label>
<input type="tel" id="mobile" placeholder="0912-345-678">
</div>
<div class="form-group" id="organizationGroup" style="display: none;">
<label>組織名稱 / Organization:</label>
<div class="input-group">
<div class="lang-label">🇹🇼</div>
<input type="text" id="organizationZh" placeholder="中文組織名稱" maxlength="50">
</div>
<div class="input-group" style="margin-top: 8px;">
<div class="lang-label">🇺🇸</div>
<input type="text" id="organizationEn" placeholder="English Organization" maxlength="60">
</div>
<div class="help-text">個人版專屬:自訂組織名稱,留空則不顯示</div>
</div>
<div class="form-group" id="addressGroup" style="display: none;">
<label>地址 / Address:</label>
<div class="form-group">
<label style="display: inline-flex; align-items: center; margin-right: 20px;">
<input type="radio" name="addressType" value="text" checked style="margin-right: 8px;">
文字地址 / Text Address
</label>
<label style="display: inline-flex; align-items: center; margin-right: 20px;">
<input type="radio" name="addressType" value="coords" style="margin-right: 8px;">
GPS 座標 / GPS Coordinates
</label>
<label style="display: inline-flex; align-items: center;">
<input type="radio" name="addressType" value="mapId" style="margin-right: 8px;">
Google Maps 連結 / Maps Link
</label>
</div>
<div id="textAddressGroup">
<div class="input-group">
<div class="lang-label">🇹🇼</div>
<input type="text" id="addressZh" placeholder="中文地址" maxlength="100">
</div>
<div class="input-group" style="margin-top: 8px;">
<div class="lang-label">🇺🇸</div>
<input type="text" id="addressEn" placeholder="English Address" maxlength="120">
</div>
<div class="help-text">個人版專屬:自訂地址,留空則不顯示</div>
</div>
<div id="coordsAddressGroup" style="display: none;">
<div class="input-group">
<div class="lang-label">📍</div>
<input type="number" id="latitude" step="0.0000001" min="-90" max="90" placeholder="緯度 / Latitude (例如: 25.0339641)" style="flex: 1;">
</div>
<div class="input-group" style="margin-top: 8px;">
<div class="lang-label">📍</div>
<input type="number" id="longitude" step="0.0000001" min="-180" max="180" placeholder="經度 / Longitude (例如: 121.5644683)" style="flex: 1;">
</div>
<div class="help-text">
💡 <strong>GPS 座標說明 / GPS Coordinates Guide:</strong><br>
• 使用十進位格式,支援 7 位小數精度 (~1 公分精度) / Use decimal format, supports 7 decimal precision (~1cm accuracy)<br>
• 可從 Google Maps 取得座標 / Get coordinates from Google Maps: 右鍵點擊地點 → 點擊座標即可複製<br>
• 座標會自動轉換為 Google Maps 連結,點擊即可導航 / Coordinates auto-convert to Google Maps link for navigation<br>
• 留空則不顯示地址資訊 / Leave empty to hide address
</div>
</div>
<div id="mapIdAddressGroup" style="display: none;">
<div class="input-group">
<div class="lang-label">🔗</div>
<input type="text" id="googleMapsUrl" placeholder="例如 / e.g.: https://maps.app.goo.gl/eKmgu7PqiUfJ2v5D9" style="flex: 1;">
</div>
<div class="help-text">
💡 <strong>Google Maps 連結說明 / Google Maps Link Guide:</strong><br>
• 開啟 Google Maps → 找到目標地點 / Open Google Maps → Find target location<br>
• 點擊「分享」→ 複製短網址 (maps.app.goo.gl) / Click "Share" → Copy short URL<br>
• 系統會自動提取 ID,僅儲存 17 字元 / System auto-extracts ID, stores only 17 characters<br>
• <strong>容量優勢 / Capacity Advantage:</strong>相比 GPS 座標節省 23% 空間 / 23% less space vs GPS coordinates<br>
• 留空則不顯示地址資訊 / Leave empty to hide address
</div>
<div style="margin-top: 15px;">
<label>顯示標籤 / Display Label:</label>
<div class="input-group">
<div class="lang-label">🇹🇼</div>
<input type="text" id="googleMapsLabelZh" placeholder="中文標籤(例如:辦公室、會議地點)" maxlength="20" style="flex: 1;">
</div>
<div class="input-group" style="margin-top: 8px;">
<div class="lang-label">🇺🇸</div>
<input type="text" id="googleMapsLabelEn" placeholder="English Label (e.g.: Office, Meeting Venue)" maxlength="20" style="flex: 1;">
</div>
<div class="help-text">
💡 <strong>自訂標籤說明 / Custom Label Guide:</strong><br>
• 自訂地圖連結的顯示文字 / Customize the display text for map link<br>
• 中文 <span id="labelCharCountZh" style="color: #059669;">0</span>/20 字 | 英文 <span id="labelCharCountEn" style="color: #059669;">0</span>/20 字 / CN <span id="labelCharCountZh2" style="color: #059669;">0</span>/20 | EN <span id="labelCharCountEn2" style="color: #059669;">0</span>/20<br>
• 留空則顯示預設「Google Maps」 / Leave empty to show default "Google Maps"<br>
• <strong>範例 / Examples:</strong>辦公室/Office、會議室A/Meeting Room A、客服中心/Customer Service
</div>
</div>
</div>
</div>
<div class="form-group">
<label>大頭貼 URL:</label>
<input type="url" id="avatar" placeholder="https://i.imgur.com/example.jpg" maxlength="200">
<div class="help-text">
推薦使用 <a href="https://imgur.com" target="_blank">Imgur</a>、<a href="https://postimages.org" target="_blank">PostImages</a>(複製 Direct link) 或 <a href="https://drive.google.com" target="_blank">Google Drive</a>(分享連結自動轉換) 上傳圖片,
或留空以節省 NFC 容量
</div>
</div>
<div class="form-group">
<label>問候語 / Greetings:</label>
<div class="input-group">
<div class="lang-label">🇹🇼</div>
<textarea id="greetingsZh" placeholder="歡迎認識我! 很高興與您交流" maxlength="200"></textarea>
</div>
<div class="input-group" style="margin-top: 8px;">
<div class="lang-label">🇺🇸</div>
<textarea id="greetingsEn" placeholder="Nice to meet you! Welcome to connect" maxlength="200"></textarea>
</div>
<div class="help-text">每行一個問候語,將輪播顯示。雙語問候語會根據當前語言顯示。</div>
</div>
<div class="form-group">
<label>社群媒體資訊:</label>
<textarea id="socialNote" placeholder="GitHub: yourname FB: yourname IG: @yourname LINE: @your-line-id Twitter: @yourname LinkedIn: yourname YouTube: @yourname Discord: invite-code" maxlength="300"></textarea>
<div class="help-text">
<strong>🌐 支援平台:</strong>GitHub, Facebook, Instagram, LINE, Twitter/X, LinkedIn<br>
<strong>📝 格式說明:</strong>每行一個平台,格式為「平台名: 帳號」<br><br>
<strong>📺 平台格式範例:</strong><br>
• <code>GitHub: username</code> → 🐙 GitHub [⭐ 造訪] 按鈕<br>
• <code>FB: username</code> → 📘 Facebook [👥 造訪頁面] 按鈕<br>
• <code>IG: @username</code> → 📷 Instagram [❤️ 追蹤] 按鈕<br>
• <code>LINE: @official-id</code> → 💬 LINE [🏢 加入官方] 按鈕<br>
• <code>LINE: personal-id</code> → 💬 LINE [👤 加好友] 按鈕<br>
• <code>Twitter: @username</code> → 🐦 Twitter [👥 追蹤] 按鈕<br>
• <code>LinkedIn: username</code> → 💼 LinkedIn [🤝 連結] 按鈕<br><br>
<strong>🌍 雙語支援:</strong>按鈕文字會根據當前語言自動切換中英文<br>
<strong>🔒 安全設計:</strong>所有連結都經過安全驗證,防止惡意網址<br>
<strong>📱 即時預覽:</strong>輸入後可在下方看到按鈕效果預覽
</div>
<!-- 社群連結預覽 -->
<div id="socialPreview" style="display: none; margin-top: 10px; padding: 12px; background: #f8f9fa; border: 1px solid #dee2e6; border-radius: 6px; font-size: 0.9em; line-height: 1.5;">
<strong style="color: #6868ac;">📱 預覽效果:</strong><br>
<div id="socialPreviewContent" style="margin-top: 8px; color: #495057;"></div>
</div>
</div>
<!-- 容量監控 -->
<div class="capacity-monitor">
<div class="capacity-bar">
<div class="capacity-fill" id="capacityFill" style="width: 0%"></div>
</div>
<div class="capacity-text" id="capacityText">0/492 bytes (0%)</div>
</div>
<button type="button" onclick="generateBilingualNFC()" class="generate-btn">
🚀 生成雙語 NFC 連結
</button>
</form>
<div id="result" class="result-section">
<h3>✅ 生成成功!</h3>
<p>請複製以下連結並寫入 NFC 卡片:</p>
<div id="resultUrl" class="result-url"></div>
<div>
<button onclick="copyToClipboard(event)" class="copy-btn">📋 複製連結</button>
<button onclick="testLink()" class="copy-btn" style="background: #007bff;">🔗 測試連結</button>
</div>
<div id="qrCodeSection" style="margin-top: 25px; border: 2px solid #3b82f6; border-radius: 10px; padding: 20px; background-color: #f0f7ff; animation: pulse 2s infinite;">
<h3 style="color: #3b82f6; margin-bottom: 15px; font-size: 1.3em;">🔍 QR 碼下載</h3>
<div class="qr-container" id="qrcode" style="margin-bottom: 20px; text-align: center;">QR 碼載入中...</div>
<button onclick="downloadQRCode(event)" class="copy-btn" style="background: #3b82f6; margin-bottom: 10px; padding: 15px 20px; font-size: 1.1em; width: 100%; font-weight: bold; box-shadow: 0 4px 6px rgba(59, 130, 246, 0.3); border-radius: 8px; cursor: pointer; transition: all 0.3s ease;">
💾 下載 QR 碼圖片
</button>
</div>
<style>
@keyframes pulse {
0% {
box-shadow: 0 0 0 0 rgba(59, 130, 246, 0.4);
}
70% {
box-shadow: 0 0 0 10px rgba(59, 130, 246, 0);
}
100% {
box-shadow: 0 0 0 0 rgba(59, 130, 246, 0);
}
}
/* Stop animation after 5 seconds */
#qrCodeSection {
animation: pulse 2s 3;
}
</style>
<div class="help-text" style="margin-top: 15px; line-height: 1.5;">
<strong>下一步:</strong><br>
<div style="display: flex; margin-top: 10px;">
<div style="flex: 1; padding-right: 10px; border-right: 1px solid #e5e7eb;">
<strong style="color: #4f46e5;">📱 NFC 卡片方式:</strong><br>
1. 使用 NFC 寫入工具(如 NFC Tools)<br>
2. 選擇「URL/URI」記錄類型<br>
3. 貼上上方連結並寫入 NFC 卡片<br>
4. 用手機觸碰測試
</div>
<div style="flex: 1; padding-left: 10px;">
<strong style="color: #4f46e5;">🔍 QR 碼方式:</strong><br>
1. 點擊上方「下載 QR 碼圖片」按鈕<br>
2. 將下載的 QR 碼圖片列印或分享<br>
3. 他人可使用手機相機掃描 QR 碼<br>
4. 自動開啟您的數位名片
</div>
</div>
</div>
</div>
</div>
</div>
<script>
let generatedUrl = '';
const SAFE_AVATAR_URL_PREFIX = /^data:image\/(png|jpe?g|gif|webp);base64,/i;
const FILENAME_UNSAFE_CHARS = /[\\/:*?"<>|]/g;
function sanitizeFileName(rawName, fallback) {
const trimmed = safeTrim(rawName);
if (!trimmed) {
return fallback;
}
const sanitized = trimmed.replace(FILENAME_UNSAFE_CHARS, '_').replace(/\s+/g, ' ').trim();
return sanitized || fallback;
}
function createDownloadFileName(nameZh, nameEn) {
const sanitizedZh = sanitizeFileName(nameZh, '');
if (sanitizedZh) {
return `${sanitizedZh}-數位名片.png`;
}
const sanitizedEn = sanitizeFileName(nameEn, '');
if (sanitizedEn) {
return `${sanitizedEn}-digital-business-card.png`;
}
return 'digital-business-card.png';
}
function buildCardUrl(targetPage, paramKey, encodedPayload) {
try {
const targetUrl = new URL(targetPage, window.location.href);
targetUrl.searchParams.set(paramKey, encodedPayload);
return targetUrl.toString();
} catch (error) {
logSecurityEvent('buildCardUrl', 'URL construction failed', { error: error.message, targetPage });
return '';
}
}
function safeTrim(value) {
return typeof value === 'string' ? value.trim() : '';
}
function convertGoogleDriveUrl(url) {
if (!url) return url;
try {
const match = url.match(/\/file\/d\/([a-zA-Z0-9_-]+)/);
if (match) {
return `https://drive.google.com/uc?export=view&id=${match[1]}`;
}
if (url.includes('drive.google.com/uc?')) {
return url;
}
} catch (error) {
logSecurityEvent('convertGoogleDriveUrl', 'Conversion failed', { error: error.message });
}
return url;
}
function isSafeUrl(url) {
if (!url) return false;
if (SAFE_AVATAR_URL_PREFIX.test(url)) {
return true;
}
if (typeof SecurityUtils !== 'undefined' && typeof SecurityUtils.validateURL === 'function') {
return SecurityUtils.validateURL(url);
}
try {
const parsed = new URL(url);
return ['http:', 'https:'].includes(parsed.protocol);
} catch (_) {
return false;
}
}
function normalizeCoordinate(value, type = 'lat') {
if (typeof SecurityUtils !== 'undefined' && typeof SecurityUtils.normalizeCoordinate === 'function') {
const normalized = SecurityUtils.normalizeCoordinate(value, type, 6);
return typeof normalized === 'number' ? normalized : null;
}
if (value === null || value === undefined || value === '') {
return null;
}
const numeric = typeof value === 'number' ? value : parseFloat(String(value).trim());
if (!Number.isFinite(numeric)) {
return null;
}
const limit = type === 'lng' ? 180 : 90;
if (Math.abs(numeric) > limit) {
return null;
}
return parseFloat(numeric.toFixed(6));
}
function normalizeGoogleMapsId(input) {
if (typeof SecurityUtils !== 'undefined' && typeof SecurityUtils.normalizeGoogleMapsId === 'function') {
return SecurityUtils.normalizeGoogleMapsId(input);
}
if (typeof input !== 'string') {
return '';
}
const trimmed = input.trim();
if (!trimmed) {
return '';
}
const pattern = /(?:https?:\/\/)?(?:www\.)?maps\.app\.goo\.gl\/([a-zA-Z0-9]+)/i;
const match = trimmed.match(pattern);
const candidate = match && match[1] ? match[1] : trimmed;
return /^[a-zA-Z0-9]{10,25}$/.test(candidate) ? candidate : '';
}
function getValidatedAvatarUrl(rawUrl) {
const trimmed = safeTrim(rawUrl);
if (!trimmed) {
return '';
}
if (SAFE_AVATAR_URL_PREFIX.test(trimmed)) {
return trimmed;
}
const converted = convertGoogleDriveUrl(trimmed);
if (isSafeUrl(converted)) {
return converted;
}
logSecurityEvent('collectFormData', 'Blocked unsafe avatar URL', { url: converted });
return '';
}
function legacyCopyToClipboard(text) {
const textArea = document.createElement('textarea');
textArea.value = text;
textArea.setAttribute('readonly', '');
textArea.style.position = 'fixed';
textArea.style.opacity = '0';
document.body.appendChild(textArea);
textArea.select();
let success = false;
try {
success = document.execCommand('copy');
} catch (_) {
success = false;
}
document.body.removeChild(textArea);
return success;
}
// 即時容量監控
function updateCapacityMonitor() {
const data = collectFormData();
if (!data) return;
// 使用動態域名計算容量
const encoded = encodeCompact(data);
const layoutType = document.getElementById('layoutType').value;
let targetPage;
if (layoutType === 'personal') {
targetPage = 'index-bilingual-personal.html';
} else if (layoutType === 'official-xinyi') {
targetPage = 'index1-bilingual.html';
} else {
targetPage = 'index-bilingual.html';
}
const testUrl = buildCardUrl(targetPage, 'data', encoded);
if (!testUrl) {
logSecurityEvent('updateCapacityMonitor', 'Failed to build preview URL', { targetPage });
return;
}
const urlSize = new Blob([testUrl]).size;
const percentage = Math.min((urlSize / 492) * 100, 100);
document.getElementById('capacityFill').style.width = `${percentage}%`;
document.getElementById('capacityText').textContent =
`${urlSize}/492 bytes (${Math.round(percentage)}%)`;
// 顏色警告
const fillElement = document.getElementById('capacityFill');
if (percentage > 90) {
fillElement.style.background = '#dc3545';
} else if (percentage > 70) {
fillElement.style.background = '#ffc107';
} else {
fillElement.style.background = '#28a745';
}
// 更新社群連結預覽
updateSocialPreview();
}
// 社群連結預覽功能
function updateSocialPreview() {
const socialNote = document.getElementById('socialNote').value.trim();
const previewElement = document.getElementById('socialPreview');
const previewContent = document.getElementById('socialPreviewContent');
if (!previewElement || !previewContent) return;
if (socialNote) {
// 使用簡化版的社群連結處理
const processedLinks = processSocialLinksPreview(socialNote);
SecurityUtils.safeRender(previewContent, processedLinks, true);
previewElement.style.display = 'block';
} else {
previewElement.style.display = 'none';
}
}
// 簡化版社群連結處理(用於預覽)
function processSocialLinksPreview(text) {
if (!text || !text.trim()) return '';
const lines = text.split('\n').filter(line => line.trim());
const processedLines = lines.map(line => {
let processedLine = line.trim();
// 簡化版處理,只顯示格式化的文字和圖示
if (/^GitHub:/i.test(processedLine)) {
const url = processedLine.replace(/^GitHub:\s*/i, '').trim();
return `<span style="color: #6868ac; font-weight: 500;">🐙 GitHub:</span> <span style="color: #495057;">${url}</span> <small style="color: #28a745;">✓ 可點擊</small>`;
}
else if (/^FB:/i.test(processedLine)) {
const url = processedLine.replace(/^FB:\s*/i, '').trim();
return `<span style="color: #6868ac; font-weight: 500;">📘 Facebook:</span> <span style="color: #495057;">${url}</span> <small style="color: #28a745;">✓ 可點擊</small>`;
}
else if (/^IG:/i.test(processedLine)) {
const url = processedLine.replace(/^IG:\s*/i, '').trim();
return `<span style="color: #6868ac; font-weight: 500;">📷 Instagram:</span> <span style="color: #495057;">${url}</span> <small style="color: #28a745;">✓ 可點擊</small>`;
}
else if (/^LINE:/i.test(processedLine)) {
const url = processedLine.replace(/^LINE:\s*/i, '').trim();
const isOfficial = url.startsWith('@');
const friendType = isOfficial ? '官方帳號' : '個人帳號';
return `<span style="color: #6868ac; font-weight: 500;">💬 LINE:</span> <span style="color: #495057;">${url}</span> <small style="color: #17a2b8;">(${friendType})</small> <small style="color: #28a745;">✓ 可加好友</small>`;
}
else if (/^(Twitter|X):/i.test(processedLine)) {
const [platform, ...urlParts] = processedLine.split(':');
const url = urlParts.join(':').trim();
return `<span style="color: #6868ac; font-weight: 500;">🐦 ${platform}:</span> <span style="color: #495057;">${url}</span> <small style="color: #28a745;">✓ 可追蹤</small>`;
}
else if (/^LinkedIn:/i.test(processedLine)) {
const url = processedLine.replace(/^LinkedIn:\s*/i, '').trim();
return `<span style="color: #6868ac; font-weight: 500;">💼 LinkedIn:</span> <span style="color: #495057;">${url}</span> <small style="color: #28a745;">✓ 可連結</small>`;
}
else if (/^YouTube:/i.test(processedLine)) {
const url = processedLine.replace(/^YouTube:\s*/i, '').trim();
return `<span style="color: #6868ac; font-weight: 500;">📺 YouTube:</span> <span style="color: #495057;">${url}</span> <small style="color: #28a745;">✓ 可訂閱</small>`;
}
else if (/^Discord:/i.test(processedLine)) {
const url = processedLine.replace(/^Discord:\s*/i, '').trim();
return `<span style="color: #6868ac; font-weight: 500;">🎮 Discord:</span> <span style="color: #495057;">${url}</span> <small style="color: #28a745;">✓ 可加入</small>`;
}
return `<span style="color: #6c757d;">${processedLine}</span>`;
});
return processedLines.join('<br>');
}
// 輸入清理函數
function sanitizeInput(input) {
return input.replace(/[<>"'&]/g, function(match) {
const map = {'<': '<', '>': '>', '"': '"', "'": ''', '&': '&'};
return map[match];
});
}
// 收集表單資料
function collectFormData() {
const layoutTypeElement = document.getElementById('layoutType');
const layoutType = layoutTypeElement ? layoutTypeElement.value : 'official-yanping';
const isPersonal = layoutType === 'personal';
const nameZh = sanitizeInput(document.getElementById('nameZh').value.trim());
const nameEn = sanitizeInput(document.getElementById('nameEn').value.trim());
const titleZh = sanitizeInput(document.getElementById('titleZh').value.trim());
const titleEn = sanitizeInput(document.getElementById('titleEn').value.trim());
if (!nameZh || !nameEn) {
return null;
}
if (!isPersonal && (!titleZh || !titleEn)) {
return null;
}
// 處理雙語問候語
const greetingsZhText = document.getElementById('greetingsZh').value.trim();
const greetingsEnText = document.getElementById('greetingsEn').value.trim();
const greetingsZh = greetingsZhText ? greetingsZhText.split('\n').filter(g => g.trim()) : ['歡迎認識我!'];
const greetingsEn = greetingsEnText ? greetingsEnText.split('\n').filter(g => g.trim()) : ['Nice to meet you!'];
// 將雙語問候語結合
const maxLength = Math.max(greetingsZh.length, greetingsEn.length);
const bilingualGreetings = [];
for (let i = 0; i < maxLength; i++) {
const zh = greetingsZh[i] || greetingsZh[greetingsZh.length - 1];
const en = greetingsEn[i] || greetingsEn[greetingsEn.length - 1];
bilingualGreetings.push(`${zh}~${en}`);
}
// 轉換 Google Drive 分享連結為直接圖片連結
function convertGoogleDriveUrl(url) {
if (!url) return url;
// 匹配 Google Drive 分享連結格式
const match = url.match(/\/file\/d\/([a-zA-Z0-9_-]+)/);
if (match) {
const fileId = match[1];
return `https://drive.google.com/uc?export=view&id=${fileId}`;
}
// 如果已經是直接連結格式,返回原連結
if (url.includes('drive.google.com/uc?')) {
return url;
}
return url;
}
const rawAvatarInput = document.getElementById('avatar').value;
const avatar = getValidatedAvatarUrl(rawAvatarInput);
if (!avatar && safeTrim(rawAvatarInput)) {
alert('請輸入有效的圖片 URL');
return null;
}
// 收集組織和地址(僅個人版)
let organization = '';
let address = '';
let location = null;
if (layoutType === 'personal') {
const organizationZh = sanitizeInput(document.getElementById('organizationZh').value.trim());
const organizationEn = sanitizeInput(document.getElementById('organizationEn').value.trim());
if (organizationZh || organizationEn) {
organization = `${organizationZh}~${organizationEn}`;
}
// 判斷地址類型:文字地址、GPS 座標或 Google Maps ID
const addressType = document.querySelector('input[name="addressType"]:checked');
if (addressType && addressType.value === 'coords') {
// GPS 座標模式
const lat = document.getElementById('latitude').value.trim();
const lng = document.getElementById('longitude').value.trim();
if (lat && lng) {
const sanitizedLat = normalizeCoordinate(lat, 'lat');
const sanitizedLng = normalizeCoordinate(lng, 'lng');
if (sanitizedLat === null || sanitizedLng === null) {
alert('請輸入有效的緯度與經度(緯度範圍 -90~90,經度範圍 -180~180),最多 6 位小數。');
return null;
}
location = {
coords: {
lat: sanitizedLat,
lng: sanitizedLng
}
};
}
} else if (addressType && addressType.value === 'mapId') {
// Google Maps ID 模式
const mapsUrl = document.getElementById('googleMapsUrl').value.trim();
if (mapsUrl) {
const normalizedId = normalizeGoogleMapsId(mapsUrl);
if (!normalizedId) {
if (typeof SecurityUtils !== 'undefined' && typeof SecurityUtils.logSecurityEvent === 'function') {
SecurityUtils.logSecurityEvent('collectFormData', 'Invalid Google Maps ID provided', { input: mapsUrl });
}
alert('無效的 Google Maps 連結或 ID。請使用 maps.app.goo.gl 分享連結或有效的分享 ID。');
return null;
}
location = {
mapId: normalizedId
};
// 收集雙語自訂標籤
const labelZh = sanitizeInput(document.getElementById('googleMapsLabelZh').value.trim());
const labelEn = sanitizeInput(document.getElementById('googleMapsLabelEn').value.trim());
if (labelZh || labelEn) {
location.label = `${labelZh}~${labelEn}`;
}
}
} else {
// 文字地址模式
const addressZh = sanitizeInput(document.getElementById('addressZh').value.trim());
const addressEn = sanitizeInput(document.getElementById('addressEn').value.trim());
if (addressZh || addressEn) {
address = `${addressZh}~${addressEn}`;
}
}
}
const result = {
name: `${nameZh}~${nameEn}`,
title: `${titleZh}~${titleEn}`,
department: sanitizeInput(document.getElementById('department').value.trim()),
email: document.getElementById('email').value.trim(),
phone: document.getElementById('phone').value.trim(),
mobile: document.getElementById('mobile').value.trim(),
avatar: avatar,
greetings: bilingualGreetings.map(g => sanitizeInput(g)),
socialNote: sanitizeInput(document.getElementById('socialNote').value.trim()),
organization: organization,
address: address
};
// 如果有座標,加入 location 物件
if (location) {
result.location = location;
}
return result;
}
// 生成雙語 NFC 連結
function generateBilingualNFC() {
const layoutType = document.getElementById('layoutType').value;
const isPersonal = layoutType === 'personal';
const data = collectFormData();
if (!data) {
const message = isPersonal ? '請至少填寫中文與英文姓名。' : '請填寫必填欄位:中英文姓名和職稱';
alert(message);
return;
}
if (!data.email && !isPersonal) {
alert('請填寫電子郵件');
return;
}
// 檢查容量(使用動態域名)
const testEncoded = encodeCompact(data);
const testLayoutType = layoutType;
let testTargetPage;
if (testLayoutType === 'personal') {
testTargetPage = 'index-bilingual-personal.html';
} else if (testLayoutType === 'official-xinyi') {
testTargetPage = 'index1-bilingual.html';
} else {
testTargetPage = 'index-bilingual.html';
}
const testUrl = buildCardUrl(testTargetPage, 'data', testEncoded);
if (!testUrl) {
showWarning('URL 生成失敗,請稍後再試。');