-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathehx_direct_download.user.js
1453 lines (1316 loc) · 63.6 KB
/
ehx_direct_download.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 ehx direct download
// @namespace https://github.com/x94fujo6rpg/SomeTampermonkeyScripts
// @updateURL https://github.com/x94fujo6rpg/SomeTampermonkeyScripts/raw/master/ehx_direct_download.user.js
// @downloadURL https://github.com/x94fujo6rpg/SomeTampermonkeyScripts/raw/master/ehx_direct_download.user.js
// @version 1.15
// @description direct download archive from list / sort gallery (in current page) / show full title in pure text
// @author x94fujo6
// @match https://e-hentai.org/*
// @exclude https://e-hentai.org/mytags
// @exclude https://e-hentai.org/mpv/*
// @match https://exhentai.org/*
// @exclude https://exhentai.org/mytags
// @exclude https://exhentai.org/mpv/*
// @grant GM_getValue
// @grant GM_setValue
// ==/UserScript==
/* jshint esversion: 9 */
// this script only work in Thumbnail mode
(function () {
'use strict';
let api;
let domain;
let hid = true;
let m = "[ehx direct download] ";
let debug_message = true;
let debug_adv = false;
let gdata = [];
let gallery_count = 0;
let timer_list = [];
let key_list = {
dl_list: "exhddl_list",
exclude_tag: "exhddl_exclude_tag_list",
exclude_uploader: "exhddl_exclude_uploader_list",
sort_setting: "exhddl_sortsetting",
dl_and_copy: "exhddl_dl_and_copy",
auto_fix_title: "exhddl_auto_fix_title",
auto_enable_puretext: "exhddl_auto_enable_puretext",
sort_numeric: "exhddl_sort_numeric",
sort_ignore_punctuation: "exhddl_sort_ignore_punctuation",
};
let default_value = {
dl_list: [],
exclude_tag: [],
exclude_uploader: [],
sort_setting: true,
dl_and_copy: true,
auto_fix_title: true,
auto_enable_puretext: true,
sort_numeric: true,
sort_ignore_punctuation: false,
};
let id_list = {
mainbox: "exhddl_activate",
dd: "exhddl_ddbutton",
puretext: "exhddl_puretext",
sort_setting: "exhddl_sortsetting",
jump_to_last: "exhddl_jump_to_last",
dl_and_copy: "exhddl_dl_and_copy",
auto_fix_title: "exhddl_auto_fix_title",
auto_enable_puretext: "exhddl_auto_enable_puretext",
sort_numeric: "exhddl_sort_numeric",
sort_ignore_punctuation: "exhddl_sort_ignore_punctuation",
};
let style_list = {
top_button: { width: "max-content" },
gallery_button: {
//width: "max-content",
width: "75%",
alignSelf: "center",
lineHeight: "1.75rem",
border: "transparent solid 0.125rem",
borderRadius: "0.25rem",
marginTop: "0.75rem",
},
gallery_marked: { backgroundColor: "black" },
button_marked: {
color: "gray",
backgroundColor: "transparent",
border: "gray solid 0.125rem",
},
ex: { backgroundColor: "goldenrod" },
mainbox: { textAlign: "center", lineHeight: "2rem" },
separator: { color: "transparent" },
};
let status_update_interval = 500;
let fix_prefix = false;
let ignore_prefix = [
"(同人誌)",
"(成年コミック)",
"(成年コミック・雑誌)",
"(一般コミック)",
"(一般コミック・雑誌)",
"(エロライトノベル)",
"(ゲームCG)",
"(同人ゲームCG)",
"(18禁ゲームCG)",
"(同人CG集)",
"(画集)",
];
let container_list = [
"()", "[]", "{}", "()",
"[]", "{}", "【】", "『』", "《》", "〈〉", "「」"
];
let [container_start, container_end] = extracContainer();
let container_reg = containerRegexGenerator();
let group_reg = new RegExp(`^${container_reg}`);
let excess_reg = new RegExp(`^\\s*${container_reg}\\s*|\\s*${container_reg}\\s*$`, "g");
let blank_reg = /[\s ]{2,}/g;
let enable_sim_search = false;
let sim_search_threshold = 0.6;
let gallery_data_max_size = 0; // kb, 0 = no limit
let gallery_data_limit = { max_size: 1024 * (gallery_data_max_size), max_length: parseInt((1024 * gallery_data_max_size) / 7, 10), };
window.onload = main();
function main() {
domain = `https://${document.domain}`;
api = `${domain}/api.php`;
myCss();
timerMananger();
let link = document.location.href;
if (link.includes(".php")) {
return print(`${m}see php, abort`);
} else if (link.includes("/g/")) {
print(`${m}gallery page`);
return setEvent(link);
} else {
print(`${m}normal start`);
if (!checkDisplayMode) return wrongDisplayMode();
return setButton();
}
function myCss() {
let newcss = Object.assign(document.createElement("style"), {
id: "ehx_direct_download_css",
innerHTML: `
.puretext {
overflow: hidden;
min-height: 32px;
line-height: 16px;
margin: 6px 4px 0;
font-size: 10pt;
text-align: center;
}
.gallery_box {
text-align: center;
/*line-height: 2rem;*/
/*margin: auto auto 0rem auto;*/
margin: auto 0.25rem 0.25rem;
max-height: max-content;
}
.torrent_title {
line-height: 1rem;
text-align: center;
margin: 0.2rem;
border: 0.1rem solid;
padding-bottom: 0.4rem;
display: inline-grid;
}
.prefix_from {
text-align: center;
white-space: break-spaces;
border: 0.1rem solid blueviolet;
position: relative;
background: rgba(138, 43, 226, 0.1);
}
.prefix_from>div {
display: none;
}
.prefix_from:hover>div {
display: block;
position: absolute;
z-index: 10;
margin: 0.6rem;
overflow: hidden;
bottom: 100%;
}
`,
});
document.head.appendChild(newcss);
setTitleStyle();
}
function setTitleStyle() {
[...[...document.styleSheets].find(s => s.href).cssRules].find(s => s.selectorText == ".gl4t").style.removeProperty("max-height");
}
function setEvent(link) {
let _link = link.split("/");
if (_link[3] === "g") {
let gid = _link[4];
let archive_download = findEleByText("a", "Archive Download");
if (archive_download) {
archive_download.addEventListener("click", () => {
addToDownloadedList(gid);
});
print(`${m}set trigger for updateList on gallery:${gid}`);
} else {
if (typeof gotonext != "undefined") {
let search_site = `https://panda.chaika.moe/search/?qsearch=${encodeURIComponent(link)}`;
let redir = () => {
let _ele = document.querySelector("#continue");
_ele.firstChild.text = "(Go to Front Page)";
_ele = _ele.parentElement;
_ele.children[1].remove();
_ele.innerHTML = `${_ele.innerHTML}
<p></p>
<br>
<p><a href="${search_site}">[ Search on panda.chaika.moe ]</a></p>
`;
};
gotonext = () => { };
redir();
return;
}
}
}
let url = document.location.href;
let match_eh = url.match(/e-hentai.org\/g\/\d+\/\w+\//) ? url.replace("e-hentai", "exhentai") : false;
let match_ex = url.match(/exhentai.org\/g\/\d+\/\w+\//) ? url.replace("exhentai", "e-hentai") : false;
if (match_eh || match_ex) {
let pos = document.querySelector(".gtb");
let b = Object.assign(document.createElement("div"), {
className: "tha",
textContent: match_eh ? "goto exhentai" : "goto e-hentai",
style: `
margin: auto;
margin-bottom: 0.5rem;
float: none;
width: max-content;
`,
onclick: () => { document.location.href = match_eh ? match_eh : match_ex; },
});
pos.insertAdjacentElement("afterbegin", b);
}
}
function checkDisplayMode() {
let setting = document.getElementById("dms");
if (!setting) return false;
setting = setting.querySelector("option[value='t']");
return setting.selected;
}
function wrongDisplayMode() {
let pos = document.querySelector(".ido");
pos.insertAdjacentElement("afterbegin", newSpan("Display mode is not Thumbnail. Script stop"));
}
function setButton() {
let box = Object.assign(document.createElement("div"), { id: id_list.mainbox });
Object.assign(box.style, style_list.mainbox);
let nodelist = [
newButton(id_list.dd, "Enable Archive Download / Sorting / Show torrents Title / Fix Event in Ttile / Exclude Gallery", style_list.top_button, enableDirectDownload),
newSeparate(),
newButton(id_list.puretext, "Show Pure Text", style_list.top_button, pureText),
newSeparate(),
newButton(id_list.jump_to_last, "Jump To Nearest Downloaded", style_list.top_button, jumpToLastDownload),
newLine(),
];
box = appendAllChild(box, nodelist);
document.getElementById("toppane").insertAdjacentElement("afterend", box);
if (hid) hlexg();
forEachGallery(gallery => {
addInfoToGallery(gallery);
setLinkToNewTab(gallery);
});
addTimer(updateGalleryStatus, status_update_interval);
function enableDirectDownload() {
group();
time(`${m}request_data`);
let dd = document.getElementById(id_list.dd);
dd.disabled = true;
dd.removeAttribute("onclick");
dd.insertAdjacentElement("afterend", newSpan("Processing... Please Wait"));
acquireGalleryData();
setBottomStyle();
function setBottomStyle() {
[...[...document.styleSheets].find(s => s.href).cssRules].find(s => s.selectorText == ".gl5t").style.removeProperty("margin");
}
function acquireGalleryData() {
let gallery_nodelist = selectAllGallery();
if (gallery_nodelist) {
print(`${m}acquire gallery data`);
let data = { method: "gdata", gidlist: [], namespace: 1 };
let alldata = [];
let count = 0;
let glist = [];
for (let index = 0, length = gallery_nodelist.length; index < length; index++) {
let gallery = gallery_nodelist[index];
let gid = gallery.getAttribute("gid");
let gtoken = gallery.getAttribute("gtoken");
if (gid && gtoken) {
glist.push([gid, gtoken]);
count++;
gallery_count++;
if (count === 25 || index === length - 1) {
count = 0;
let newdata = Object.assign({}, data);
newdata.gidlist = Object.assign([], glist);
alldata.push(newdata);
glist = [];
}
}
}
print(`${m}gallery queue length:[${alldata.length}], total gallery count:[${gallery_count}]`);
if (alldata.length != 0) {
requestData(alldata);
} else {
print(`${m}gallery queue is empty`);
}
}
}
async function requestData(datalist) {
print(`${m}start sending request`);
print(`${m}----------------------`);
for (let index = 0, length = datalist.length; index < length; index++) {
print(`${m}sending request[${index}]`);
await myApiCall(datalist[index])
.then(async reslove => {
print(`${m}receive request[${index}]`);
await directDL(reslove, index);
})
.catch(reject => {
print(`${m}request[${index}] failed`, reject);
});
}
// all done
groupEnd();
timeEnd(`${m}request_data`);
print(`${m}all request done`);
print(`${m}process data`);
processGdata();
print(`${m}setup sorting`);
setSortingButton();
print(`${m}setup copy title`);
forEachGallery(setCopyTitle);
print(`${m}setup show torrent title`);
forEachGallery(setShowTorrent);
if (getGMValue("auto_fix_title")) {
print(`${m}auto enable fix title`);
document.getElementById("exhddl_fix_title").click();
}
if (getGMValue("auto_enable_puretext")) {
print(`${m}auto enable pure text`);
document.getElementById("exhddl_puretext").click();
}
}
function myApiCall(data) {
return new Promise((reslove, reject) => {
let request = new XMLHttpRequest();
request.open("POST", api);
request.setRequestHeader("Content-Type", "application/json");
request.withCredentials = true;
request.onreadystatechange = () => {
if (request.readyState == 4) {
return (request.status == 200) ? reslove(request.responseText) : reject(request.responseText);
}
};
request.send(JSON.stringify(data));
});
}
}
function pureText() {
let button = document.getElementById(id_list.puretext);
button.disabled = true;
button.removeAttribute("onclick");
forEachGallery(gallery => {
let puretext_div = Object.assign(document.createElement("div"), {
innerHTML: gallery.querySelector(".glname").innerHTML,
className: "puretext",
});
puretext_div.setAttribute("name", id_list.puretext);
let pos = gallery.querySelector(".prefix_from");
if (!pos) pos = gallery.querySelector(".gl3t");
pos.insertAdjacentElement("afterend", puretext_div);
});
}
function jumpToLastDownload() {
let last = document.querySelector("[marked='true']");
if (last) last.scrollIntoView();
}
function hlexg() {
forEachGallery(gallery => { if (gallery.querySelector("s")) Object.assign(gallery.style, style_list.ex); });
}
}
}
function directDL(data, index) {
data = JSON.parse(data);
print(`${m}process request[${index}] data, gallery count:[${Object.keys(data.gmetadata).length}]`);
let downloaded_list = getGMList("dl_list");
let gidlist = [];
let mark_list = [];
for (let gallery_data of data.gmetadata) {
gdata.push(gallery_data);
let gid = gallery_data.gid;
let gtoken = gallery_data.token;
let archiver_key = gallery_data.archiver_key;
let archivelink = `${domain}/archiver.php?gid=${gid}&token=${gtoken}&or=${archiver_key}`;
let glink = `${domain}/g/${gid}/${gtoken}/`;
let gallery = document.querySelector(`.gl1t[gid='${gid}']`);
if (gallery) {
let dl_button = Object.assign(document.createElement("button"), {
id: `gallery_dl_${gid}`,
className: "gdd",
textContent: "Archive Download",
onclick: function () {
let self = this;
let ck = document.getElementById(id_list.dl_and_copy).checked;
if (ck) {
navigator.clipboard.writeText(repalceForbiddenChar(self.parentElement.parentElement.querySelector(".glname").textContent.trim()))
.then(() => downloadButton(self, gid, archivelink, glink));
} else {
downloadButton(self, gid, archivelink, glink);
}
},
});
Object.assign(dl_button.style, style_list.gallery_button);
if (downloaded_list.indexOf(`${gid}`) != -1) {
Object.assign(dl_button.style, style_list.button_marked);
dl_button.setAttribute("marked", true);
mark_list.push(gid);
}
let box = Object.assign(document.createElement("div"), { className: "gallery_box" });
box.insertAdjacentElement("afterbegin", dl_button);
let pos = gallery.querySelector(".gl5t");
pos.insertAdjacentElement("beforebegin", box);
gidlist.push(dl_button.id);
let set_status_button = Object.assign(document.createElement("button"), {
id: `gallery_status_${gid}`,
className: "gstatus",
textContent: "Mark/Unmark This",
onclick: function () { setGalleryStatus(gid); },
});
Object.assign(set_status_button.style, style_list.gallery_button);
dl_button.insertAdjacentElement("afterend", set_status_button);
dl_button.insertAdjacentElement("afterend", newLine());
}
}
if (mark_list.length > 0) print(`${m}found in list, set as downloaded:\n`, mark_list);
print(`${m}request[${index}] done`);
print(`${m}----------------------`);
return new Promise(reslove => reslove());
function downloadButton(button, gid, archivelink, glink) {
Object.assign(button.style, style_list.button_marked);
button.setAttribute("marked", true);
visitGallery(glink);
addToDownloadedList(gid);
my_popUp(archivelink, 480, 320);
updateGalleryStatus();
}
function setGalleryStatus(gid) {
gid = `${gid}`; // convert to string
let downloaded_list = getGMList("dl_list");
if (downloaded_list.length > 0) {
let index = downloaded_list.indexOf(gid);
if (index != -1) {
downloaded_list.splice(index, 1);
print(`${m}gallery:[${gid}] in list, remove from list`);
resetGalleryStatus(gid);
} else {
downloaded_list.push(gid);
print(`${m}gallery:[${gid}] not in list, add to list`);
let count = 0;
if (downloaded_list.length > gallery_data_limit.max_size && gallery_data_limit.max_size != 0) {
while (downloaded_list.length > gallery_data_limit.max_size) {
let r = downloaded_list.shift();
print(`${m}%creach limit, remove [${r}]`, "color:OrangeRed;");
count++;
if (count > 100) return print(`${m}unknow error while removing old data, script stop`);
}
}
}
} else {
downloaded_list = [gid];
}
let list_length = downloaded_list.length;
setGMData(key_list.dl_list, downloaded_list);
//downloaded_list = downloaded_list.join();
//GM_setValue(key_list.dl_list, downloaded_list);
print(`${m}save list. [list_size:${downloaded_list.join().length} (limit:${gallery_data_limit.max_size}), list_length:${list_length} (possible limit:${gallery_data_limit.max_length})]`);
updateGalleryStatus();
function resetGalleryStatus(gid) {
let gallery = document.querySelector(`[gid="${gid}"]`);
if (!gallery.querySelector("s")) gallery.style.removeProperty("background-color");
gallery.removeAttribute("marked");
let dl_button = gallery.querySelector(".gdd");
dl_button.style = "";
Object.assign(dl_button.style, style_list.gallery_button);
dl_button.removeAttribute("marked");
}
}
}
function processGdata() {
let tag_key_list = [
"artist:",
"group:",
"female:",
"male:",
"parody:",
"character:",
"language:",
];
dGroup();
for (let data of gdata) {
// extract tags
let copy_tags = Object.assign([], data.tags);
for (let tag_key of tag_key_list) {
let data_key = tag_key.replace(":", "");
data[data_key] = [];
copy_tags.forEach(tag => { if (tag.includes(tag_key)) data[data_key].push(tag); });
// remove used
copy_tags = copy_tags.filter(tag => !data[data_key].includes(tag));
}
data.misc = copy_tags; // unuse list
data.title_original = decodeHTMLString(data.title);
data.title_jpn = data.title_jpn.length > 0 ? decodeHTMLString(data.title_jpn) : data.title;
data.title_jpn_original = data.title_jpn;
[data.title_prefix, data.title_no_event] = extractPrefix(data.title);
let title_prefix_jpn;
[title_prefix_jpn, data.title_no_event_jpn] = extractPrefix(data.title_jpn);
// try to found prefix in title_jpn
if (title_prefix_jpn) data.title_prefix = title_prefix_jpn;
let from_torrent = false;
if (data.title_prefix.length == 0) {
// try to found prefix in torrent
let torrent_list = getTorrentList(data.gid);
if (torrent_list) {
for (let torrent of torrent_list) {
let [prefix,] = extractPrefix(torrent);
if (prefix) {
data.title_prefix = prefix;
from_torrent = true;
break;
}
}
}
}
[data.title_group, data.title_no_group] = extractGroup(data.title_no_event);
[data.title_group_jpn, data.title_no_group_jpn] = extractGroup(data.title_no_event_jpn);
data.title_pure = removeExcess(data.title_no_group);
data.title_pure_jpn = removeExcess(data.title_no_group_jpn);
data.title_pure_for_sim = removeAllPunctuation(data.title_pure).toLowerCase();
data.title_pure_jpn_for_sim = removeAllPunctuation(data.title_pure_jpn).toLowerCase();
if (debug_message && debug_adv) {
dPrint(`${String(data.gid).padStart(10)}|__________`);
let title_list = [
"title_original",
//"title_no_event",
//"title_no_group",
"title_pure",
"title_prefix",
//"title_group",
"title_jpn",
//"title_no_event_jpn",
//"title_no_group_jpn",
"title_pure_jpn",
//"title_group_jpn"
"title_pure_for_sim",
"title_pure_jpn_for_sim",
];
title_list.forEach(key => {
let add = (from_torrent && key == "title_prefix") ? " found in torrent" : "";
dPrint(`${String(data.gid).padStart(10)}|${key.padStart(25)}|${data[key]}%c${add}`, "color:DarkOrange;");
});
}
}
dGroupEnd();
}
function setSortingButton() {
let pos = document.getElementById(id_list.mainbox);
let input_list = [
newSetting("(Sort) Descending", "sort_setting"), newSeparate(),
newSetting("(Sort) Numeric", "sort_numeric"), newSeparate(),
newSetting("(Sort) Ignore Punctuation", "sort_ignore_punctuation"), newSeparate(),
newSetting("Copy Title When Download", "dl_and_copy"), newSeparate(),
newSetting("Auto Enable Pure Text", "auto_enable_puretext"), newSeparate(),
newSetting("Auto Enable Fix Title", "auto_fix_title"), newLine(),
].flat();
input_list.every(node => { if (node.tagName == "INPUT") { node.addEventListener("change", updateSetting); } });
let form = Object.assign(
document.createElement("form"),
{
id: "exhddl_setting_form_prevent_send_data",
onsubmit: (event) => {
event.preventDefault();
return false;
},
}
);
appendAllChild(form, input_list);
let nodelist = [form];
let sort_jp = document.createElement("div");
sort_jp.textContent = "Sort by JP";
sort_jp.style = "display: inline-block;";
let sort_en = document.createElement("div");
sort_en.textContent = "Sort by EN";
sort_en.style = "display: inline-block;";
nodelist.push([
sort_jp,
newSeparate(),
newButton("exhddl_sort_by_title_jp", "Full Title", style_list.top_button, () => { sortGalleryByKey("title_jpn"); }),
newSeparate(),
newButton("exhddl_sort_by_title_pure", "ignore Prefix/Group/End", style_list.top_button, () => { sortGalleryByKey("title_pure_jpn"); }),
newSeparate(),
newButton("exhddl_sort_by_title_no_group", "ignore Prefix/Group", style_list.top_button, () => { sortGalleryByKey("title_no_group_jpn"); }),
newSeparate(),
newButton("exhddl_sort_by_title_no_event", "ignore Prefix", style_list.top_button, () => { sortGalleryByKey("title_no_event_jpn"); }),
newLine(),
sort_en,
newSeparate(),
newButton("exhddl_sort_by_title_en", "Full Title", style_list.top_button, () => { sortGalleryByKey("title"); }),
newSeparate(),
newButton("exhddl_sort_by_title_pure", "ignore Prefix/Group/End", style_list.top_button, () => { sortGalleryByKey("title_pure"); }),
newSeparate(),
newButton("exhddl_sort_by_title_no_group", "ignore Prefix/Group", style_list.top_button, () => { sortGalleryByKey("title_no_group"); }),
newSeparate(),
newButton("exhddl_sort_by_title_no_event", "ignore Prefix", style_list.top_button, () => { sortGalleryByKey("title_no_event"); }),
newLine(),
newButton("exhddl_sort_by_date", "Date (Default)", style_list.top_button, () => { sortGalleryByKey("posted"); }),
newSeparate(),
newButton("exhddl_sort_by_prefix", "Event", style_list.top_button, () => { sortGalleryByKey("title_prefix"); }),
newSeparate(),
newButton("exhddl_sort_by_artist", "Artist", style_list.top_button, () => { sortGalleryByKey("artist"); }),
newSeparate(),
newButton("exhddl_sort_by_group", "Group/Circle", style_list.top_button, () => { sortGalleryByKey("group"); }),
newSeparate(),
newButton("exhddl_sort_by_category", "Category", style_list.top_button, () => { sortGalleryByKey("category"); }),
newSeparate(),
newButton("exhddl_sort_by_page", "Page Count", style_list.top_button, () => { sortGalleryByKey("filecount"); }),
newSeparate(),
newButton("exhddl_sort_by_rating", "Rating", style_list.top_button, () => { sortGalleryByKey("rating"); }),
newSeparate(),
newButton("exhddl_sort_by_uploader", "Uploader", style_list.top_button, () => { sortGalleryByKey("uploader"); }),
newLine(),
/*
newButton("exhddl_sort_by_ex", "???", style_list.top_button, () => { sortGalleryByKey("expunged"); }),
newLine(),
*/
newButton("exhddl_fix_title", "Fix/Unfix Event in Title (Search in torrents/same title gallery)", style_list.top_button, () => { fixTitlePrefix(); }),
newSeparate(),
newButton("exhddl_exclude_buttons", "Show Exclude List", style_list.top_button, () => { setExclude(); }),
]);
nodelist = nodelist.flat();
pos.querySelector("span").remove(); // remove loading message
appendAllChild(pos, nodelist);
function newSetting(lable_text, id_key) {
return [
Object.assign(document.createElement("input"), {
type: "checkbox",
id: id_list[id_key],
checked: getGMValue(id_key),
}),
Object.assign(document.createElement("label"), {
htmlFor: id_list[id_key],
textContent: lable_text,
})
];
}
function updateSetting() {
let skip_list = Object.keys(default_value).filter(key => typeof (default_value[key]) != "boolean");
let updatelist = Object.keys(key_list).filter(key => !skip_list.includes(key));
let [info, style] = [[], []];
for (let key of updatelist) {
let value = document.getElementById(id_list[key]).checked;
GM_setValue(key_list[key], value);
info.push(`[${key}]:%c${value}`);
style.push("", value ? "color:DeepSkyBlue" : "color:DeepPink");
}
print(`${m}updateSetting | %c${info.join(" %c| ")}`, ...style);
}
function sortGalleryByKey(key = "") {
if (!key) return print("no key");
dTime(sortGalleryByKey.name);
dGroup();
let sortedID = getSortedID(gdata, key);
let container = document.querySelector(".itg.gld");
dGroupEnd();
if (container) {
sortedID.forEach(id => {
let gallery = document.querySelector(`[gid="${id}"]`);
if (gallery) container.appendChild(gallery);
});
} else {
print(`${m}container not found`);
}
dTimeEnd(sortGalleryByKey.name);
function getSortedID(gdata, sort_key) {
let descending = document.getElementById(id_list.sort_setting).checked;
return gdata.sort((a, b) => {
return descending ? naturalSort(b[sort_key], a[sort_key]) : naturalSort(a[sort_key], b[sort_key]);
}).map(g => {
dPrint(`${String(g.gid).padStart(10)} | ${g[sort_key]}`);
return g.gid;
});
}
function naturalSort(a, b) {
let numeric = document.getElementById(id_list.sort_numeric).checked;
let ignore_punctuation = document.getElementById(id_list.sort_ignore_punctuation).checked;
return String(a).localeCompare(String(b), navigator.languages[0] || navigator.language, { numeric: numeric, ignorePunctuation: ignore_punctuation });
}
}
function setExclude() {
let self = document.getElementById("exhddl_exclude_buttons");
self.disabled = true;
self.removeAttribute("onclick");
forEachGallery(gallery => {
let gid = gallery.getAttribute("gid");
let data = gdata.find(gallery_data => gallery_data.gid == gid);
let pos = gallery.querySelector(".gallery_box");
let tag_list = [
`uploader:${data.uploader.trim()}`,
data.language,
data.artist,
data.group,
data.female,
data.male,
data.parody,
data.character,
].flat();
let select = newSelect(tag_list);
Object.assign(select.style, style_list.gallery_button);
let span = newSpan("Exclude");
Object.assign(span.style, style_list.gallery_button);
appendAllChild(pos, [
newLine(),
span, newLine(),
select, newLine(),
newButton(`exhddl_exclude_${gid}`, "Add/Remove", style_list.gallery_button, () => { updateExcludeList(gid); }),
]);
});
function newSelect(data_list) {
let select = document.createElement("select");
if (data_list.length == 0) return select;
data_list.forEach(data => { if (data.length > 0) select.appendChild(newOption(data)); });
return select;
}
function newOption(text) {
return Object.assign(document.createElement("option"), { textContent: text });
}
function updateExcludeList(gid) {
let gallery = document.querySelector(`[gid="${gid}"]`);
if (!gallery) return;
let up = "uploader:";
let exclude = gallery.querySelector("select").selectedOptions[0].textContent;
let update_key = exclude.includes(up) ? "exclude_uploader" : "exclude_tag";
if (exclude.includes(up)) exclude = exclude.replace(up, "");
updateByKey(update_key, exclude);
function updateByKey(key, value) {
let list = getGMList(key);
let index = list.indexOf(value);
if (index == -1) {
list.push(value);
print(`${m}add [${value}] to list [${key}]`);
} else {
list.splice(index, 1);
print(`${m}remove [${value}] from list [${key}]`);
}
setGMData(key_list[key], list);
//GM_setValue(key_list[key], list.join());
}
}
}
}
function setCopyTitle(gallery) {
let gid = gallery.getAttribute("gid");
let pos = gallery.querySelector(`#gallery_status_${gid}`);
let button = newButton(`copy_title_${gid}`, "Copy Title", style_list.gallery_button, function () {
navigator.clipboard.writeText(repalceForbiddenChar(document.querySelector(`[gid="${gid}"] .glname`).textContent.trim()));
});
pos.insertAdjacentElement("beforebegin", button);
pos.insertAdjacentElement("beforebegin", newLine());
}
function setShowTorrent(gallery) {
let gid = gallery.getAttribute("gid");
let torrent_list = getTorrentList(gid);
let pos = gallery.querySelector(".gallery_box");
let button = newButton(`t_title_${gid}`, "Show torrent List", style_list.gallery_button, function () {
let torrent_list = document.querySelector(`[gid="${gid}"] .torrent_title`);
torrent_list.style.display = (torrent_list.style.display == "none") ? "" : "none";
});
pos.insertAdjacentElement("beforeend", newLine());
pos.insertAdjacentElement("beforeend", button);
if (torrent_list) {
let box = Object.assign(document.createElement("div"), { className: "torrent_title", style: "display:none" });
torrent_list.forEach(torrent => { box.appendChild(Object.assign(newSpan(torrent), { className: "puretext", })); });
pos.insertAdjacentElement("beforebegin", box);
} else {
button.disabled = true;
button.removeAttribute("onclick");
}
}
function extracContainer() {
let [start, end] = ["", ""];
for (let c of container_list) {
start += c[0];
end += c[1];
}
return [start, end];
}
function containerRegexGenerator() {
let reg = [];
let esc_reg = /[-\/\\^$*+?.()|[\]{}]/g;
for (let c of container_list) {
let esc = c.replace(esc_reg, "\\$&");
let end = esc.slice(esc.length / 2);
let start = esc.replace(end, "");
reg.push(`${start}[^${esc}]*${end}`);
}
return `(${reg.join("|")})`;
}
function forEachGallery(handler) {
for (let index = 0, all = selectAllGallery(), length = all.length; index < length; index++) handler(all[index]);
}
function selectAllGallery() {
return document.querySelectorAll(".gl1t");
}
function print(...any) {
if (debug_message) console.log(...any);
}
function time(tag = "") {
if (debug_message) return tag ? console.time(tag) : console.time();
}
function timeEnd(tag = "") {
if (debug_message) return tag ? console.timeEnd(tag) : console.timeEnd();
}
function group() {
if (debug_message) return console.groupCollapsed();
}
function groupEnd() {
if (debug_message) return console.groupEnd();
}
function dPrint(...any) {
if (debug_message && debug_adv) console.log(...any);
}
function dTime(tag = "") {
if (debug_message && debug_adv) return tag ? console.time(tag) : console.time();
}
function dTimeEnd(tag = "") {
if (debug_message && debug_adv) return tag ? console.timeEnd(tag) : console.timeEnd();
}
function dGroup() {
if (debug_message && debug_adv) return console.groupCollapsed();
}
function dGroupEnd() {
if (debug_message && debug_adv) return console.groupEnd();
}
function timerMananger() {
document.addEventListener("visibilitychange", () => {
if (timer_list.length > 0) {
let pause = (document.visibilityState === "visible") ? false : true;
for (let timer of timer_list) {
if (!pause) {
timer.id = setInterval(timer.handler, timer.delay);
//dPrint(`${m}start timer[${timer.note}] id:${timer.id}`);
} else {
clearInterval(timer.id);
//dPrint(`${m}stop timer[${timer.note}] id:${timer.id}`);
}
}
}
});
}
function addTimer(handler, delay, note = "") {
if (note == "") note = handler.name;
timer_list.push({
id: setInterval(handler, delay),
handler: handler,
delay: delay,
note: note,
});
}
function findEleByText(css_selector, string) {
let es = document.querySelectorAll(css_selector);
for (let ele of es) {
if (ele.textContent.includes(string)) return ele;
}
return false;
}
function visitGallery(link) {
if (link) {
// trigger :visited
let current = window.location.href;
history.pushState({}, "", link); // add link to history. this will change current winodw link.
print(`${m}add history, link:${window.location.href}`);
history.pushState({}, "", current); // change it back.
}
}
function addToDownloadedList(gid) {
gid = `${gid}`; // convert to string
let downloaded_list = getGMList("dl_list");
if (downloaded_list.includes(gid)) return print(`${m}[${gid}] is already in the list, abort`);
if (downloaded_list.length > gallery_data_limit.max_size && gallery_data_limit.max_size != 0) {
let count = 0;
while (downloaded_list.length > gallery_data_limit.max_size) {
let r = downloaded_list.shift();
print(`${m}%creach limit, remove [${r}]`, "color:OrangeRed;");
count++;
if (count > 100) return print(`${m}unknow error while removing old data, script stop`);
}
}
downloaded_list.push(gid);
let list_length = downloaded_list.length;
setGMData(key_list.dl_list, downloaded_list);
//downloaded_list = downloaded_list.join();
//GM_setValue(key_list.dl_list, downloaded_list);
print(`${m}add [${gid}] to list. [list_size:${downloaded_list.join().length}, list_length:${list_length}]`);
}
function my_popUp(URL, w, h) {
window.open(
URL,
`_pu${Math.random().toString().replace(/0\./, "")}`,
`toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0` +