File size: 19,506 Bytes
cf2569a
 
 
 
 
 
 
 
 
4f8826f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cf2569a
 
4f8826f
cf2569a
 
 
 
 
 
4f8826f
 
 
 
 
 
 
 
 
 
cf2569a
 
 
 
 
4f8826f
 
cf2569a
 
 
4f8826f
 
cf2569a
4f8826f
 
 
 
 
 
 
 
 
 
cf2569a
 
 
4f8826f
 
cf2569a
4f8826f
 
 
cf2569a
 
4f8826f
cf2569a
4f8826f
cf2569a
 
4f8826f
 
 
 
 
 
 
 
 
 
 
 
 
 
cf2569a
 
 
 
 
 
 
 
 
 
 
 
 
4f8826f
cf2569a
 
 
 
4f8826f
cf2569a
 
 
 
 
 
4f8826f
cf2569a
 
 
 
4f8826f
 
cf2569a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f8826f
cf2569a
 
 
 
 
 
 
 
 
 
 
 
 
 
4f8826f
 
cf2569a
 
4f8826f
cf2569a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f8826f
cf2569a
 
 
 
 
 
4f8826f
cf2569a
 
 
 
4f8826f
 
 
 
 
 
 
cf2569a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f8826f
cf2569a
4f8826f
 
cf2569a
 
 
 
 
4f8826f
cf2569a
 
 
4f8826f
 
 
 
 
 
 
 
 
 
 
 
cf2569a
 
4f8826f
 
 
cf2569a
 
 
 
 
4f8826f
cf2569a
 
4f8826f
 
cf2569a
 
 
 
 
 
 
 
4f8826f
cf2569a
4f8826f
cf2569a
4f8826f
cf2569a
 
 
 
4f8826f
 
 
cf2569a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f8826f
cf2569a
 
 
4f8826f
 
 
 
 
cf2569a
 
 
4f8826f
 
 
cf2569a
 
 
4f8826f
cf2569a
 
 
50428e9
 
 
 
 
4f8826f
50428e9
4f8826f
50428e9
 
4f8826f
50428e9
 
 
 
 
 
 
 
cf2569a
 
 
4f8826f
 
 
cf2569a
4f8826f
 
cf2569a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50428e9
cf2569a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50428e9
 
 
 
cf2569a
 
 
 
 
4f8826f
cf2569a
 
4f8826f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cf2569a
 
 
 
 
 
 
 
 
 
 
50428e9
 
 
 
 
 
 
 
 
 
 
 
 
 
cf2569a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50428e9
 
cf2569a
 
50428e9
 
cf2569a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50428e9
cf2569a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50428e9
cf2569a
 
 
 
 
 
 
50428e9
 
