forked from Mapaler/EhTagTranslator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EhTagBuilder.user.js
1482 lines (1397 loc) · 48.7 KB
/
EhTagBuilder.user.js
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 EhTagBuilder
// @name:zh-CN E绅士标签构建者
// @name:zh-TW E紳士標籤構建者
// @name:zh-HK E紳士標籤構建者
// @namespace http://www.mapaler.com/
// @homepage https://github.com/Mapaler/EhTagTranslator
// @supportURL https://github.com/Mapaler/EhTagTranslator/issues
// @description Build EhTagTranslater from Wiki.
// @description:zh-CN 从Wiki获取EhTagTranslater数据库,将E绅士TAG翻译为中文
// @description:zh-TW 從Wiki獲取EhTagTranslater資料庫,將E紳士TAG翻譯為中文
// @description:zh-HK 從Wiki獲取EhTagTranslater資料庫,將E紳士TAG翻譯為中文
// @include *://github.com/Mapaler/EhTagTranslator*
// @include *://github.com/EhTagTranslation/Database*
// @icon http://exhentai.org/favicon.ico
// @version 2.9.0
// @grant none
// @author Mapaler <mapaler@163.com>
// @copyright 2017+, Mapaler <mapaler@163.com>
//-@grant GM_xmlhttpRequest
//-@grant GM_getValue
//-@grant GM_setValue
//-@grant GM_deleteValue
//-@grant GM_listValues
// ==/UserScript==
(function() {
var newRe = location.pathname.indexOf("EhTagTranslation/Database")>=0; //是否已经迁移
var wiki_URL= newRe ? //GitHub wiki 的地址
"https://github.com/EhTagTranslation/Database/tree/master/" : //新的组织项目地址
"https://github.com/Mapaler/EhTagTranslator/wiki"; //传统Wiki地址
var wiki_version_filename = "version"; //版本的地址
var rows_filename = newRe?"database/rows.md":"rows"; //行名的地址
var buttonInserPlace = document.querySelector(".pagehead-actions"); //按钮插入位置
var windowInserPlace = document.querySelector(".reponav"); //窗口插入位置
var scriptVersion = "unknown"; //本程序的版本
var scriptName = "EhTagBuilder"; //本程序的名称
if (typeof(GM_info)!="undefined")
{
scriptVersion = GM_info.script.version.replace(/(^\s*)|(\s*$)/g, "");
if (GM_info.script.name_i18n)
{
var i18n = (navigator.language||navigator.userLanguage).replace("-","_"); //获取浏览器语言
scriptName = GM_info.script.name_i18n[i18n]; //支持Tampermonkey
}
else
{
scriptName = GM_info.script.localizedName || //支持Greasemonkey 油猴子 3.x
GM_info.script.name; //支持Violentmonkey 暴力猴
}
}
var optionVersion = 1; //当前设置版本,用于提醒是否需要重置设置
var database_structure_version = newRe?5:4; //当前数据库结构版本,用于提醒是否需要更新脚本
var downOverCheckHook; //检测下载是否完成的循环函数
var rowsCount = 0; //行名总数
var rowsCurrent = 0; //当前下载行名
//匹配Emoji正则表达式,Made by @xioxin
var emojireg = /\u{2139}|[\u{2194}-\u{2199}]|[\u{21A9}-\u{21AA}]|[\u{231A}-\u{231B}]|\u{2328}|\u{23CF}|[\u{23E9}-\u{23F3}]|[\u{23F8}-\u{23FA}]|\u{24C2}|[\u{25AA}-\u{25AB}]|\u{25B6}|\u{25C0}|[\u{25FB}-\u{25FE}]|[\u{2600}-\u{2604}]|\u{260E}|\u{2611}|[\u{2614}-\u{2615}]|\u{2618}|\u{261D}|\u{2620}|[\u{2622}-\u{2623}]|\u{2626}|\u{262A}|[\u{262E}-\u{262F}]|[\u{2638}-\u{263A}]|[\u{2648}-\u{2653}]|\u{2660}|\u{2663}|\u{2666}|\u{2668}|\u{267B}|\u{267F}|[\u{2692}-\u{2697}]|\u{2699}|[\u{269B}-\u{269C}]|[\u{26A0}-\u{26A1}]|[\u{26AA}-\u{26AB}]|[\u{26B0}-\u{26B1}]|[\u{26BD}-\u{26BE}]|[\u{26C4}-\u{26C5}]|\u{26C8}|\u{26CE}|\u{26CF}|\u{26D1}|[\u{26D3}-\u{26D4}]|[\u{26E9}-\u{26EA}]|[\u{26F0}-\u{26F5}]|[\u{26F7}-\u{26FA}]|\u{26FD}|\u{2702}|\u{2705}|[\u{2708}-\u{2709}]|[\u{270A}-\u{270B}]|[\u{270C}-\u{270D}]|\u{270F}|\u{2712}|\u{2714}|\u{2716}|\u{271D}|\u{2721}|\u{2728}|[\u{2733}-\u{2734}]|\u{2744}|\u{2747}|\u{274C}|\u{274E}|[\u{2753}-\u{2755}]|\u{2757}|\u{2763}|[\u{2795}-\u{2797}]|\u{27A1}|\u{27B0}|\u{27BF}|[\u{2934}-\u{2935}]|[\u{2B05}-\u{2B07}]|[\u{2B1B}-\u{2B1C}]|\u{2B50}|\u{2B55}|\u{3030}|\u{303D}|\u{3297}|\u{3299}|\u{1F004}|\u{1F0CF}|[\u{1F170}-\u{1F171}]|\u{1F17E}|\u{1F17F}|\u{1F18E}|[\u{1F191}-\u{1F19A}]|[\u{1F1E6}-\u{1F1FF}]|[\u{1F201}-\u{1F202}]|\u{1F21A}|\u{1F22F}|[\u{1F232}-\u{1F23A}]|[\u{1F250}-\u{1F251}]|[\u{1F300}-\u{1F320}]|\u{1F321}|[\u{1F324}-\u{1F32C}]|[\u{1F32D}-\u{1F32F}]|[\u{1F330}-\u{1F335}]|\u{1F336}|[\u{1F337}-\u{1F37C}]|\u{1F37D}|[\u{1F37E}-\u{1F37F}]|[\u{1F380}-\u{1F393}]|[\u{1F396}-\u{1F397}]|[\u{1F399}-\u{1F39B}]|[\u{1F39E}-\u{1F39F}]|[\u{1F3A0}-\u{1F3C4}]|\u{1F3C5}|[\u{1F3C6}-\u{1F3CA}]|[\u{1F3CB}-\u{1F3CE}]|[\u{1F3CF}-\u{1F3D3}]|[\u{1F3D4}-\u{1F3DF}]|[\u{1F3E0}-\u{1F3F0}]|[\u{1F3F3}-\u{1F3F5}]|\u{1F3F7}|[\u{1F3F8}-\u{1F3FF}]|[\u{1F400}-\u{1F43E}]|\u{1F43F}|\u{1F440}|\u{1F441}|[\u{1F442}-\u{1F4F7}]|\u{1F4F8}|[\u{1F4F9}-\u{1F4FC}]|\u{1F4FD}|\u{1F4FF}|[\u{1F500}-\u{1F53D}]|[\u{1F549}-\u{1F54A}]|[\u{1F54B}-\u{1F54E}]|[\u{1F550}-\u{1F567}]|[\u{1F56F}-\u{1F570}]|[\u{1F573}-\u{1F579}]|\u{1F57A}|\u{1F587}|[\u{1F58A}-\u{1F58D}]|\u{1F590}|[\u{1F595}-\u{1F596}]|\u{1F5A4}|\u{1F5A5}|\u{1F5A8}|[\u{1F5B1}-\u{1F5B2}]|\u{1F5BC}|[\u{1F5C2}-\u{1F5C4}]|[\u{1F5D1}-\u{1F5D3}]|[\u{1F5DC}-\u{1F5DE}]|\u{1F5E1}|\u{1F5E3}|\u{1F5E8}|\u{1F5EF}|\u{1F5F3}|\u{1F5FA}|[\u{1F5FB}-\u{1F5FF}]|\u{1F600}|[\u{1F601}-\u{1F610}]|\u{1F611}|[\u{1F612}-\u{1F614}]|\u{1F615}|\u{1F616}|\u{1F617}|\u{1F618}|\u{1F619}|\u{1F61A}|\u{1F61B}|[\u{1F61C}-\u{1F61E}]|\u{1F61F}|[\u{1F620}-\u{1F625}]|[\u{1F626}-\u{1F627}]|[\u{1F628}-\u{1F62B}]|\u{1F62C}|\u{1F62D}|[\u{1F62E}-\u{1F62F}]|[\u{1F630}-\u{1F633}]|\u{1F634}|[\u{1F635}-\u{1F640}]|[\u{1F641}-\u{1F642}]|[\u{1F643}-\u{1F644}]|[\u{1F645}-\u{1F64F}]|[\u{1F680}-\u{1F6C5}]|[\u{1F6CB}-\u{1F6CF}]|\u{1F6D0}|[\u{1F6D1}-\u{1F6D2}]|[\u{1F6E0}-\u{1F6E5}]|\u{1F6E9}|[\u{1F6EB}-\u{1F6EC}]|\u{1F6F0}|\u{1F6F3}|[\u{1F6F4}-\u{1F6F6}]|[\u{1F6F7}-\u{1F6F8}]|[\u{1F910}-\u{1F918}]|[\u{1F919}-\u{1F91E}]|\u{1F91F}|[\u{1F920}-\u{1F927}]|[\u{1F928}-\u{1F92F}]|\u{1F930}|[\u{1F931}-\u{1F932}]|[\u{1F933}-\u{1F93A}]|[\u{1F93C}-\u{1F93E}]|[\u{1F940}-\u{1F945}]|[\u{1F947}-\u{1F94B}]|\u{1F94C}|[\u{1F950}-\u{1F95E}]|[\u{1F95F}-\u{1F96B}]|[\u{1F980}-\u{1F984}]|[\u{1F985}-\u{1F991}]|[\u{1F992}-\u{1F997}]|\u{1F9C0}|[\u{1F9D0}-\u{1F9E6}]/gu;
//仿GM_xmlhttpRequest函数v1.3
if (typeof(GM_xmlhttpRequest) == "undefined") {
var GM_xmlhttpRequest = function(GM_param) {
var xhr = new XMLHttpRequest(); //创建XMLHttpRequest对象
xhr.open(GM_param.method, GM_param.url, true);
if (GM_param.responseType) xhr.responseType = GM_param.responseType;
if (GM_param.overrideMimeType) xhr.overrideMimeType(GM_param.overrideMimeType);
xhr.onreadystatechange = function() //设置回调函数
{
if (xhr.readyState === xhr.DONE) {
if (xhr.status === 200 && GM_param.onload)
GM_param.onload(xhr);
if (xhr.status !== 200 && GM_param.onerror)
GM_param.onerror(xhr);
}
}
for (var header in GM_param.headers) {
xhr.setRequestHeader(header, GM_param.headers[header]);
}
xhr.send(GM_param.data ? GM_param.data : null);
}
}
//仿GM_getValue函数v1.0
if(typeof(GM_getValue) == "undefined")
{
var GM_getValue = function(name, type){
var value = localStorage.getItem(name);
if (value == undefined) return value;
if ((/^(?:true|false)$/i.test(value) && type == undefined) || type == "boolean")
{
if (/^true$/i.test(value))
return true;
else if (/^false$/i.test(value))
return false;
else
return Boolean(value);
}
else if((/^\-?[\d\.]+$/i.test(value) && type == undefined) || type == "number")
return Number(value);
else
return value;
}
}
//仿GM_setValue函数v1.0
if(typeof(GM_setValue) == "undefined")
{
var GM_setValue = function(name, value){
localStorage.setItem(name, value);
}
}
//仿GM_deleteValue函数v1.0
if(typeof(GM_deleteValue) == "undefined")
{
var GM_deleteValue = function(name){
localStorage.removeItem(name);
}
}
//仿GM_listValues函数v1.0
if(typeof(GM_listValues) == "undefined")
{
var GM_listValues = function(){
var keys = [];
for (var ki=0, kilen=localStorage.length; ki<kilen; ki++)
{
keys.push(localStorage.key(ki));
}
return keys;
}
}
var ds = [];
var rowObj = function(){
this.name = "";
this.cname = [];
this.info = [];
this.tags = [];
this.links = [];
}
rowObj.prototype.addTagFromName = function(rowObj)
{
if (rowObj == undefined) rowObj = this;
GM_xmlhttpRequest({
method: "GET",
//url: wiki_URL + (rowObj.name.length?"/"+rowObj.name:""),
url: rowObj.links[0]?rowObj.links[0].href:(wiki_URL + (rowObj.name.length?"/"+rowObj.name:"")),
onload: function(response) {
var page_get_w = document.querySelector("#ETB_page-get");
if (page_get_w)
{
var statetxt = page_get_w.querySelector(".page-get-" + rowObj.name);
statetxt.classList.add("page-load");
statetxt.innerHTML = "获取成功";
}
console.debug("正在处理 %s %s 页面",rowObj.name,
rowObj.cname
.filter(function(item){return item.type==0;})
.map(function(item){return item.text;})
.join("")
);
dealTags(response.responseText,rowObj);
}
});
}
var tagObj = function(){
this.type = 0;
this.name = "";
this.cname = [];
this.info = [];
this.links = [];
}
//一条新的外部链接
var linkObj = function(text,href,title){
this.text = text||"";
this.href = href||"";
this.title = title||"";
}
//处理版本的页面
function dealVersion(response)
{
var PageDOM = new DOMParser().parseFromString(response, "text/html");
var wiki_version_Dom;
if (newRe)
{ //新仓库的结构版本
wiki_version_Dom = PageDOM.querySelector("#LC1");
}else
{ //wiki的结构版本
wiki_version_Dom = PageDOM.querySelector("a[title='database-structure-version']");
}
if (!wiki_version_Dom)
{
alert("未找到数据库结构版本,你的 " + scriptName + " 版本可能已经不适用新的数据库,请更新你的脚本。");
return;
}
var wiki_version = Number(wiki_version_Dom.textContent.replace(/\D/ig,""));
var page_get_w = document.querySelector("#ETB_page-get");
if (page_get_w)
{
var statetxt = page_get_w.querySelector(".page-get-wiki-version");
statetxt.classList.add("page-load");
statetxt.innerHTML = (newRe?"ETT组织仓库":"传统Wiki仓库")+"最新版本" + wiki_version + ",当前" + database_structure_version;
}
if (wiki_version > database_structure_version)
{
alert("Wiki数据库结构已更新,你的 " + scriptName + " 版本可能已经不适用新的数据库,请更新你的脚本。");
return;
}
}
//处理行的页面
function dealRows(response, dataset)
{
var PageDOM = new DOMParser().parseFromString(response, "text/html");
var page_get_w = document.querySelector("#ETB_page-get");
if (page_get_w)
{
var statetxt = page_get_w.querySelector(".page-get-rows");
statetxt.classList.add("page-load");
statetxt.innerHTML = "获取成功";
}
var table = PageDOM.querySelector((newRe?"#readme":"#wiki-body")+" .markdown-body table").tBodies[0];
rowsCount = table.rows.length;
for(var ri=0, rilen=table.rows.length; ri<rilen; ri++)
{
var trow = table.rows[ri];
var row = new rowObj;
row.name = trow.cells[0].textContent.replace(/(^\s|\s$)/ig,""); //去除首尾空格;
row.cname = InfoToArray(trow.cells[1]);
row.info = InfoToArray(trow.cells[2]);
row.links = LinksToArray(trow.cells[3]);
row.addTagFromName();
dataset.push(row);
if (page_get_w)
{
page_get_w.querySelector(".select-menu-list").appendChild(
buildMenuItem((function()
{
var div = document.createElement("div");
var span1 = document.createElement("span");
span1.className = "page-title";
span1.innerHTML = row.cname
.filter(function(item){return item.type==0;})
.map(function(item){return item.text;})
.join("");
div.appendChild(span1);
var span2 = document.createElement("span");
span2.className = "page-get-" + row.name;
span2.innerHTML = "等待";
div.appendChild(span2);
return div;
})()
)
);
}
}
}
//生成按钮
function specialCharToCss(str)
{
var strn = str;
strn = strn.replace("\\","\\\\");
strn = strn.replace("\"","\\\"");
strn = strn.replace("\r","");
strn = strn.replace("\n","\\A");
return strn;
}
//将外部链接变换为数组
function LinksToArray(linksDom)
{
var arr = [];
var as = linksDom.querySelectorAll("a");
for (var ai=0;ai<as.length;ai++)
{
var a = as[ai];
arr.push(new linkObj(a.textContent,a.href,a.title));
}
return arr;
}
//介绍信息转为数组
function InfoToArray(infoDom)
{
var arr = [];
if (infoDom.childNodes != undefined)
{
for (var ci=0, cilen=infoDom.childNodes.length; ci<cilen; ci++)
{
var node = infoDom.childNodes[ci];
//type,0是文字,1是换行,2是图片,3是链接
var InfoObj = {type:0};
switch (node.nodeName) {
case "#text":case "G-EMOJI":
InfoObj.type = 0;
if (node.textContent == "\n")
continue;
InfoObj.text = node.textContent;
break;
case "BR":
InfoObj.type = 1;
break;
case "IMG":
InfoObj.type = 2;
var osrc = node.getAttribute("data-canonical-src");
if (osrc) //链接写在data-canonical-src
{
InfoObj.src = osrc;
}else if(node.title.length > 0) //链接写在title
{
InfoObj.src = node.title;
}else if(node.src.length > 0) //链接写在src
{
InfoObj.src = node.src;
}else
{
console.error("发现未知的其他图片地址格式",node,"来自",infoDom);
}
InfoObj.alt = node.alt;
break;
case "A":
InfoObj.type = 3;
InfoObj.text = node.textContent;
InfoObj.href = node.href;
InfoObj.title = node.title;
break;
default: //未知的其他格式
console.error("发现未知的其他Node格式:" + node.nodeName,node,"来自",infoDom);
continue;
}
arr.push(InfoObj);
}
}
return arr;
}
//介绍信息数组输出到CSS
function InfoArrayToCssString(infoArr, creatImage)
{
if (creatImage == undefined) creatImage = true;
var infoArrTmp = infoArr.concat(); //创建编辑用临时数组
var str = []; //最终输出的内容字符串的部件数组
var lastText = false; //上一条是不是文字,用渝判断下一条是储存到临时数组等候拼接还是将前方的加入最终字符串
var strPart = []; //当前的临时字符串(储存多块相邻文本,用于拼接)
while (infoArrTmp.length>0)
{
var inf = infoArrTmp.shift();
if (inf.type == 0 || inf.type == 1 || inf.type == 3) //type,0是文字,1是换行,2是图片,3是链接
{ //处理文本
if (lastText) //如果上一条不是文本
{ //添加一条新的文本,或者换行
strPart.push(inf.text || "\\A");
}else
{ //处理每一张图片
str.push(strPart.map(function(item){return 'url("' + item + '")'}).join("")); //已储存的图片添加到最终字符串
strPart = [];
lastText = true;
strPart.push(inf.text || "\\A");
}
}else if (creatImage) //如果同意生成图片,才处理图片
{ //处理图片
if (lastText)
{ //处理每一条文本
var txtTmp = strPart.join("");
txtTmp = txtTmp.replace(/\"/igm,"\\\""); //将引号改为斜杠引号
if (!creatImage) txtTmp = dealEmoji(txtTmp); //去除Emoji
str.push('"' + txtTmp + '"');
strPart = [];
lastText = false;
strPart.push(inf.src);
}else
{ //添加一张新的图片
strPart.push(inf.src);
}
}
}
//最后没被处理掉的
if (lastText)
{ //处理每一条文本
var txtTmp = strPart.join("");
txtTmp = txtTmp.replace(/\"/igm,"\\\""); //将引号改为斜杠引号
if (!creatImage) txtTmp = dealEmoji(txtTmp); //去除Emoji
str.push('"' + txtTmp + '"');
strPart = [];
lastText = false;
}else
{ //处理每一张图片
str.push(strPart.map(function(item){return 'url("' + item + '")'}).join(""));
strPart = [];
lastText = true;
}
return str.join("");
}
//处理Tag页面
function dealTags(response, rowdataset)
{
var rowTags = rowdataset.tags;
var PageDOM = new DOMParser().parseFromString(response, "text/html");
var table = PageDOM.querySelector((newRe?"#readme":"#wiki-body")+" .markdown-body table");
if (table == undefined)
{
alert(PageDOM.title + "\n该页面未发现数据表格,可能存在格式错误。")
console.error("未发现表格",PageDOM.title);
}
var tBody = table.tBodies[0];
for(var ri=0, rilen=tBody.rows.length; ri<rilen; ri++)
{
var trow = tBody.rows[ri];
var tag = new tagObj;
if (trow.cells.length > 2)
{//没有足够单元格的跳过
tag.name = trow.cells[0].textContent.replace(/(^\s|\s$)/ig,""); //去除首尾空格
tag.cname = InfoToArray(trow.cells[1]);
tag.info = InfoToArray(trow.cells[2]);
tag.links = LinksToArray(trow.cells[3]);
//type=0代表一行翻译,1代表注释
tag.type = tag.name.replace(/\s/ig,"").length < 1 ? 1 : 0; //英文去除所有空格后如果没有文字,则算为注释
if (tag.type != 1 && tag.cname.length < 1) //不是注释,中文名又没有文字
{
//console.error("发现无中文翻译行%d - %s:%s",ri,rowdataset.name,tag.name);
}
if (tag.type != 1 && rowTags.some(function(ttag){return ttag.name == tag.name;})) //从数组中搜索任一符合条件的,返回true
{
console.error("发现重复定义行%d - %s:%s",ri,rowdataset.name,tag.name);
}
rowTags.push(tag);
}
else
{
console.error("发现无3单元格的错误行%d %s",ri,tag.name);
}
}
rowsCurrent++;
}
//点击开始任务按钮
function startProgram(dataset, mode){
var downOver = startProgramCheck(dataset, mode);
if (!downOver || downOverCheckHook == undefined)
{
GM_xmlhttpRequest({
method: "GET",
url: wiki_URL + "/" + wiki_version_filename,
onload: function(response) {
dealVersion(response.responseText);
GM_xmlhttpRequest({
method: "GET",
url: wiki_URL + "/" + rows_filename,
onload: function(response) {
dealRows(response.responseText,dataset);
}
});
},
onerror: function(response) {
dealVersion("");
GM_xmlhttpRequest({
method: "GET",
url: wiki_URL + "/" + rows_filename,
onload: function(response) {
dealRows(response.responseText,dataset);
}
});
},
});
downOverCheckHook = setInterval(function () { startProgramCheck(dataset, mode) }, 200);
}
if (!downOver)
{
buildDownloadProgress();
}
}
//创建下载进度窗口
function buildDownloadProgress()
{
var page_get_w = document.querySelector("#ETB_page-get");
if (!page_get_w)
{
windowInserPlace.appendChild(buildMenuModal("window", "ETB_page-get", "数据获取进度", null, [
buildMenuList([
buildMenuItem((function()
{
var div = document.createElement("div");
var span1 = document.createElement("span");
span1.className = "page-title";
span1.innerHTML = "Wiki结构版本";
div.appendChild(span1);
var span2 = document.createElement("span");
span2.className = "page-get-wiki-version";
span2.innerHTML = "等待";
div.appendChild(span2);
return div;
})()
),
buildMenuItem((function()
{
var div = document.createElement("div");
var span1 = document.createElement("span");
span1.className = "page-title";
span1.innerHTML = "列表页面";
div.appendChild(span1);
var span2 = document.createElement("span");
span2.className = "page-get-rows";
span2.innerHTML = "等待";
div.appendChild(span2);
return div;
})()
),
])
],
[
".ETB_page-get .page-title" + "{\r\n" + [
'font-weight: bold',
'margin-right: 15px',
].join(';\r\n') + "\r\n}",
".ETB_page-get .page-load" + "{\r\n" + [
'color: #0A0',
].join(';\r\n') + "\r\n}",
].join('\r\n')
));
}
else
{
page_get_w.style.display = "block";
}
}
//检测下载完成情况
function startProgramCheck(dataset, mode)
{
if (rowsCount > 0 && rowsCurrent >= rowsCount)
{
console.debug("获取完成");
clearInterval(downOverCheckHook);
switch (mode) {
case 1:
buildJSOutput(dataset);
break;
case 0:
default:
buildCSSOutput(dataset);
break;
}
var page_get_w = document.querySelector("#ETB_page-get");
if (page_get_w)
{
page_get_w.parentNode.removeChild(page_get_w);
}
return true;
}
else
{
console.debug("获取%d/%d",rowsCurrent, rowsCount);
return false;
}
}
//获取完成后创建CSS输出窗口
function buildCSSOutput(dataset)
{
var css = createOutputCSS(dataset
,GM_getValue("ETB_create-info","boolean")
,GM_getValue("ETB_create-info-image","boolean")
,GM_getValue("ETB_create-cname-image","boolean")
);
var downBlob = new Blob([css], {'type': 'text\/css'});
var downurl = window.URL.createObjectURL(downBlob);
var css_output_w = document.querySelector("#ETB_css-output");
if (!css_output_w)
{
windowInserPlace.appendChild(buildMenuModal("window", "ETB_css-output", "用户样式版EhTagTranslator", null,
[
buildMenuList([
buildMenuItem("CSS文本",
(function()
{
var textarea = document.createElement("textarea");
textarea.id = "ETB_css-textarea";
textarea.name = textarea.id;
textarea.className = "txta " + textarea.id;
textarea.value = css;
textarea.wrap = "off";
textarea.setAttribute("readonly",true);
return textarea;
})()
,buildSVG("css")),
buildMenuItem("直接下载CSS文件",null,buildSVG("download"),downurl,1),
])
],
[
".ETB_css-output .txta" + "{\r\n" + [
'resize: vertical',
'width:100%',
'height:300px',
].join(';\r\n') + "\r\n}",
].join('\r\n')
));
}
else
{
css_output_w.style.display = "block";
css_output_w.querySelector(".ETB_css-textarea").value = css;
css_output_w.querySelector("a").href = downurl;
}
}
//开始构建CSS
function createOutputCSS(dataset, createInfo, createInfoImage, createCnameImage)
{
if (createInfo == undefined) createInfo = true;
if (createInfoImage == undefined) createInfoImage = true;
if (createCnameImage == undefined) createCnameImage = true;
var date = new Date();
var cssAry = [];
//样式信息说明
cssAry.push(
"/* EhTagTranslator 用户样式版,由 " + scriptName + " v" + scriptVersion + " 构建"
," * 数据来源于" + wiki_URL
," * 构建时间为"
," * " + date.toString()
," */"
)
//样式命名区间
cssAry.push(
//▼CSS内容部分
"@namespace url(http://www.w3.org/1999/xhtml);"
//▲CSS内容部分
);
//表里通用样式
cssAry.push(""
//▼CSS内容部分
,"/* 表里通用样式 */"
,"@-moz-document"
," domain('exhentai.org'),"
," domain('e-hentai.org')"
,"{"
,GM_getValue("ETB_global-style")
,"}"
//▲CSS内容部分
);
//表站样式
cssAry.push(""
//▼CSS内容部分
,"/* 表站样式 */"
,"@-moz-document"
," domain('e-hentai.org')"
,"{"
,GM_getValue("ETB_global-style-eh")
,"}"
//▲CSS内容部分
);
//里站样式
cssAry.push(""
//▼CSS内容部分
,"/* 里站样式 */"
,"@-moz-document"
," domain('exhentai.org')"
,"{"
,GM_getValue("ETB_global-style-ex")
,"}"
//▲CSS内容部分
);
//翻译内容
cssAry.push(""
//▼CSS内容部分
,"/* 翻译内容 */"
,"@-moz-document"
," domain('exhentai.org'),"
," domain('e-hentai.org')"
,"{"
//▲CSS内容部分
);
dataset.forEach(function(row){
//添加行名的注释
cssAry.push(""
,"/* " + row.name
," * " + row.cname
.filter(function(item){return item.type==0;})
.map(function(item){return item.text;})
.join("")
," */"
);
row.tags.forEach(function(tag){
if (tag.type == 0)
{
var tagid = (row.name=="misc"?"":row.name + ":") + tag.name.replace(/\s/ig,"_");
cssAry.push(""
//▼CSS内容部分
," a[id=\"ta_" + tagid + "\"]{"
," font-size:0;"
," }"
," a[id=\"ta_" + tagid + "\"]::before{"
," content:" + InfoArrayToCssString(tag.cname, createCnameImage) + ";"
," }"
//▲CSS内容部分
);
if (createInfo)
{
var sinfo = InfoArrayToCssString(tag.info, createInfoImage);
if (sinfo.replace(/\s/ig,"").length > 0)
{
cssAry.push(""
//▼CSS内容部分
," a[id=\"ta_" + tagid + "\"]::after{"
," content:" + sinfo + ";"
," }"
//▲CSS内容部分
);
}
}
}
else
{ //将注释写成CSS注释
cssAry.push(
"/* " + InfoArrayToCssString(tag.cname, false)
," * " + InfoArrayToCssString(tag.info, false)
," */"
);
}
})//row.tags.forEach
});//dataset.forEach
cssAry.push(
//▼CSS内容部分
"}"
//▲CSS内容部分
);
var css = cssAry.join("\r\n");
return css;
}
//获取完成后创建JS输出窗口
function buildJSOutput(dataset)
{
var json = createOutputJSON(dataset
,GM_getValue("ETB_create-info","boolean")
,GM_getValue("ETB_create-info-image","boolean")
,GM_getValue("ETB_create-cname-image","boolean")
);
var jsonStr = JSON.stringify(json);
var downBlob = new Blob([jsonStr], {'type': 'text\/json'});
var downurl = window.URL.createObjectURL(downBlob);
var js_output_w = document.querySelector("#ETB_js-output");
if (!js_output_w)
{
windowInserPlace.appendChild(buildMenuModal("window", "ETB_js-output", "用户脚本版EhTagTranslator数据库", null,
[
buildMenuList([
buildMenuItem("JSON文本",
(function()
{
var textarea = document.createElement("textarea");
textarea.id = "ETB_js-textarea";
textarea.name = textarea.id;
textarea.className = "txta " + textarea.id;
textarea.value = jsonStr;
textarea.setAttribute("readonly",true);
return textarea;
})()
,buildSVG("json")),
buildMenuItem("数据库数量",
(function()
{
var tb = document.createElement("table");
tb.id = "ETB_database-count";
tb.name = tb.id;
tb.className = tb.id;
//正式开始构建内容
//tHead
tb.createTHead();
var th = tb.tHead.insertRow();
th.insertCell().appendChild(document.createTextNode("行名"));
th.insertCell().appendChild(document.createTextNode("数据行数"));
th.insertCell().appendChild(document.createTextNode("注释行数"));
//tBody
json.dataset.forEach(function(item){
var tr = tb.insertRow();
tr.insertCell().appendChild(document.createTextNode(item.cname
.filter(function(item){return item.type==0;})
.map(function(item){return item.text;})
.join("")));
tr.insertCell().appendChild(document.createTextNode(item.tags.filter(function(item){return item.type==0}).length));
tr.insertCell().appendChild(document.createTextNode(item.tags.filter(function(item){return item.type==1}).length));
})
return tb;
})()
,buildSVG("book")),
buildMenuItem("直接下载JSON文件",null,buildSVG("download"),downurl,1)
])
],
[
".ETB_js-output .txta" + "{\r\n" + [
'resize: vertical',
'width:100%',
'height:200px',
].join(';\r\n') + "\r\n}",
"#ETB_js-output .select-menu-list" + "{\r\n" + [
'max-height: 700px',
].join(';\r\n') + "\r\n}",
"#ETB_database-count,#ETB_database-count td" + "{\r\n" + [
'border: solid 1px lightgrey;',
].join(';\r\n') + "\r\n}",
].join('\r\n')
));
}
else
{
js_output_w.style.display = "block";
js_output_w.querySelector(".ETB_js-textarea").value = jsonStr;
js_output_w.querySelector("a").href = downurl;
}
}
//开始构建JSON
function createOutputJSON(dataset, createInfo, createInfoImage, createCnameImage)
{
if (createInfo == undefined) createInfo = true;
if (createInfoImage == undefined) createInfoImage = true;
if (createCnameImage == undefined) createCnameImage = true;
var outArray =
/*
* 生成一个不会干涉到原对象的Row对象
*/
dataset.map(function(row_orignal){
var row = Object.assign(new rowObj(), row_orignal);
row.cname=row_orignal.cname
.filter(function(item){return createCnameImage || item.type != 2;})
.map(function(item){
var newItem = Object.assign({}, item);
if(!createCnameImage && item.text) {
newItem.text = dealEmoji(item.text);
}
return newItem;
});
if (createInfo)
{
row.info=row_orignal.info
.filter(function(item){return createInfoImage || item.type != 2;})
.map(function(item){
var newItem = Object.assign({}, item);
if(!createInfoImage && item.text) {
newItem.text = dealEmoji(item.text);
}
return newItem;
});
}
else
{
row.info = null;
}
row.links=row_orignal.links
.map(function(item){
var newItem = Object.assign(new linkObj(), item);
return newItem;
});
row.tags =
/*
* 生成一个不会干涉到原对象的Tag对象
*/
row_orignal.tags.map(function(tag_orignal){
var tag = Object.assign(new tagObj(), tag_orignal);
tag.cname=tag_orignal.cname
.filter(function(item){return createCnameImage || item.type != 2;})
.map(function(item){
var newItem = Object.assign({}, item);
if(!createCnameImage && item.text) {
newItem.text = dealEmoji(item.text);
}
return newItem;
});
tag.links=tag_orignal.links
.map(function(item){
var newItem = Object.assign(new linkObj(), item);
return newItem;
});
if (createInfo || tag.type==1)
{
tag.info=tag_orignal.info
.filter(function(item){return createInfoImage || item.type != 2;})
.map(function(item){
var newItem = Object.assign({}, item);
if(!createInfoImage && item.text) {
newItem.text = dealEmoji(item.text);
}
return newItem;
});
}
else
{
tag.info = null;
}
return tag;
})//row.tags.forEach
return row;
});//dataset.forEach
var date = new Date();
var outJson =
{
"scriptName":scriptName,
"scriptVersion":scriptVersion,
"database-structure-version":database_structure_version,
"date":date.getTime(),
"dataset":outArray
}
return outJson;
}
//去除文本中的Emoji字符
function dealEmoji(str)
{
return str.replace(emojireg,"");
}
//生成按钮
function buildButton(title, icon, modal)
{
var li = document.createElement("li");
var select_menu = document.createElement("div");
select_menu.className = "select-menu js-menu-container js-select-menu";
li.appendChild(select_menu);
var button = document.createElement("button");
button.className = "btn btn-sm select-menu-button js-menu-target css-truncate";
select_menu.appendChild(button);
var span = document.createElement("span");
span.className = "js-select-button";
if (icon != undefined)
span.appendChild(icon);
span.innerHTML += title;
button.appendChild(span);
select_menu.appendChild(modal);
return li;
}
//生成菜单窗口
function buildMenuModal(mode, id, stitle, filters, lists, sstyle)
{
var modal_holder = document.createElement("div");
modal_holder.className = "select-menu-modal-holder js-menu-content js-navigation-container js-active-navigation-container";
if (id != undefined)
{
modal_holder.id = id;
modal_holder.classList.add(id);
}
if (sstyle != undefined)
{
var style = document.createElement("style");
style.innerHTML = sstyle;
modal_holder.appendChild(style);
}
var modal = document.createElement("div");
modal.className = "select-menu-modal subscription-menu-modal js-menu-content";
modal_holder.appendChild(modal);
var header = document.createElement("div");
header.className = "select-menu-header js-navigation-enable";
modal.appendChild(header);
var CloseSvg = buildSVG("Close");
header.appendChild(CloseSvg);
switch (mode) {
case "window":
modal_holder.style.display = "block";
CloseSvg.onclick = function(){
modal_holder.style.display = "none";
}
break;
case "menu":
default: