-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
dlsite_title_reformat.user.js
1567 lines (1443 loc) · 60.6 KB
/
dlsite_title_reformat.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 dlsite title reformat
// @namespace https://github.com/x94fujo6rpg/SomeTampermonkeyScripts
// @updateURL https://github.com/x94fujo6rpg/SomeTampermonkeyScripts/raw/master/dlsite_title_reformat.user.js
// @downloadURL https://github.com/x94fujo6rpg/SomeTampermonkeyScripts/raw/master/dlsite_title_reformat.user.js
// @version 0.92
// @description remove title link / remove excess text / custom title format / click button to copy
// @author x94fujo6
// @match https://www.dlsite.com/*
// @grant GM_getValue
// @grant GM_setValue
// ==/UserScript==
/* jshint esversion: 9 */
(function () {
'use strict';
let debug = true;
let formatted_data = {
id: "",
title_original: "",
title_formatted: "",
circle: "",
Year: "",
year: "",
month: "",
day: "",
series: "",
author: "",
scenario: "",
illust: "",
cv: "",
age: "",
type: "",
tags: "",
};
let data_list = Object.keys(formatted_data);
let updateid;
const forbidden = `<>:"/|?*\\`;
const replacer = `<>:”/|?*\`;
//-----------------------------------------------------
const key_format = "format_seting";
const key_adv = "format_adv";
const key_f2h = "format_f2h";
const key_half = "format_falf";
const key_full = "format_full";
const key_show_ot = "format_show_ot";
const key_show_ft = "format_show_ft";
const key_sep = "format_sep";
//-----------------------------------------------------
const default_format = "%id% %title_formatted%";
const default_adv = false;
const default_f2h = true;
const default_half = "1234567890()[]{}~!@#$%^&_+-=;':,.()~";
const default_full = "1234567890()[]{}~!@#$%︿&_+-=;’:,.()〜";
const default_show_ot = true;
const default_show_ft = true;
const default_sep = "、";
//-----------------------------------------------------
let setting_format = GM_getValue(key_format, default_format);
let setting_adv = GM_getValue(key_adv, default_adv);
let setting_f2h = GM_getValue(key_f2h, default_f2h);
let setting_half = default_half;
let setting_full = default_full;
let setting_show_ot = GM_getValue(key_show_ot, default_show_ot);
let setting_show_ft = GM_getValue(key_show_ft, default_show_ft);
let setting_sep = GM_getValue(key_sep, default_sep);
//-----------------------------------------------------
debug_msg("load setting");
debug_msg(`${key_format}: ${setting_format}`);
debug_msg(`${key_adv}: ${setting_adv}`);
debug_msg(`${key_f2h}: ${setting_f2h}`);
debug_msg(`${key_show_ot}: ${setting_show_ot}`);
debug_msg(`${key_show_ft}: ${setting_show_ft}`);
debug_msg(`${key_sep}: ${setting_sep}`);
//-----------------------------------------------------
const container_list = [
"()", "[]", "{}", "()", "<>",
"[]", "{}", "【】", "『』", "《》", "〈〉", "「」"
];
const regesc = t => t.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&");
const [container_start, container_end] = extracContainer();
const reg_container = containerRegexGenerator();
const reg_excess = new RegExp(`^\\s*${reg_container}\\s*|\\s*${reg_container}\\s*$`, "g");
const reg_blank = /[\s ]{2,}/g;
const reg_muti_blank = /[\s \n\t]+/g;
const reg_ascii = /[\x00-\x7F]/g;
const reg_until_number = /[^\d]*[\d]+/;
const reg_time = new RegExp(`[${regesc(container_start)}]*(\\d+:\\d+|約\\d*時*間*\\d+分\\d*秒*|合*計*\\d+分\\d+秒|\\d+時間\\d+分\\d*秒*)[${regesc(container_end)}]*`, "g");
/*
\u0021-\u002f !"#$%&'()*+,-./
\u003a-\u0040 :;<=>?@
\u005b-\u0060 [\]^_`
\u007b-\u007e {|}~
\uff5f-\uff63 ⦅⦆。「」
*/
const reg_non_word_at_start = /^[\u0021-\u002f\u003a-\u0040\u005b-\u0060\u007b-\u007e\uff5f-\uff63 \s]/;
const reg_text_start = /^(トラック|track)/;
const reg_non_track = /(?<=[mM][pP]|[kK][uU])\d+|\d*\.*\d+(?=[kK][bB]|[kK][hH][zZ]|[bB][iI][tT]|[dD][iI][oO])/g;
const reg_non_track2 = /([mM][pP]|[kK][uU])\d+|\d*\.*\d+([kK][bB]|[kK][hH][zZ]|[bB][iI][tT]|[dD][iI][oO])/g;
const max_depth = 10;
debug_msg("container_start | ", container_start);
debug_msg("container_end | ", container_end);
debug_msg("reg_container | ", reg_container);
debug_msg("reg_excess | ", reg_excess);
debug_msg("reg_time | ", reg_time);
window.document.body.onload = main();
async function main() {
let
link = window.location.href,
match_list = [
"/circle/profile/",
"/fsr",
"/genres/works",
];
await wait_tab();
if (link.includes("/product_id/")) {
myCss();
productHandler();
fix_switch_link();
return debug_msg(productHandler.name);
}
if (match_list.some(key => link.includes(key))) {
myCss();
waitHTML(".display_normal,.display_block", () => searchHandler());
fix_switch_link();
return debug_msg("match link");
}
if (link.includes("/announce/list")) {
debug_msg("announce list");
return wait_for_fav();
}
return debug_msg("not in support list");
}
function wait_tab() {
return new Promise(resolve => {
if (document.visibilityState === "visible") return resolve();
debug_msg("tab in background, script paused");
document.addEventListener("visibilitychange", () => {
if (document.visibilityState === "visible") { debug_msg("script unpaused"); return resolve(); }
});
});
}
function wait_for_fav(max_retry = 10) {
let
list = document.querySelector(".n_worklist"),
ck = [...list.children].every(item => item.querySelector(".work_sales_info") ? true : false);
if (max_retry <= 0) return debug_msg(`max retries exceeded, abort`);
if (!ck) return retry();
debug_msg(`found fav`);
return setTimeout(sort_ann_list, 1500);
function retry() {
debug_msg(`wait for ele, retry ${max_retry}`);
setTimeout(() => wait_for_fav(--max_retry), 1000);
}
}
function sort_ann_list() {
let
container = document.querySelector(".n_worklist"),
item = container.querySelectorAll("div.n_worklist_item"),
data = [], no_fav = item.length,
likes = 0,
type = "unknown";
data = [...item].map(i => {
likes = i.querySelector(".work_sales_info>div>span");
type = i.querySelector(".work_category");
if (!likes) no_fav++;
return {
item: i,
likes: likes ? parseInt(likes.textContent) : 0,
type: type ? [...type.classList].pop() : "_unknown",
};
});
data = sort_by(data, "likes");
data = sort_by(data, "type");
data.forEach(i => container.appendChild(i.item));
}
function sort_by(data, key = "", dec = true) {
if (dec) {
return data.sort((a, b) => s(b[key], a[key]));
} else {
return data.sort((a, b) => s(a[key], b[key]));
}
function s(a, b) {
if (a < b) return -1;
if (a > b) return 1;
return 0;
}
}
function extracContainer() {
let start = "", end = "";
container_list.forEach(c => {
start += c[0];
end += c[1];
});
return [start, end];
}
function containerRegexGenerator() {
let reg = [];
container_list.forEach(c => {
let esc = regesc(c);
let end = esc.slice(esc.length / 2);
let start = esc.replace(end, "");
reg.push(`${start}[^${esc}]*${end}`);
});
return `(${reg.join("|")})`;
}
function newCheckbox(id, onclick) {
let ck = document.createElement("input");
ck.type = "checkbox";
ck.id = id;
ck.onclick = onclick;
return ck;
}
function newLable(forid = "", text = "", className = "dtr_textsize05") {
let lable = document.createElement("label");
lable.className = className;
lable.htmlFor = forid;
lable.textContent = text;
return lable;
}
function waitHTML(css_selector, run) {
let id = setInterval(() => {
if (document.querySelectorAll(css_selector).length) {
clearInterval(id);
run();
console.log(`found [${css_selector}]`);
} else {
console.log(`[${css_selector}] not found`);
}
}, 1000);
}
function searchHandler() {
let display_list = document.querySelector(".display_normal.on"),
display_grid = document.querySelector(".display_block.on");
console.log(`[${searchHandler.name}] display_list:${Boolean(display_list)}, display_grid:${Boolean(display_grid)}`);
setRefreshPage();
if (display_list) {
listHandler();
} else if (display_grid) {
gridHandler();
}
function setRefreshPage() {
document.querySelectorAll(".display_normal,.display_block")
.forEach(ele => ele.addEventListener("click", () => {
let href_o = document.location.href,
timer_id = setInterval(() => {
let href_new = document.location.href;
if (href_new != href_o) {
clearInterval(timer_id);
document.location.reload();
}
}, 500);
}));
}
}
function getMutipleDataToList(pos, type = "a") {
let list = [];
let es = pos.querySelectorAll(type);
if (es) {
es.forEach(e => list.push(e.textContent));
list = stringFormatter(list.join(setting_sep));
return list;
} else {
return "";
}
}
function fix_switch_link() {
let links = document.querySelectorAll(".floorNavLink-item a");
if (!links || links.length == 0) return;
let current = window.location.href.replace(/.+www\.dlsite\.com\/\w+\/(.+)/, "$1");
links.forEach(link => link.href += current);
}
const to_full_size_image = url => url.replace(/(.*)resize(.*)_\d+x\d+(.*)/, "$1modpub$2$3");
const getCover = (id) => {
let ele = document.querySelector(`#_link_${id} img`);
if (ele) {
// grid / list
if (ele.src.includes("data:image")) {
return to_full_size_image(`https:${ele.getAttribute("data-src")}`);
} else {
return to_full_size_image(ele.src);
}
}
ele = document.querySelector(`img[src*="${id}_img_main"`);
if (ele) {
return to_full_size_image(ele.src);
} else {
return false;
}
};
function newCoverUrl(id, is_b = false) {
let url = getCover(id);
if (url) {
if (is_b) {
return newCopyButton(url, "Cover(Url)");
} else {
return url;
}
} else {
url = "no cover";
if (is_b) {
return newCopyButton(url, url);
} else {
return url;
}
}
}
function newCoverDownload(id) {
let b = document.createElement("button");
b.id = "dtr_cover_dl";
b.textContent = "Cover(DL)";
b.onclick = () => {
let url = getCover(id);
let rq = new XMLHttpRequest();
rq.open("GET", url, true);
rq.responseType = "blob";
rq.onload = () => dl(rq.response, url.match(/[Rr][Jj]\d+[^\/]*\.[a-zA-Z]+/)[0]);
rq.send();
function dl(blob, filename) {
let file_url = window.URL.createObjectURL(blob);
let a = document.querySelector("#dtr_img_dl_url");
if (!a) {
a = document.createElement("a");
a.id = "dtr_img_dl_url";
document.body.insertAdjacentElement("afterbegin", a);
}
a.href = file_url;
a.download = filename;
a.click();
window.URL.revokeObjectURL(file_url);
}
};
return b;
}
function addSortButton() {
let classname = "reSortByID";
let ele = document.querySelector(`.${classname}`);
if (ele) return;
let pos = document.querySelector(".sort_box .status_select");
if (!pos) return;
ele = document.createElement("div");
ele.textContent = "SortByID:";
ele.style = "margin: 0.5rem;";
pos.appendChild(ele);
ele = document.createElement("button");
ele.textContent = "Descent";
ele.className = classname;
ele.onclick = () => sortByID(true);
pos.appendChild(ele);
ele = document.createElement("button");
ele.textContent = "Ascent";
ele.className = classname;
ele.onclick = () => sortByID();
pos.appendChild(ele);
ele = document.createElement("div");
ele.textContent = "SortByType:";
ele.style = "margin: 0.5rem;";
pos.appendChild(ele);
ele = document.createElement("button");
ele.textContent = "Descent";
ele.className = classname;
ele.onclick = () => sortByType(true);
pos.appendChild(ele);
ele = document.createElement("button");
ele.textContent = "Ascent";
ele.className = classname;
ele.onclick = () => sortByType();
pos.appendChild(ele);
}
function sortByType(descent = false) {
let grid_mode = document.querySelector(".display_block.on") ? true : false;
let eles = document.querySelectorAll(grid_mode ? ".search_result_img_box_inner" : "#search_result_list tr");
let pos = document.querySelector(grid_mode ? "#search_result_img_box" : "#search_result_list tbody");
let data = [], type = "";
data = [...eles].map(e => {
type = e.querySelector(".work_category");
return {
ele: e,
type: type ? [...type.classList].pop() : "_unknown",
};
});
data = sort_by(data, "type", descent);
data.forEach(item => pos.appendChild(item.ele));
}
async function sortByID(descent = false) {
console.time(sortByID.name);
let grid_mode = document.querySelector(".display_block.on") ? true : false;
let eles = document.querySelectorAll(grid_mode ? ".search_result_img_box_inner" : "#search_result_list tr");
let pos = document.querySelector(grid_mode ? "#search_result_img_box" : "#search_result_list tbody");
let attrname = "sort_id";
await new Promise(r => {
eles.forEach(e => {
let id = e.querySelector(grid_mode ? ".search_img" : ".work_thumb_inner")
.id.replace("_link_", "");
e.setAttribute(attrname, id);
});
r();
});
let id_list = [...eles].map(e => e.getAttribute(attrname).replace("RJ", ""));
id_list = id_list.sort((a, b) => descent ? b - a : a - b);
console.log(id_list);
id_list.forEach(id => pos.appendChild(document.querySelector(`[${attrname}="RJ${id}"]`)));
console.timeEnd(sortByID.name);
}
function listHandler() {
console.time(listHandler.name);
let list = document.querySelectorAll("#search_result_list");
if (!list) {
debug_msg("list not found");
} else {
list = list[list.length - 1].querySelectorAll("tr");
list.forEach(tr => {
let id,
title_o_text, title_f_text,
circle_text,
cv, tags,
pos, newbox, node_list;
if (tr.querySelector(".page_no")) return;
pos = tr.querySelector("dl");
id = tr.querySelector(".work_thumb a[href*='/product_id/']").id.replace("_link_", "");
title_o_text = pos.querySelector(".work_name a[href*='/product_id/']").textContent;
title_f_text = stringFormatter(title_o_text);
circle_text = stringFormatter(pos.querySelector(".maker_name a").textContent);
node_list = [
newLine(),
newCopyButton(title_o_text), newLine(),
newCopyButton(title_f_text), newLine(),
newCopyButton(id), newSeparate(),
newCoverDownload(id), newSeparate(),
newCoverUrl(id, true), newSeparate(),
newCopyButton(circle_text),
];
if (title_o_text == title_f_text) node_list.splice(3, 2);
newbox = appendAll(document.createElement("dd"), node_list);
cv = pos.querySelector(".author");
cv = cv ? getMutipleDataToList(cv) : "";
tags = pos.querySelector(".search_tag");
tags = tags ? getMutipleDataToList(tags) : "";
if (cv != "") appendAll(newbox, [newSeparate(), newCopyButton(cv, "CV/Author")]);
if (tags != "") appendAll(newbox, [newSeparate(), newCopyButton(tags, "Tags")]);
pos.appendChild(newbox);
});
}
console.timeEnd(listHandler.name);
addSortButton();
}
function gridHandler() {
console.time(gridHandler.name);
let list = document.querySelectorAll(".search_result_img_box_inner");
if (!list) {
debug_msg("list not found");
} else {
let w = document.createElement("div");
w.appendChild(newSpan("Can't get full CV/Author list in grid view. If you need it, switch to list view.", "dtr_list_w_text"));
document.querySelector(".sort_box").insertAdjacentElement("afterend", w);
list.forEach(box => {
let id,
title_o_text, title_f_text,
circle_text,
cv,
pos, newbox, node_list;
pos = box.querySelector(".work_price_wrap");
id = box.querySelector(".search_img.work_thumb").id.replace("_link_", "");
title_o_text = box.querySelector(".work_name a").textContent;
title_f_text = stringFormatter(title_o_text);
circle_text = stringFormatter(box.querySelector(".maker_name a").textContent);
cv = box.querySelector(".author");
cv = cv ? getMutipleDataToList(cv) : "";
node_list = [
newCopyButton(id), newLine(),
newCoverDownload(id), newSeparate(), newCoverUrl(id, true), newLine(),
newCopyButton(title_o_text, "Original"), newSeparate(),
newCopyButton(title_f_text, "Formatted"), newLine(),
newCopyButton(circle_text, "Circle"),
];
if (title_o_text == title_f_text) node_list.splice(5, 2);
newbox = appendAll(document.createElement("dd"), node_list);
if (cv != "") appendAll(newbox, [newSeparate(), newCopyButton(cv, "CV/Author")]);
pos.insertAdjacentElement("beforebegin", newbox);
});
}
console.timeEnd(gridHandler.name);
addSortButton();
}
function appendAll(node, nodeList) {
nodeList.forEach(e => node.appendChild(e));
return node;
}
function saveSetting() {
debug_msg("[saveSetting]");
if (setting_format.length > 0) {
GM_setValue(key_format, setting_format);
debug_msg(`saved ${key_format}: ${setting_format}`);
} else {
debug_msg(`${key_format} not saved cus is empty`);
}
GM_setValue(key_adv, setting_adv);
debug_msg(`saved ${key_adv}: ${setting_adv}`);
GM_setValue(key_f2h, setting_f2h);
debug_msg(`saved ${key_f2h}: ${setting_f2h}`);
GM_setValue(key_show_ot, setting_show_ot);
debug_msg(`saved ${key_show_ot}: ${setting_show_ot}`);
GM_setValue(key_show_ft, setting_show_ft);
debug_msg(`saved ${key_show_ft}: ${setting_show_ft}`);
GM_setValue(key_sep, setting_sep);
debug_msg(`saved ${key_sep}: ${setting_sep}`);
}
function getData() {
console.time(getData.name);
//------------------------------------------------------
let sitedata = contents.detail[0];
let [Y, m, d] = sitedata.regist_date.split("/");
let y = Y.slice(2);
let circle = document.getElementById("work_maker").querySelector("span.maker_name[itemprop='brand']");
let circle_text = stringFormatter(circle.querySelector("a").textContent);
circle.insertAdjacentElement("afterbegin", newCopyButton(circle_text, "Copy"));
Object.assign(formatted_data, {
id: sitedata.id,
title_original: sitedata.name,
title_formatted: stringFormatter(sitedata.name),
circle: circle_text,
Year: Y,
year: y,
month: m,
day: d,
});
//------------------------------------------------------
let datapart = document.getElementById("work_right_inner").querySelectorAll("th");
let parselist = {
series: ["Series name", "シリーズ名", "系列名", "系列名"],
author: ["Author", "作者", "作者", "作者"],
scenario: ["Scenario", "シナリオ", "剧情", "劇本"],
illust: ["Illustration", "イラスト", "插画", "插畫"],
cv: ["Voice Actor", "声優", "声优", "聲優"],
age: ["Age", "年齢指定", "年龄指定", "年齡指定"],
type: ["Product format", "作品形式", "作品类型", "作品形式"],
};
let release = ["Release date", "販売日", "贩卖日", "販賣日"];
let text;
let all = [];
datapart.forEach(th => {
for (let key in parselist) {
text = th.textContent;
if (isInList(text, parselist[key], formatted_data[key])) {
all = [];
if (key == ("age" || "type")) {
th.parentNode.querySelectorAll("span").forEach(span => all.push(span.textContent));
} else {
th.parentNode.querySelectorAll("a").forEach(a => all.push(a.textContent));
}
formatted_data[key] = stringFormatter(all.join(setting_sep));
insertCopyDataButton(th, formatted_data[key]);
delete parselist[key];
break;
} else if (release) {
if (release.some(t => text.includes(t))) {
let date = `${Y}${m}${d}`;
insertCopyDataButton(th, date, date);
date = `${y}${m}${d}`;
insertCopyDataButton(th, date, date);
release = false;
}
}
}
});
//------------------------------------------------------
let tagpart = document.querySelector("#work_right_inner div.main_genre");
let insertpos = tagpart;
tagpart = tagpart.querySelectorAll("a");
let tags = [];
tagpart.forEach(a => tags.push(a.textContent));
formatted_data.tags = stringFormatter(tags.join(setting_sep));
insertCopyDataButton(insertpos, formatted_data.tags);
console.timeEnd(getData.name);
}
function insertCopyDataButton(ele, copytext = "", btext = "Copy") {
ele = searchNodeNameInParents(ele, "TR");
if (!ele) return;
let pos = ele.querySelector("div");
if (!pos) pos = ele.querySelector("td");
if (!pos) return;
pos.insertAdjacentElement("afterbegin", newCopyButton(copytext, btext));
}
function searchNodeNameInParents(ele, nodename = "") {
if (!ele || !nodename) return false;
nodename = nodename.toUpperCase();
let count = 0;
while (true) {
ele = ele.parentNode;
count++;
if (!ele || count > 100) return false;
if (ele.nodeName == nodename) break;
}
return ele;
}
function isInList(text, list, data) {
if (!data) if (list.some(t => text.includes(t))) return true;
return false;
}
function stringFormatter(text) {
text = removeExcess(text);
if (setting_f2h) text = toHalfWidth(text);
text = repalceForbiddenChar(text);
return text;
}
function removeExcess(text) {
let o_text = text;
// remove excess text
let count = 0;
while (count < 100 && text.match(reg_excess)) {
text = text.replace(reg_excess, "");
count++;
}
if (text.length == 0) text = o_text;
// remove container if it at start or end
count = 0;
while (count < 100) {
let index_start = container_start.indexOf(text[0]);
if (index_start != -1) {
if (container_end[index_start] == text[text.length - 1]) {
// found start & end
text = text.slice(1, text.length - 1).trim();
debug_msg(`[removeExcess] remove container:"${text}"`);
} else if (!text.includes(container_end[index_start])) {
// found start but no end
text = text.slice(1).trim();
debug_msg(`[removeExcess] remove start:"${text}"`);
}
}
let index_end = container_end.indexOf(text[text.length - 1]);
if (index_end != -1) {
if (!text.includes(container_start[index_end])) {
// found end but no start
text = text.slice(0, text.length - 1).trim();
debug_msg(`[removeExcess] remove end:"${text}"`);
}
}
if (index_start == -1 && index_end == -1) break;
count++;
}
text = text.replace(reg_blank, " ");
text = text.length > 0 ? text : o_text;
debug_data(`o:[${o_text}]\np:[${text}]`);
return text;
}
function toHalfWidth(text) {
for (let i in setting_full) {
text = text.replace(new RegExp(setting_full[i], "g"), setting_half[i]);
}
return text;
}
function repalceForbiddenChar(text) {
for (let index in forbidden) {
text = text.replace(new RegExp(regesc(forbidden[index]), "g"), replacer[index]);
}
return text;
}
function updateSetting() {
let s = document.getElementById("format_title_setting");
let p = document.getElementById("format_title_preview");
let cs = document.getElementById("format_title_custom_span");
let cb = document.getElementById("format_title_custom_button");
if (s.value.length > 0) {
if (setting_format != s.value) {
setting_format = s.value;
let formatted = parseFormatString(setting_format);
cs.textContent = p.value = formatted;
cb.onclick = () => navigator.clipboard.writeText(formatted);
}
}
let sep = document.getElementById(`dtr_${key_sep}`);
setting_sep = sep.value;
}
function parseFormatString(string = "") {
let formatted_text = string;
data_list.forEach(key => {
formatted_text = formatted_text.replace(new RegExp(`%${key}%`, "g"), formatted_data[key]);
});
formatted_text = repalceForbiddenChar(formatted_text);
return formatted_text;
}
function setting() {
console.time(setting.name);
//------------------------------------------------------
let pos = document.getElementById("work_name");
let button = document.createElement("button");
Object.assign(button, {
textContent: "Open Setting",
value: "open",
onclick: function () {
let ele = document.getElementById("format_setting_ui");
if (this.value === "close") {
ele.style.display = "none";
this.value = "open";
this.textContent = "Open Setting";
clearInterval(updateid);
} else {
ele.style.display = "";
this.value = "close";
this.textContent = "Close Setting";
updateid = setInterval(updateSetting, 100);
}
},
});
//------------------------------------------------------
let box = document.createElement("div");
box.id = "format_setting_ui";
box.className = "dtr_setting_box";
box.style.display = "none";
let textarea;
//------------------------------------------------------
pos.insertAdjacentElement("afterbegin", box);
pos.insertAdjacentElement("afterbegin", newLine());
pos.insertAdjacentElement("afterbegin", button);
//------------------------------------------------------
button = document.createElement("button");
let mode = setting_adv ? "on" : "off";
Object.assign(button, {
className: "dtr_textsize05",
id: "format_title_setting_advance_model",
textContent: `Advance mode: ${mode}`,
value: mode,
onclick: function () {
let t = document.getElementById("format_title_setting");
if (this.value === "off") {
Object.assign(this, {
value: "on",
textContent: "Advance mode: on",
});
t.readOnly = false;
setting_adv = true;
} else {
Object.assign(this, {
value: "off",
textContent: "Advance mode: off",
});
t.readOnly = true;
setting_adv = false;
}
}
});
box.appendChild(button);
box.appendChild(newSpan(" enable this to direct edit format setting",
"dtr_textsize05 dtr_setting_w_text"));
appendNewLine(box);
//------------------------------------------------------
// all data
data_list.forEach(s => box.appendChild(newDataButton(`+${s}`, `%${s}%`)));
appendNewLine(box);
//------------------------------------------------------
textarea = document.createElement("textarea");
textarea.className = "dtr_textsize05 dtr_max_width";
textarea.id = "format_title_setting";
textarea.rows = 1;
textarea.value = setting_format;
textarea.readOnly = !setting_adv;
box.appendChild(newSpan("Format setting:"));
appendNewLine(box);
box.appendChild(textarea);
appendNewLine(box);
textarea = document.createElement("textarea");
textarea.className = "dtr_textsize05 dtr_max_width";
textarea.id = "format_title_preview";
textarea.readOnly = true;
textarea.rows = 1;
textarea.value = parseFormatString(setting_format);
box.appendChild(newSpan("Preview:"));
appendNewLine(box);
box.appendChild(textarea);
appendNewLine(box);
//------------------------------------------------------
box.appendChild(newButton("save", saveSetting));
box.appendChild(newSeparate());
box.appendChild(newButton("default", () => {
document.getElementById("format_title_setting").value = default_format;
}));
box.appendChild(newSeparate());
box.appendChild(newButton("clear", () => {
document.getElementById("format_title_setting").value = "";
}));
appendNewLine(box);
appendNewLine(box);
//------------------------------------------------------
let ck_area = document.createElement("div");
ck_area.className = "dtr_setting_ck_box";
appendAll(ck_area, [
newSpan(`Save & Refresh to make these setting work`, ""),
newLine(),
]);
let checkbox;
let lable;
checkbox = newCheckbox(`dtr_${key_f2h}`, function () {
setting_f2h = this.checked ? true : false;
debug_msg(`${key_f2h}: ${setting_f2h}`);
});
lable = newLable(`dtr_${key_f2h}`, "Replace some half-width to full-width");
if (setting_f2h) checkbox.checked = true;
appendAll(ck_area, [checkbox, lable, newLine()]);
checkbox = newCheckbox(`dtr_${key_show_ot}`, function () {
setting_show_ot = this.checked ? true : false;
debug_msg(`${key_show_ot}: ${setting_show_ot}`);
});
lable = newLable(`dtr_${key_show_ot}`, "Show Original / ID+Original");
if (setting_show_ot) checkbox.checked = true;
appendAll(ck_area, [checkbox, lable, newLine()]);
checkbox = newCheckbox(`dtr_${key_show_ft}`, function () {
setting_show_ft = this.checked ? true : false;
debug_msg(`${key_show_ft}: ${setting_show_ft}`);
});
lable = newLable(`dtr_${key_show_ft}`, "Show Formatted / ID+Formatted");
if (setting_show_ft) checkbox.checked = true;
appendAll(ck_area, [checkbox, lable, newLine()]);
textarea = document.createElement("textarea");
textarea.className = "dtr_textsize05";
textarea.id = `dtr_${key_sep}`;
textarea.rows = 1;
textarea.cols = 1;
textarea.value = setting_sep;
textarea.style = "resize: none;";
lable = newLable(`dtr_${key_sep}`, "Separator: ");
appendAll(ck_area, [
lable, textarea, newLable(`dtr_${key_sep}`, " for data have muti value like tags"),
newLine(),
]);
box.appendChild(ck_area);
appendNewLine(box);
//------------------------------------------------------
box.appendChild(newSpan("data list:"));
appendNewLine(box);
textarea = document.createElement("textarea");
textarea.className = "dtr_textsize05";
textarea.id = "format_title_all_data";
textarea.readOnly = true;
box.appendChild(textarea);
listAllData();
updateSetting();
console.timeEnd(setting.name);
}
function listAllData() {
let textbox = document.getElementById("format_title_all_data");
textbox.value = "";
let count = 0;
let maxlength = 0;
let s;
for (let key in formatted_data) {
s = `%${key}%: ${formatted_data[key]}\n`;
textbox.value += s;
count++;
if (formatted_data[key] && s.length > maxlength) maxlength = s.length;
}
textbox.rows = count + 1;
textbox.cols = maxlength << 1;
}
function updateSettingString(id, format_string) {
let textarea = document.getElementById(id);
let list = [
"year",
"Year",
"month",
"day",
];
let o = textarea.value;
if (list.some(s => format_string.includes(s)) && list.some(s => o.endsWith(`%${s}%`))) {
textarea.value += format_string;
} else {
textarea.value += ` ${format_string}`;
}
textarea.value = textarea.value.trim();
}
function productHandler() {
getData();
console.time(productHandler.name);
//------------------------------------------------------
let pos = document.querySelector("#work_name");
pos.innerHTML = `<div style="${setting_show_ot ? '' : 'display:none;'} user-select: text;">${pos.innerText}</div>`;
let id = formatted_data.id;
let title_o = formatted_data.title_original;
let title_f = formatted_data.title_formatted;
let title_id_c = parseFormatString(setting_format);
let title_id_o = `${id} ${title_o}`;
let title_id_f = `${id} ${title_f}`;
let notSame_o_c = Boolean(title_id_o != title_id_c);
let notSame_f_c_o = Boolean(title_id_f != title_id_c && title_id_f != title_id_o);
//------------------------------------------------------
// Cover url
let span_cover = newSpan(newCoverUrl(id), "");
span_cover.style = "user-select: text;";
pos.append(span_cover);
appendNewLine(pos);
//------------------------------------------------------
// ID + original title
if (notSame_o_c && setting_show_ot) {
let span = newSpan(title_id_o, "");
span.style = "user-select: text;";
pos.append(span);
appendNewLine(pos);
}
//------------------------------------------------------
// ID + formatted title
if (notSame_f_c_o && setting_show_ft) {
let span = newSpan(title_id_f, "");
span.style = "user-select: text;";
pos.append(span);
appendNewLine(pos);
}
//------------------------------------------------------
// custom title
let span = newSpan(title_id_c, "");
span.style = "user-select: text;";
span.id = "format_title_custom_span";
pos.append(span);
appendNewLine(pos);
//------------------------------------------------------
// add copy ID button
pos.append(newCopyButton(id));
pos.append(newSeparate());
//------------------------------------------------------
// add download cover
pos.append(newCoverDownload(id));
pos.append(newSeparate());
//------------------------------------------------------
// add copy cover url
pos.append(newCopyButton(newCoverUrl(id), "Cover(Url)"));
pos.append(newSeparate());
//------------------------------------------------------
// add copy custom format button