cf2569a
 
 
 
 
50428e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cf2569a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sublink — 订阅转换</title>
<style>
  *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

  /* 自定义滚动条 */
  ::-webkit-scrollbar { width: 4px; height: 4px; }
  ::-webkit-scrollbar-track { background: #060914; }
  ::-webkit-scrollbar-thumb { background: #00d4ff; border-radius: 2px; }

  /* CSS 自定义属性用于旋转边框动画 */
  @property --angle {
    syntax: '<angle>';
    initial-value: 0deg;
    inherits: false;
  }

  @keyframes spin-border {
    to { --angle: 360deg; }
  }

  /* 标题光标闪烁 */
  @keyframes blink {
    0%, 100% { opacity: 1; }
    50% { opacity: 0; }
  }

  /* 扫描线按钮效果 */
  @keyframes scan-line {
    0% { left: -100%; }
    100% { left: 150%; }
  }

  body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    background: #060914;
    color: #e2e8f0;
    min-height: 100vh;
    display: flex;
    align-items: flex-start;
    justify-content: center;
    padding: 2rem 1rem;
    position: relative;
  }

  /* 背景 canvas 全屏 */
  #bg-canvas {
    position: fixed;
    top: 0; left: 0;
    width: 100%; height: 100%;
    z-index: 0;
    pointer-events: none;
  }

  .container {
    width: 100%;
    max-width: 680px;
    position: relative;
    z-index: 1;
  }

  h1 {
    font-size: 1.6rem;
    font-weight: 700;
    margin-bottom: 0.25rem;
    color: #00d4ff;
    font-family: 'Courier New', Courier, monospace;
    letter-spacing: 0.15em;
    text-shadow: 0 0 20px #00d4ff, 0 0 40px rgba(0, 212, 255, 0.3);
  }

  .cursor {
    display: inline-block;
    animation: blink 1s step-end infinite;
    color: #00d4ff;
  }

  .subtitle {
    font-size: 0.75rem;
    color: #00d4ff;
    margin-bottom: 2rem;
    letter-spacing: 0.25em;
    text-transform: uppercase;
    opacity: 0.6;
  }

  /* Card 旋转渐变边框 */
  .card {
    background: rgba(10, 14, 28, 0.9);
    border-radius: 12px;
    padding: 1.5rem;
    position: relative;
    z-index: 0;
    overflow: hidden;
    backdrop-filter: blur(12px);
  }

  .card::before {
    content: '';
    position: absolute;
    inset: -1px;
    border-radius: 13px;
    background: conic-gradient(from var(--angle), #6366f1, #00d4ff, #6366f1);
    animation: spin-border 4s linear infinite;
    z-index: -1;
  }

  .field {
    margin-bottom: 1.25rem;
  }

  label {
    display: block;
    font-size: 0.8125rem;
    font-weight: 500;
    color: #94a3b8;
    margin-bottom: 0.4rem;
    text-transform: uppercase;
    letter-spacing: 0.08em;
  }

  textarea, select, input[type="text"] {
    width: 100%;
    background: rgba(5, 8, 18, 0.8);
    border: 1px solid #2d3348;
    border-radius: 8px;
    color: #e2e8f0;
    font-size: 0.9rem;
    padding: 0.625rem 0.75rem;
    outline: none;
    transition: border-color 0.2s, box-shadow 0.2s;
    font-family: inherit;
  }

  textarea:focus, select:focus, input[type="text"]:focus {
    border-color: #00d4ff;
    box-shadow: 0 0 0 2px rgba(0, 212, 255, 0.15), 0 0 12px rgba(0, 212, 255, 0.2);
  }

  textarea {
    resize: vertical;
    min-height: 80px;
  }

  select {
    cursor: pointer;
    appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%2364748b' d='M6 8L1 3h10z'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 0.75rem center;
    padding-right: 2rem;
  }

  select option { background: #0a0e1c; }

  /* 高级选项折叠 */
  .advanced-toggle {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    cursor: pointer;
    font-size: 0.8125rem;
    color: #64748b;
    user-select: none;
    margin-bottom: 1rem;
    border: none;
    background: none;
    padding: 0;
    letter-spacing: 0.08em;
    transition: color 0.2s;
  }

  .advanced-toggle:hover { color: #00d4ff; }

  .toggle-icon {
    display: inline-block;
    transition: transform 0.2s;
    font-style: normal;
  }

  .toggle-icon.open { transform: rotate(90deg); }

  #advanced-section { display: none; }
  #advanced-section.open { display: block; }

  /* Bool 开关行 */
  .bool-grid {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    margin-top: 0.4rem;
  }

  .bool-item {
    display: flex;
    align-items: center;
    gap: 0.375rem;
    background: rgba(5, 8, 18, 0.8);
    border: 1px solid #2d3348;
    border-radius: 6px;
    padding: 0.3rem 0.6rem;
    cursor: pointer;
    font-size: 0.8125rem;
    color: #94a3b8;
    transition: border-color 0.15s, color 0.15s, box-shadow 0.15s;
  }

  .bool-item:hover { border-color: #6366f1; color: #e2e8f0; }

  /* checked 态发光 */
  .bool-item:has(input:checked) {
    border-color: #6366f1;
    color: #a5b4fc;
    box-shadow: 0 0 6px rgba(99, 102, 241, 0.3);
  }

  .bool-item input[type="checkbox"] {
    width: auto;
    accent-color: #6366f1;
    cursor: pointer;
  }

  /* 两列布局 */
  .row-2 {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1rem;
  }

  /* 按钮 */
  .btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.4rem;
    border: none;
    border-radius: 8px;
    font-size: 0.9rem;
    font-weight: 500;
    cursor: pointer;
    transition: opacity 0.15s, box-shadow 0.2s;
    font-family: inherit;
    position: relative;
    overflow: hidden;
  }

  .btn:active { opacity: 0.7; }

  .btn-primary {
    background: linear-gradient(90deg, #6366f1, #00d4ff);
    color: #fff;
    padding: 0.65rem 1.5rem;
    width: 100%;
    box-shadow: 0 0 20px rgba(99, 102, 241, 0.4);
    letter-spacing: 0.05em;
  }

  /* 扫描光效 */
  .btn-primary::after {
    content: '';
    position: absolute;
    top: 0; left: -100%;
    width: 60%; height: 100%;
    background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.15), transparent);
    transition: left 0.4s ease;
  }

  .btn-primary:hover::after { left: 150%; }
  .btn-primary:hover { box-shadow: 0 0 30px rgba(0, 212, 255, 0.5); }

  .btn-sm {
    background: #2d3348;
    color: #e2e8f0;
    padding: 0.4rem 0.85rem;
    font-size: 0.8rem;
    transition: background 0.15s, color 0.15s;
  }

  .btn-sm:hover { background: #3d4460; color: #00d4ff; }

  /* 结果区域 */
  #result-section {
    margin-top: 1.25rem;
    display: none;
  }

  #result-section.show { display: block; }

  /* 终端风格结果框 */
  .result-box {
    background: rgba(5, 8, 18, 0.9);
    border: 1px solid #2d3348;
    border-left: 2px solid #00d4ff;
    border-radius: 8px;
    padding: 0.75rem 1rem;
    word-break: break-all;
    font-size: 0.8375rem;
    font-family: 'Courier New', Courier, monospace;
    color: #00d4ff;
    text-shadow: 0 0 8px rgba(0, 212, 255, 0.4);
    line-height: 1.5;
    margin-bottom: 0.75rem;
  }

  .result-actions {
    display: flex;
    gap: 0.5rem;
  }

  /* 错误提示 */
  #error-msg {
    display: none;
    background: #3b1f1f;
    border: 1px solid #7f1d1d;
    color: #fca5a5;
    border-radius: 8px;
    padding: 0.6rem 0.9rem;
    font-size: 0.8375rem;
    margin-top: 0.75rem;
  }

  #error-msg.show { display: block; }

  /* Toast:左下角,玻璃风格 */
  #toast {
    position: fixed;
    bottom: 1.5rem;
    left: 1.5rem;
    background: rgba(6, 9, 20, 0.9);
    backdrop-filter: blur(12px);
    border: 1px solid #00d4ff;
    color: #00d4ff;
    padding: 0.5rem 1rem;
    border-radius: 8px;
    font-size: 0.875rem;
    font-family: 'Courier New', monospace;
    letter-spacing: 0.05em;
    box-shadow: 0 0 16px rgba(0, 212, 255, 0.3);
    opacity: 0;
    transition: opacity 0.3s;
    pointer-events: none;
    z-index: 100;
  }

  #toast.show { opacity: 1; }

  /* 预览区域 */
  #preview-section { margin-top: 0.75rem; display: none; }
  #preview-section.show { display: block; }
  #preview-content {
    background: rgba(5, 8, 18, 0.95);
    border: 1px solid #2d3348;
    border-left: 2px solid #00d4ff;
    border-radius: 8px;
    padding: 0.75rem 1rem;
    font-family: 'Courier New', Courier, monospace;
    font-size: 0.75rem;
    color: #94a3b8;
    max-height: 400px;
    overflow-y: auto;
    white-space: pre;
    line-height: 1.4;
  }
  #preview-content.error { color: #fca5a5; }
