-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathcsdn2md.js
More file actions
executable file
·5497 lines (4972 loc) · 233 KB
/
csdn2md.js
File metadata and controls
executable file
·5497 lines (4972 loc) · 233 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
// ==UserScript==
// @name csdn2md - 批量下载CSDN文章为Markdown
// @namespace http://tampermonkey.net/
// @version 3.4.1
// @description 下载CSDN文章为Markdown格式,支持专栏批量下载。CSDN排版经过精心调教,最大程度支持CSDN的全部Markdown语法:KaTeX内联公式、KaTeX公式块、图片、内联代码、代码块、Bilibili视频控件、有序/无序/任务/自定义列表、目录、注脚、加粗斜体删除线下滑线高亮、内容居左/中/右、引用块、链接、快捷键(kbd)、表格、上下标、甘特图、UML图、FlowChart流程图
// @author ShizuriYuki
// @match https://*.csdn.net/*
// @icon https://g.csdnimg.cn/static/logo/favicon32.ico
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_addStyle
// @grant GM_xmlhttpRequest
// @run-at document-idle
// @license PolyForm Strict License 1.0.0 https://polyformproject.org/licenses/strict/1.0.0/
// @supportURL https://github.com/Qalxry/csdn2md
// @require https://cdn.jsdmirror.com/gh/Qalxry/csdn2md/plugins/jszip.min.js#sha256-yeSlK6wYruTz+Q0F+8pgP1sPW/HOjEXmC7TtOiyy7YY=
// @require https://cdn.jsdmirror.com/gh/Qalxry/csdn2md/plugins/fflate.min.js#sha256-w7NPLp9edNTX1k4BysegwBlUxsQGQU1CGFx7U9aHXd8=
// @require https://cdn.jsdmirror.com/gh/Qalxry/csdn2md/plugins/streamSaver.min.js#sha256-VxQm++CYEdHipBjKWh4QQHHOYZmyo8F/7dJQxG11xFM=
// @require https://cdn.jsdmirror.com/gh/Qalxry/csdn2md/plugins/katex.min.js#sha256-5r/l3uvUx8zScgVbq2O9OrLHO5B7bmoi01J0CoE4H9Q=
// ==/UserScript==
(function () {
"use strict";
// 需要加载的库及其备用源
const libsToLoad = {
JSZip: {
isLoaded: () => typeof JSZip !== "undefined",
urls: [
"https://cdn.jsdelivr.net/gh/Qalxry/csdn2md/plugins/jszip.min.js#sha256-yeSlK6wYruTz+Q0F+8pgP1sPW/HOjEXmC7TtOiyy7YY=",
"https://cdn.jsdmirror.com/gh/Qalxry/csdn2md/plugins/jszip.min.js#sha256-yeSlK6wYruTz+Q0F+8pgP1sPW/HOjEXmC7TtOiyy7YY=",
"https://cdnjs.webstatic.cn/ajax/libs/jszip/3.7.1/jszip.min.js#sha256-yeSlK6wYruTz+Q0F+8pgP1sPW/HOjEXmC7TtOiyy7YY=",
"https://mirrors.sustech.edu.cn/cdnjs/ajax/libs/jszip/3.7.1/jszip.min.js#sha256-yeSlK6wYruTz+Q0F+8pgP1sPW/HOjEXmC7TtOiyy7YY=",
"https://use.sevencdn.com/ajax/libs/jszip/3.7.1/jszip.min.js#sha256-yeSlK6wYruTz+Q0F+8pgP1sPW/HOjEXmC7TtOiyy7YY=",
"https://cdn.jsdmirror.com/ajax/libs/jszip/3.7.1/jszip.min.js#sha256-yeSlK6wYruTz+Q0F+8pgP1sPW/HOjEXmC7TtOiyy7YY=",
"https://cdnjs.cloudflare.com/ajax/libs/jszip/3.7.1/jszip.min.js#sha256-yeSlK6wYruTz+Q0F+8pgP1sPW/HOjEXmC7TtOiyy7YY=",
],
},
fflate: {
isLoaded: () => typeof fflate !== "undefined",
urls: [
"https://cdn.jsdelivr.net/gh/Qalxry/csdn2md/plugins/fflate.min.js#sha256-w7NPLp9edNTX1k4BysegwBlUxsQGQU1CGFx7U9aHXd8=",
"https://cdn.jsdmirror.com/gh/Qalxry/csdn2md/plugins/fflate.min.js#sha256-w7NPLp9edNTX1k4BysegwBlUxsQGQU1CGFx7U9aHXd8=",
"https://npm.webcache.cn/fflate@0.8.2/umd/index.js#sha256-w7NPLp9edNTX1k4BysegwBlUxsQGQU1CGFx7U9aHXd8=",
"https://use.sevencdn.com/npm/fflate@0.8.2/umd/index.js#sha256-w7NPLp9edNTX1k4BysegwBlUxsQGQU1CGFx7U9aHXd8=",
"https://cdn.jsdmirror.com/npm/fflate@0.8.2/umd/index.js#sha256-w7NPLp9edNTX1k4BysegwBlUxsQGQU1CGFx7U9aHXd8=",
"https://unpkg.com/fflate@0.8.2/umd/index.js#sha256-w7NPLp9edNTX1k4BysegwBlUxsQGQU1CGFx7U9aHXd8=",
"https://cdn.jsdelivr.net/npm/fflate@0.8.2/umd/index.js#sha256-w7NPLp9edNTX1k4BysegwBlUxsQGQU1CGFx7U9aHXd8=",
],
},
streamSaver: {
isLoaded: () => typeof streamSaver !== "undefined",
urls: [
"https://cdn.jsdelivr.net/gh/Qalxry/csdn2md/plugins/streamSaver.min.js#sha256-VxQm++CYEdHipBjKWh4QQHHOYZmyo8F/7dJQxG11xFM=",
"https://cdn.jsdmirror.com/gh/Qalxry/csdn2md/plugins/streamSaver.min.js#sha256-VxQm++CYEdHipBjKWh4QQHHOYZmyo8F/7dJQxG11xFM=",
"https://use.sevencdn.com/npm/streamsaver@2.0.6/StreamSaver.min.js#sha256-VxQm++CYEdHipBjKWh4QQHHOYZmyo8F/7dJQxG11xFM=",
"https://cdn.jsdelivr.net/npm/streamsaver@2.0.6/StreamSaver.min.js#sha256-VxQm++CYEdHipBjKWh4QQHHOYZmyo8F/7dJQxG11xFM=",
"https://cdn.jsdmirror.com/npm/streamsaver@2.0.6/StreamSaver.min.js#sha256-VxQm++CYEdHipBjKWh4QQHHOYZmyo8F/7dJQxG11xFM=",
],
},
katex: {
isLoaded: () => typeof katex !== "undefined",
urls: [
"https://cdn.jsdelivr.net/gh/Qalxry/csdn2md/plugins/katex.min.js#sha256-5r/l3uvUx8zScgVbq2O9OrLHO5B7bmoi01J0CoE4H9Q=",
"https://cdn.jsdmirror.com/gh/Qalxry/csdn2md/plugins/katex.min.js#sha256-5r/l3uvUx8zScgVbq2O9OrLHO5B7bmoi01J0CoE4H9Q=",
"https://use.sevencdn.com/npm/katex@0.16.11/dist/katex.min.js#sha256-5r/l3uvUx8zScgVbq2O9OrLHO5B7bmoi01J0CoE4H9Q=",
"https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/katex.min.js#sha256-5r/l3uvUx8zScgVbq2O9OrLHO5B7bmoi01J0CoE4H9Q=",
"https://cdn.jsdmirror.com/npm/katex@0.16.11/dist/katex.min.js#sha256-5r/l3uvUx8zScgVbq2O9OrLHO5B7bmoi01J0CoE4H9Q=",
],
},
};
// 动态插入脚本
function loadScript(src) {
return new Promise((resolve, reject) => {
const s = document.createElement("script");
let hash = src.match(/#(.*)$/)?.[1];
s.src = src;
if (hash) {
s.setAttribute("integrity", hash);
s.setAttribute("crossorigin", "anonymous");
}
s.onload = () => {
resolve();
};
s.onerror = () => reject(new Error(`Failed to load ${src.slice(0, 100)}`));
document.head.appendChild(s);
});
}
// 如果全局对象不存在,就按顺序尝试加载备用源
(async () => {
for (const [libName, libData] of Object.entries(libsToLoad)) {
if (!libData.isLoaded()) {
console.warn(`${libName} not found, loading from additional sources...`);
for (const url of libData.urls) {
try {
await loadScript(url);
// 检查是否加载成功
if (!libData.isLoaded()) {
throw new Error(`not loaded after script injection`);
}
console.info(`${libName} loaded successfully from ${url}`);
break;
} catch (e) {
console.error(`Failed to load ${libName} from ${url}:`, e);
}
}
} else {
console.info(`${libName} is already loaded.`);
}
}
})();
/**
* 模块: 工具函数
* 提供各种辅助功能的工具函数集合
*/
const Utils = {
/**
* 清除字符串中的特殊字符
* @param {string} str - 输入字符串
* @returns {string} 清理后的字符串
*/
clearSpecialChars(str) {
return str
.replaceAll(/[\s]{2,}/g, "")
.replaceAll(
/[\u200B-\u200F\u202A-\u202E\u2060-\u206F\uFEFF\u00AD\u034F\u061C\u180E\u2800\u3164\uFFA0\uFFF9-\uFFFB]/g,
""
)
.replaceAll("⎧", "")
.replaceAll("⎨", "{")
.replaceAll("⎩", "")
.replaceAll("⎫", "")
.replaceAll("⎬", "}")
.replaceAll("⎭", "")
.replaceAll("⎡", "[")
.replaceAll("⎢", "")
.replaceAll("⎣", "")
.replaceAll("⎤", "]")
.replaceAll("⎥", "")
.replaceAll("⎦", "")
// 清除 \uXXXX 这样的 ascii 表示的 unicode 符号
.replaceAll(/\\u[0-9a-fA-F]{4}/g, "");
},
/**
* 从 KaTeX 元素中提取 LaTeX 源码
* @param {HTMLElement} katexMathmlElem - .katex-mathml 元素
* @param {HTMLElement} katexHtmlElem - .katex-html 元素
* @param {boolean} isDisplay - 是否为行间公式
* @returns {string} 提取的 LaTeX 源码
*/
extractKatexLatex(katexMathmlElem, katexHtmlElem, isDisplay = false) {
const textContent = katexMathmlElem.textContent || "";
const mathml = Utils.clearSpecialChars(textContent);
const katexHtml = Utils.clearSpecialChars(katexHtmlElem.textContent);
// 策略1:比较 MathML 和 KaTeX HTML 的文本内容
// 若 mathml 以 katexHtml 开头,则直接去除前缀
if (mathml.startsWith(katexHtml)) {
return mathml.replace(katexHtml, "");
}
// 策略2:
// 先根据空白字符和换行符分割,取最长的那一段(通常是正文内容),如果分割后有多段且最长的那段明显长于其他段,则认为策略生效
const strSplit = textContent.split(/(?=.*\n)(?=.* )[\s\n]{10,}/);
let maxLen = 0;
let maxStr = "";
for (const item of strSplit) {
if (item.length > maxLen) {
maxLen = item.length;
maxStr = item;
}
}
// 检查策略是否生效
if (strSplit.length > 1) {
return maxStr;
}
// // 策略3:
// // 首先用 innerText 截获 可读文本 (先根据空白字符分割,取第一个非空项)
// // 然后将 textContent 中的 可读文本 去除即可
// const rubbish = katexMathmlElem.innerText.split(/\s+/).find(item => item.trim().length > 0) || "";
// const cleanedText = textContent.replace(rubbish, "").trim();
// if (rubbish.length > 0 && cleanedText.length > 0) {
// return cleanedText;
// }
// 策略4:
// 仅对复杂公式生效,简单公式应当被前面的策略处理掉
// 基于 KaTeX 渲染器进行验证,搜索切分点
if (typeof katex !== "undefined") {
// 启发式:找到第一个 LaTeX 结构字符作为搜索起点
const specialIdx = textContent.search(/[\\_{}\^]/);
if (specialIdx > 0) {
for (let i = specialIdx; i >= 0; i--) {
const candidate = textContent.substring(i);
try {
const rendered = katex.renderToString(candidate, {
displayMode: isDisplay,
throwOnError: true,
output: "mathml",
});
// 提取 MathML 可读文本(排除 annotation)
const temp = document.createElement("div");
temp.innerHTML = rendered;
const annotation = temp.querySelector("annotation");
if (annotation) annotation.remove();
const generatedPrefix = Utils.clearSpecialChars(temp.textContent);
const candidatePrefix = Utils.clearSpecialChars(textContent.substring(0, i));
if (generatedPrefix === candidatePrefix) {
return candidate;
}
} catch (e) {
continue;
}
}
}
}
return textContent; // 如果以上策略都不生效,则返回原始文本
},
/**
* 清理URL中的参数和锚点
* @param {string} url - 输入URL
* @returns {string} 清理后的URL
*/
clearUrl(url) {
return url.replaceAll(/[?#@!$&'()*+,;=].*$/g, "");
},
/**
* 将文件名转换为安全的文件名
* @param {string} filename - 原始文件名
* @returns {string} 安全的文件名
*/
safeFilename(filename) {
return filename.replaceAll(/[\\/:*?"<>|]/g, "_");
},
/**
* 压缩HTML内容,移除多余的空白和换行符
* @param {string} html - 输入的HTML字符串
* @returns {string} 压缩后的HTML字符串
*/
shrinkHtml(html) {
return html
.replaceAll(/>\s+</g, "><") // 去除标签之间的空白
.replaceAll(/\s{2,}/g, " ") // 多个空格压缩成一个
.replaceAll(/^\s+|\s+$/g, ""); // 去除首尾空白
},
/**
* 将SVG图片转换为Base64编码的字符串
* @param {string} svgText - SVG图片的文本内容
* @returns {string} Base64编码的字符串
*/
svgToBase64(svgText) {
const uint8Array = new TextEncoder().encode(svgText);
const binaryString = uint8Array.reduce((data, byte) => data + String.fromCharCode(byte), "");
return btoa(binaryString);
},
formatSeconds(seconds) {
const hrs = Math.floor(seconds / 3600);
const mins = Math.floor((seconds % 3600) / 60);
const secs = Math.floor(seconds % 60);
const pad = (num) => num.toString().padStart(2, "0");
return `${pad(hrs)}:${pad(mins)}:${pad(secs)}`;
},
async parallelPool(array, iteratorFn, poolLimit = 10) {
const ret = []; // 存储所有任务
const executing = []; // 存储正在执行的任务
let index = 0;
for (const item of array) {
const currentIndex = index++;
const p = new Promise(async (resolve, reject) => {
try {
await iteratorFn(item, currentIndex);
resolve();
} catch (error) {
reject(error);
}
});
ret.push(p);
if (poolLimit <= array.length) {
const e = p.finally(() => executing.splice(executing.indexOf(e), 1));
executing.push(e);
if (executing.length >= poolLimit) {
await Promise.race(executing);
}
}
}
return Promise.all(ret);
},
/**
* 计算字符串的简单哈希值
* @param {string} str - 输入字符串
* @param {number} length - 返回的16进制字符串长度,默认为8
* @returns {string} 指定长度的16进制哈希字符串
*/
simpleHash(str, length = 8) {
let hash = 0;
if (str.length === 0) return "0".repeat(length);
for (let i = 0; i < str.length; i++) {
const char = str.charCodeAt(i);
hash = (hash << 5) - hash + char;
hash = hash & hash; // 转换为32位整数
}
// 转换为16进制并确保为正数
let hexHash = Math.abs(hash).toString(16);
// 如果长度不够,重复哈希直到达到要求长度
while (hexHash.length < length) {
hash = (hash << 5) - hash + hash;
hash = hash & hash;
hexHash += Math.abs(hash).toString(16);
}
// 截取到指定长度
return hexHash.substring(0, length);
},
formatFileSize(bytes) {
if (bytes === 0) return "0 Bytes";
const k = 1024;
const sizes = ["Bytes", "KB", "MB", "GB"];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + " " + sizes[i];
},
// ---------- 新增:把 SVG 序列化字符串里的 <br> 规范化为 XML 自闭合形式 <br/> ----------
// 说明:采用字符串替换是最简单可靠的方式,能够处理 <br>, <br >, <br/>, <br />, 大小写等情况。
// 只针对要输出的 SVG 字符串进行替换(不改变原 DOM 对象)。
normalizeBrToSelfClosing(svgString) {
// 把所有 <br ...> / <br> / <br/> / <br /> 都规范成 <br/>
// \b 确保是单词边界(匹配 br),[^>]* 捕获可能的空格或属性(通常没有属性)
// 最简单、兼容的写法:
return svgString.replace(/<br\s*\/?>/gi, '<br/>');
}
// ------------------------------------------------------------------------------------
};
/**
* 模块: 锁管理
* 处理异步操作锁
*/
class ReentrantAsyncLock {
/**
* 创建一个可重入异步锁
* @param {boolean} enableReentrant - 是否启用重入功能
*/
constructor(enableReentrant = true) {
this.queue = [];
this.locked = false;
this.owner = null; // 记录锁的持有者,用于重入
this.enableReentrant = enableReentrant;
}
/**
* 获取锁
* @param {any} ownerId - 锁持有者的标识
* @returns {Promise<void>}
*/
async acquire(ownerId = null) {
if (this.locked) {
// 如果允许重入,且当前持有者是ownerId,则直接返回
if (this.enableReentrant && this.owner === ownerId) {
return;
}
// 否则加入队列等待
await new Promise((resolve) => this.queue.push(resolve));
}
this.locked = true;
this.owner = ownerId;
}
/**
* 释放锁
* @param {any} ownerId - 锁持有者的标识
*/
release(ownerId) {
if (this.enableReentrant && this.owner !== ownerId) {
throw new Error("Cannot release a lock you do not own");
}
this.locked = false;
this.owner = null;
if (this.queue.length > 0) {
const resolve = this.queue.shift();
resolve();
this.locked = true;
this.owner = ownerId; // 继续持有锁
}
}
}
/**
* 模块: UI管理
* 处理界面相关的功能,支持多种输入类型和分组
*/
class UIManager {
/**
* 创建UI管理器
**/
constructor() {
/** @type {ConfigManager} */
this.configManager = new ConfigManager(); // 配置管理
/** @type {ArticleDownloader} */
this.downloadManager = new ArticleDownloader(); // 下载管理器
this.downloadManager.setUIManager(this); // 设置UI与下载管理器的双向引用
this.isDragging = 0;
this.offsetX = 0;
this.offsetY = 0;
this.container = null;
this.contentBox = null;
this.mainButton = null;
this.floatWindow = null;
this.downloadButton = null;
this.gotoRepoButton = null;
this.isOpen = false;
this.repo_url = "https://github.com/Qalxry/csdn2md";
this.updateCheckInterval = 24 * 60 * 60 * 1000;
// 初始化
this.initStyles();
this.initUI();
this.setupEventListeners();
this.dialogQueue = [];
this.isDialogActive = false;
this.updateAllOptions();
}
/**
* 初始化UI样式
*/
initStyles() {
GM_addStyle(`
:root {
--tm_ui-linear-gradient: linear-gradient(135deg, #12c2e9 0%, #c471ed 50%, #f64f59 100%);
}
.tm_floating-container {
position: fixed;
bottom: 20px;
right: 20px;
z-index: 9999;
transform-origin: bottom right;
font-size: 13px;
}
.tm_main-button {
width: 50px;
height: 50px;
border-radius: 50%;
background: var(--tm_ui-linear-gradient);
box-shadow: 0 0 20px rgba(0,0,0,0.2);
border: none;
color: white;
font-size: 20px;
cursor: pointer;
transition: all 0.3s ease;
display: flex;
align-items: center;
justify-content: center;
}
.tm_content-box {
background: linear-gradient(45deg, #ffffff, #f8f9fa);
border-radius: 13px;
padding: 13px;
width: 360px;
box-shadow: 0 7px 20px rgba(0,0,0,0.15);
margin-bottom: 13px;
opacity: 0;
transform: scale(0);
transform-origin: bottom right;
transition: all 0.4s cubic-bezier(0.68, -0.55, 0.265, 1.55);
position: absolute;
bottom: 100%;
right: 0;
}
.tm_content-box.open {
opacity: 1;
transform: scale(1);
}
.tm_complex-content {
display: flex;
flex-direction: column;
gap: 10px;
}
#myFloatWindow {
width: 100%;
position: relative;
}
.tm_ui-options-container {
max-height: 480px;
overflow-y: auto;
padding-right: 5px;
margin: 10px 0px 2px 0px;
scrollbar-width: thin;
scrollbar-color: rgba(0,0,0,0.3) transparent;
position: relative;
}
.tm_ui-options-disabled-overlay {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(240,240,240,0.7);
z-index: 10;
display: none;
border-radius: 8px;
}
.tm_ui-options-container::-webkit-scrollbar {
width: 4px;
}
.tm_ui-options-container::-webkit-scrollbar-thumb {
background-color: rgba(0,0,0,0.3);
border-radius: 2px;
}
.tm_ui-option-group {
border: 1px solid rgba(0,0,0,0.08);
border-radius: 6px;
padding: 0;
margin-bottom: 7px;
background: #ffffff;
box-shadow: 0 1px 2px rgba(0,0,0,0.05);
overflow: hidden;
transition: all 0.3s ease;
}
.tm_ui-option-group-header {
padding: 7px 8px;
cursor: pointer;
background: rgba(0,0,0,0.02);
border-bottom: 1px solid rgba(0,0,0,0.08);
display: flex;
align-items: center;
justify-content: space-between;
user-select: none;
transition: background 0.2s ease;
}
.tm_ui-option-group-header:hover {
background: rgba(0, 123, 255, 0.05);
}
.tm_ui-option-group-title {
font-weight: 800;
color: #444;
flex: 1;
font-size: 12px;
}
.tm_ui-option-group-content {
padding: 8px;
overflow: hidden;
max-height: 0px; /* 设置足够大的展开高度 */
transition: max-height 0.3s cubic-bezier(0.33, 1, 0.68, 1), padding 0.3s ease, opacity 0.3s ease;
opacity: 1;
}
.tm_ui-option-group-collapsed .tm_ui-option-group-content {
max-height: 0px;
padding-top: 0;
padding-bottom: 0;
opacity: 0;
}
.tm_ui-option-item {
margin-bottom: 8px;
display: flex;
align-items: center;
flex-wrap: wrap;
}
.tm_ui-option-item:last-child {
margin-bottom: 0;
}
.tm_ui-option-label {
margin-left: 5px;
flex: 1;
min-width: 80px;
font-size: 12px;
}
.tm_ui-input-container {
display: flex;
align-items: center;
flex: 1;
max-width: 100px;
}
.tm_ui-tooltip {
position: relative;
display: inline-flex;
margin-left: 6px;
cursor: help;
}
.tm_ui-tooltip-icon {
display: inline-flex;
align-items: center;
justify-content: center;
width: 12px;
height: 12px;
border-radius: 50%;
background-color: rgba(0,0,0,0.2);
color: white;
font-size: 9px;
font-weight: bold;
user-select: none; /* 设置不可选择文本 */
}
.tm_ui-tooltip-text {
visibility: hidden;
background-color: rgba(0,0,0,0.7);
color: #fff;
text-align: left;
border-radius: 4px;
padding: 5px 7px;
position: absolute;
z-index: 10000;
font-size: 11px;
opacity: 0;
transition: opacity 0.3s;
line-height: 1.4;
pointer-events: none;
box-shadow: 0 2px 8px rgba(0,0,0,0.2);
top: -5px;
right: 110%;
white-space: nowrap;
overflow-wrap: break-word;
}
.tm_ui-tooltip:hover .tm_ui-tooltip-text {
visibility: visible;
opacity: 1;
}
.tm_ui-switch {
--tm_ui-width: 28px;
--tm_ui-height: 14px;
--tm_ui-padding: 2px;
--tm_ui-duration: 0.2s;
--tm_ui-color-on:rgb(76, 97, 175);
--tm_ui-color-off: #e0e0e0;
--tm_ui-color-knob: #ffffff;
--tm_ui-shadow: 0 2px 5px rgba(0,0,0,0.2);
--tm_ui-knob-size: calc(var(--tm_ui-height) - var(--tm_ui-padding) * 2);
display: inline-block;
position: relative;
width: var(--tm_ui-width);
height: var(--tm_ui-height);
}
.tm_ui-switch input {
opacity: 0;
width: 0;
height: 0;
}
.tm_ui-switch-slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: var(--tm_ui-color-off);
transition: background-color var(--tm_ui-duration) ease;
border-radius: var(--tm_ui-height);
box-shadow: var(--tm_ui-shadow) inset;
}
.tm_ui-switch-slider:before {
position: absolute;
content: "";
height: var(--tm_ui-knob-size);
width: var(--tm_ui-knob-size);
left: var(--tm_ui-padding);
bottom: var(--tm_ui-padding);
background-color: var(--tm_ui-color-knob);
transition: transform var(--tm_ui-duration) ease;
border-radius: 50%;
box-shadow: var(--tm_ui-shadow);
}
.tm_ui-switch input:checked + .tm_ui-switch-slider {
background-color: var(--tm_ui-color-on);
}
.tm_ui-switch input:checked + .tm_ui-switch-slider:before {
transform: translateX(calc(var(--tm_ui-width) - var(--tm_ui-height)));
}
.tm_ui-switch input:disabled + .tm_ui-switch-slider {
opacity: 0.6;
cursor: not-allowed;
}
.tm_ui-range-container {
display: flex;
align-items: center;
width: 100%;
max-width: 120px;
gap: 5px;
}
.tm_ui-range-input {
flex: 1;
width: 90px;
}
.tm_ui-range-value {
width: 25px;
text-align: center;
border: 1px solid #ccc;
border-radius: 2px;
padding: 1px;
font-size: 10px;
}
.tm_ui-select {
padding: 3px;
border-radius: 3px;
border: 1px solid #ccc;
background-color: white;
width: 100%;
font-size: 11px;
}
.tm_ui-input-number, .tm_ui-input-text {
padding: 3px;
border-radius: 3px;
border: 1px solid #ccc;
width: 100%;
font-size: 11px;
}
.tm_ui-buttons-container {
display: flex;
gap: 7px;
justify-content: center;
margin-top: 3px;
}
.tm_footer-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
margin-top: 4px;
margin-left: 2px;
margin-bottom: -8px;
}
.tm_version-badge {
text-align: left;
color: #888;
font-size: 10px;
user-select: none;
}
.tm_footer-link {
display: inline-flex;
align-items: center;
gap: 4px;
font-size: 10px;
white-space: nowrap;
text-decoration: none;
}
.tm_footer-link:hover {
text-decoration: underline !important;
}
.tm_footer-link svg {
width: 12px;
height: 12px;
}
#myDownloadButton, #myResetButton {
text-align: center;
padding: 10px 10px;
background: var(--tm_ui-linear-gradient);
color: white;
cursor: pointer;
transition: all 0.3s ease;
border-radius: 3px;
border: none;
font-size: 11px;
}
#myDownloadButton {
width: 100%;
margin-bottom: 3px;
padding: 8px;
}
#myResetButton {
flex: 1;
}
#myGotoRepoButton {
flex: 3;
}
.collapse-icon {
width: 12px;
height: 12px;
transition: transform 0.3s ease;
display: flex;
align-items: center;
justify-content: center;
}
.tm_ui-option-group-collapsed .collapse-icon {
transform: rotate(180deg);
}
#myDownloadButton:hover, #myResetButton:hover, #myGotoRepoButton:hover {
transform: scale(1.02);
box-shadow: 0 1px 5px rgba(0,0,0,0.15);
}
#myDownloadButton:disabled, #myResetButton:disabled {
background: gray;
color: #aaa;
cursor: not-allowed;
transform: none;
box-shadow: none;
}
#myGotoRepoButton {
background: #000000;
color: #ffffff;
text-align: center;
padding: 10px 10px;
cursor: pointer;
transition: all 0.3s ease;
border-radius: 3px;
border: none;
font-size: 11px;
}
#myGotoRepoButton:active {
transform: scale(1);
}
#myGotoRepoButton:hover .goto-repo-btn-icon {
fill: #ffff00;
transform: scale(1.02);
rotate: 360deg;
filter: drop-shadow(0 0 5px rgba(255, 208, 0, 0.8))
drop-shadow(0 0 10px rgba(255, 208, 0, 0.6));
}
#myGotoRepoButton:hover .goto-repo-btn-text {
filter: drop-shadow(0 0 5px rgba(255, 255, 255, 0.2))
drop-shadow(0 0 10px rgba(255, 255, 255, 0.4));
}
#myGotoRepoButton .goto-repo-btn-text {
transition: all 1s ease;
}
#myGotoRepoButton .goto-repo-btn-icon {
display: inline-block;
width: 12px;
height: 12px;
transition: all 1s ease;
}
`);
}
/**
* 初始化UI元素
*/
initUI() {
// 创建悬浮容器
this.container = document.createElement("div");
this.container.className = "tm_floating-container";
this.container.id = "draggable";
// 创建主按钮
this.mainButton = document.createElement("button");
this.mainButton.className = "tm_main-button";
this.mainButton.innerHTML = `<svg xmlns="http://www.w3.org/2000/svg" height="20px" viewBox="0 -960 960 960" width="20px" fill="#FFFFFF"><path d="M480-337q-8 0-15-2.5t-13-8.5L308-492q-12-12-11.5-28t11.5-28q12-12 28.5-12.5T365-549l75 75v-286q0-17 11.5-28.5T480-800q17 0 28.5 11.5T520-760v286l75-75q12-12 28.5-11.5T652-548q11 12 11.5 28T652-492L508-348q-6 6-13 8.5t-15 2.5ZM240-160q-33 0-56.5-23.5T160-240v-80q0-17 11.5-28.5T200-360q17 0 28.5 11.5T240-320v80h480v-80q0-17 11.5-28.5T760-360q17 0 28.5 11.5T800-320v80q0 33-23.5 56.5T720-160H240Z"/></svg>`;
// 创建内容区域
this.contentBox = document.createElement("div");
this.contentBox.className = "tm_content-box";
// 创建复杂内容
this.contentBox.innerHTML = `
<div class="tm_complex-content" id="tmComplexContent"></div>
`;
// 组装元素
this.container.appendChild(this.contentBox);
this.container.appendChild(this.mainButton);
document.body.appendChild(this.container);
// 还原之前保存的位置
const savedTop = GM_getValue("draggableTop");
if (savedTop) {
this.container.style.top = Math.min(window.innerHeight - 50, parseInt(savedTop)) + "px";
}
// 创建浮动窗口
this.createFloatWindow();
}
/**
* 创建浮动窗口和选项
*/
createFloatWindow() {
// 创建悬浮窗
this.floatWindow = document.createElement("div");
this.floatWindow.style.display = "flex";
this.floatWindow.style.flexDirection = "column";
this.floatWindow.id = "myFloatWindow";
// 创建下载按钮
this.downloadButton = document.createElement("button");
this.downloadButton.innerHTML = "点击下载Markdown<br>(支持文章、专栏、用户全部文章页面)";
this.downloadButton.id = "myDownloadButton";
this.floatWindow.appendChild(this.downloadButton);
// 创建选项容器,设置为可滚动
const optionContainer = document.createElement("div");
optionContainer.className = "tm_ui-options-container";
this.floatWindow.appendChild(optionContainer);
// 创建选项容器禁用遮罩
const overlay = document.createElement("div");
overlay.className = "tm_ui-options-disabled-overlay";
optionContainer.appendChild(overlay);
// 添加选项分组
this.createOptionGroups(optionContainer);
// 创建底部按钮容器
const buttonsContainer = document.createElement("div");
buttonsContainer.className = "tm_ui-buttons-container";
this.floatWindow.appendChild(buttonsContainer);
// 创建恢复默认设置按钮
this.resetButton = document.createElement("button");
this.resetButton.innerHTML = "恢复默认";
this.resetButton.id = "myResetButton";
buttonsContainer.appendChild(this.resetButton);
// 创建去GitHub按钮
this.gotoRepoButton = document.createElement("button");
// this.gotoRepoButton.innerHTML = "前往 GitHub 给作者点个 Star ⭐";
this.gotoRepoButton.innerHTML = `
<span class="goto-repo-btn-text">前往 GitHub 给作者点个 Star</span>
<svg aria-hidden="true" fill="currentColor" viewBox="0 0 47.94 47.94" xmlns="http://www.w3.org/2000/svg"
width="12px" height="12px" class="goto-repo-btn-icon">
<path
d="M26.285,2.486l5.407,10.956c0.376,0.762,1.103,1.29,1.944,1.412l12.091,1.757
c2.118,0.308,2.963,2.91,1.431,4.403l-8.749,8.528c-0.608,0.593-0.886,1.448-0.742,2.285l2.065,12.042
c0.362,2.109-1.852,3.717-3.746,2.722l-10.814-5.685c-0.752-0.395-1.651-0.395-2.403,0l-10.814,5.685
c-1.894,0.996-4.108-0.613-3.746-2.722l2.065-12.042c0.144-0.837-0.134-1.692-0.742-2.285l-8.749-8.528
c-1.532-1.494-0.687-4.096,1.431-4.403l12.091-1.757c0.841-0.122,1.568-0.65,1.944-1.412l5.407-10.956
C22.602,0.567,25.338,0.567,26.285,2.486z"
></path>
</svg>
`;
this.gotoRepoButton.id = "myGotoRepoButton";
buttonsContainer.appendChild(this.gotoRepoButton);
// 底部版本号 + Greasy Fork 链接
this.footerRow = document.createElement("div");
this.footerRow.className = "tm_footer-row";
this.versionBadge = document.createElement("div");
this.versionBadge.className = "tm_version-badge";
this.versionBadge.textContent = `版本 ${GM_info.script.version}`;
this.footerRow.appendChild(this.versionBadge);
this.greasyForkLink = document.createElement("a");
this.greasyForkLink.className = "tm_footer-link";
this.greasyForkLink.href = "https://greasyfork.org/en/scripts/523540-csdn2md-%E6%89%B9%E9%87%8F%E4%B8%8B%E8%BD%BDcsdn%E6%96%87%E7%AB%A0%E4%B8%BAmarkdown";
this.greasyForkLink.target = "_blank";
this.greasyForkLink.rel = "noopener noreferrer";
this.greasyForkLink.innerHTML = `
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
<path fill="currentColor" d="M5.89 2.227a.28.28 0 0 1 .266.076l5.063 5.062c.54.54.509 1.652-.031 2.192l8.771 8.77c1.356 1.355-.36 3.097-1.73 1.728l-8.772-8.77c-.54.54-1.651.571-2.191.031l-5.063-5.06c-.304-.304.304-.911.608-.608l3.714 3.713L7.59 8.297L3.875 4.582c-.304-.304.304-.911.607-.607l3.715 3.714l1.067-1.066L5.549 2.91c-.228-.228.057-.626.342-.683ZM12 0C5.374 0 0 5.375 0 12s5.374 12 12 12c6.625 0 12-5.375 12-12S18.625 0 12 0" />
</svg>
<span>在 Greasy Fork 查看/更新</span>
`;
this.greasyForkLink.style.display = "none";