</style>
</head>
<body>
<!-- 背景粒子网络 canvas -->
<canvas id="bg-canvas"></canvas>

<div class="container">
  <h1>SUBLINK<span class="cursor">_</span></h1>
  <p class="subtitle">Subscription Converter</p>

  <div class="card">
    <!-- 订阅链接 -->
    <div class="field">
      <label>订阅链接 <span style="color:#6366f1">*</span></label>
      <textarea id="url" placeholder="输入订阅链接,多个链接用 | 分隔"></textarea>
    </div>

    <!-- 目标格式 -->
    <div class="field">
      <label>目标格式 <span style="color:#6366f1">*</span></label>
      <select id="target-select">
        <option value="clash">Clash</option>
        <option value="surge4">Surge 4</option>
        <option value="quanx">QuantumultX</option>
        <option value="loon">Loon</option>
        <option value="v2ray">V2Ray</option>
        <option value="ss">SS</option>
        <option value="ssr">SSR</option>
        <option value="trojan">Trojan</option>
        <option value="mixed">Mixed</option>
        <option value="clash-list">Node List (Clash)</option>
      </select>
    </div>

    <!-- 高级选项折叠 -->
    <button class="advanced-toggle" type="button" id="advanced-toggle">
      <i class="toggle-icon" id="toggle-icon"></i>
      高级选项
    </button>

    <div id="advanced-section">
      <!-- 外部配置 -->
      <div class="field">
        <label>外部配置 (config URL)</label>
        <input type="text" id="config" placeholder="留空使用 subconverter 默认配置">
      </div>

      <!-- 文件名 -->
      <div class="field">
        <label>文件名 (filename)</label>
        <input type="text" id="filename" placeholder="下载时的文件名">
      </div>

      <!-- 包含 / 排除 -->
      <div class="row-2">
        <div class="field">
          <label>包含节点 (include)</label>
          <input type="text" id="include" placeholder="正则表达式">
        </div>
        <div class="field">
          <label>排除节点 (exclude)</label>
          <input type="text" id="exclude" placeholder="正则表达式">
        </div>
      </div>

      <!-- 分组 -->
      <div class="field">
        <label>代理分组 (group)</label>
        <input type="text" id="group" placeholder="自定义分组名">
      </div>

      <!-- Bool 开关 -->
      <div class="field">
        <label>开关选项</label>
        <div class="bool-grid">
          <label class="bool-item"><input type="checkbox" id="emoji" checked> emoji</label>
          <label class="bool-item"><input type="checkbox" id="udp"> udp</label>
          <label class="bool-item"><input type="checkbox" id="tfo"> tfo</label>
          <label class="bool-item"><input type="checkbox" id="sort"> sort</label>
          <label class="bool-item"><input type="checkbox" id="fdn"> fdn</label>
          <label class="bool-item"><input type="checkbox" id="expand" checked> expand</label>
          <label class="bool-item"><input type="checkbox" id="append_type"> append_type</label>
          <label class="bool-item"><input type="checkbox" id="scv"> scv</label>
          <label class="bool-item"><input type="checkbox" id="append_info" checked> append_info</label>
          <label class="bool-item"><input type="checkbox" id="new_name"> new_name</label>
        </div>
      </div>
    </div>

    <!-- 生成按钮 -->
    <button class="btn btn-primary" id="gen-btn" type="button">生成订阅链接</button>

    <!-- 错误 -->
    <div id="error-msg"></div>

    <!-- 结果 -->
    <div id="result-section">
      <div class="result-box" id="result-url"></div>
      <div class="result-actions">
        <button class="btn btn-sm" id="copy-btn" type="button">复制</button>
        <button class="btn btn-sm" id="open-btn" type="button">在新标签打开</button>
        <button class="btn btn-sm" id="preview-btn" type="button">预览内容</button>
      </div>
      <div id="preview-section">
        <pre id="preview-content"></pre>
      </div>
    </div>
  </div>
</div>

<div id="toast">[ COPIED ]</div>

<script>
// 背景粒子网络动画
(function() {
  const canvas = document.getElementById('bg-canvas');
  const ctx = canvas.getContext('2d');
  const PARTICLE_COUNT = 60;
  const CONNECT_DIST = 120;
  const COLOR = '0, 212, 255';

  let particles = [];

  function resize() {
    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight;
  }

  function initParticles() {
    particles = [];
    for (let i = 0; i < PARTICLE_COUNT; i++) {
      particles.push({
        x: Math.random() * canvas.width,
        y: Math.random() * canvas.height,
        vx: (Math.random() - 0.5) * 0.4,
        vy: (Math.random() - 0.5) * 0.4,
        r: Math.random() * 1.5 + 0.5,
      });
    }
  }

  function draw() {
    ctx.clearRect(0, 0, canvas.width, canvas.height);

    // 更新粒子位置
    particles.forEach(p => {
      p.x += p.vx;
      p.y += p.vy;
      // 边界反弹
      if (p.x < 0 || p.x > canvas.width)  p.vx *= -1;
      if (p.y < 0 || p.y > canvas.height) p.vy *= -1;

      // 绘制粒子点
      ctx.beginPath();
      ctx.arc(p.x, p.y, p.r, 0, Math.PI * 2);
      ctx.fillStyle = `rgba(${COLOR}, 0.5)`;
      ctx.fill();
    });

    // 绘制连线
    for (let i = 0; i < particles.length; i++) {
      for (let j = i + 1; j < particles.length; j++) {
        const dx = particles[i].x - particles[j].x;
        const dy = particles[i].y - particles[j].y;
        const dist = Math.sqrt(dx * dx + dy * dy);
        if (dist < CONNECT_DIST) {
          const alpha = (1 - dist / CONNECT_DIST) * 0.4;
          ctx.beginPath();
          ctx.moveTo(particles[i].x, particles[i].y);
          ctx.lineTo(particles[j].x, particles[j].y);
          ctx.strokeStyle = `rgba(${COLOR}, ${alpha})`;
          ctx.lineWidth = 0.5;
          ctx.stroke();
        }
      }
    }

    requestAnimationFrame(draw);
  }

  resize();
  initParticles();
  draw();
  window.addEventListener('resize', () => { resize(); initParticles(); });
})();

// 高级选项折叠/展开
const advancedToggle = document.getElementById('advanced-toggle');
const advancedSection = document.getElementById('advanced-section');
const toggleIcon = document.getElementById('toggle-icon');

advancedToggle.addEventListener('click', () => {
  const open = advancedSection.classList.toggle('open');
  toggleIcon.classList.toggle('open', open);
  toggleIcon.textContent = open ? '▼' : '▶';
});

// subconverter 实际默认值,与之相同的 bool 不传(null = 后端跳过)
const BOOL_DEFAULTS = {
  emoji:       true,
  udp:         false,
  tfo:         false,
  sort:        false,
  fdn:         false,
  expand:      true,
  append_type: false,
  scv:         false,
  append_info: true,
  new_name:    false,
};

// 生成链接
document.getElementById('gen-btn').addEventListener('click', async () => {
  const url = document.getElementById('url').value.trim();
  if (!url) { showError('请填写订阅链接'); return; }

  const targetVal = document.getElementById('target-select').value;

  // target 映射
  const targetMap = {
    'clash':      { target: 'clash' },
    'surge4':     { target: 'surge', ver: '4' },
    'quanx':      { target: 'quanx' },
    'loon':       { target: 'loon' },
    'v2ray':      { target: 'v2ray' },
    'ss':         { target: 'ss' },
    'ssr':        { target: 'ssr' },
    'trojan':     { target: 'trojan' },
    'mixed':      { target: 'mixed' },
    'clash-list': { target: 'clash', list: true },
  };

  const extra = targetMap[targetVal] || { target: targetVal };

  // 只传与默认值不同的 bool,相同的传 null(后端跳过)
  const boolFields = Object.keys(BOOL_DEFAULTS);
  const boolValues = {};
  boolFields.forEach(id => {
    const val = document.getElementById(id).checked;
    boolValues[id] = (val === BOOL_DEFAULTS[id]) ? null : val;
  });

  // list 来自 extra(clash-list 模式)
  if (extra.list !== undefined) {
    boolValues.list = extra.list;
    delete extra.list;
  }

  const body = {
    url,
    ...extra,
    config:   document.getElementById('config').value.trim() || null,
    filename: document.getElementById('filename').value.trim() || null,
    include:  document.getElementById('include').value.trim() || null,
    exclude:  document.getElementById('exclude').value.trim() || null,
    group:    document.getElementById('group').value.trim() || null,
    ...boolValues,
  };

  hideError();
  hidePreview();

  try {
    const res = await fetch('/api/convert', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify(body),
    });

    if (!res.ok) {
      const err = await res.json().catch(() => ({}));
      showError(err.detail || `请求失败 (${res.status})`);
      return;
    }

    const data = await res.json();
    const fullUrl = window.location.origin + data.url;
    document.getElementById('result-url').textContent = fullUrl;
    document.getElementById('result-section').classList.add('show');

    // 绑定按钮(每次生成都重新绑,避免重复监听器)
    const copyBtn = document.getElementById('copy-btn');
    const openBtn = document.getElementById('open-btn');
    const previewBtn = document.getElementById('preview-btn');

    copyBtn.onclick = () => {
      navigator.clipboard.writeText(fullUrl).then(() => showToast());
    };

    openBtn.onclick = () => window.open(fullUrl, '_blank');

    previewBtn.onclick = () => togglePreview(fullUrl, previewBtn);

  } catch (e) {
    showError('网络错误:' + e.message);
  }
});

// 预览内容 toggle
async function togglePreview(fullUrl, btn) {
  const section = document.getElementById('preview-section');
  const content = document.getElementById('preview-content');

  // 已展开则收起
  if (section.classList.contains('show')) {
    hidePreview();
    return;
  }

  btn.textContent = '加载中…';
  btn.disabled = true;

  try {
    const res = await fetch(fullUrl);
    const text = await res.text();
    const lines = text.split('\n');
    content.textContent = lines.slice(0, 500).join('\n') + (lines.length > 500 ? '\n... (已截断)' : '');
    content.classList.remove('error');
  } catch (e) {
    content.textContent = 'Error: ' + e.message;
    content.classList.add('error');
  }

  section.classList.add('show');
  btn.textContent = '预览内容';
  btn.disabled = false;
}

function hidePreview() {
  document.getElementById('preview-section').classList.remove('show');
  document.getElementById('preview-content').textContent = '';
  const btn = document.getElementById('preview-btn');
  if (btn) { btn.textContent = '预览内容'; btn.disabled = false; }
}

function showError(msg) {
  const el = document.getElementById('error-msg');
  el.textContent = msg;
  el.classList.add('show');
}

function hideError() {
  document.getElementById('error-msg').classList.remove('show');
}

function showToast() {
  const t = document.getElementById('toast');
  t.classList.add('show');
  setTimeout(() => t.classList.remove('show'), 2000);
}
</script>
</body>
</html>