-
Notifications
You must be signed in to change notification settings - Fork 6
/
V2Next-Mobile.user.js
7797 lines (7791 loc) · 340 KB
/
V2Next-Mobile.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 V2Next-Mobile
// @namespace http://tampermonkey.net/
// @version 8.3
// @author zyronon
// @description V2Next - 一个好用的V2EX脚本! 移动端专用
// @license GPL License
// @icon https://v2next.netlify.app/favicon.ico
// @homepage https://github.com/zyronon/web-scripts
// @homepageURL https://github.com/zyronon/web-scripts
// @supportURL https://update.greasyfork.org/scripts/485356/V2Next-Mobile.user.js
// @downloadURL https://update.greasyfork.org/scripts/485356/V2Next-Mobile.user.js
// @updateURL https://update.greasyfork.org/scripts/485356/V2Next-Mobile.user.js
// @match https://v2ex.com/
// @match https://v2ex.com/?tab=*
// @match https://v2ex.com/t/*
// @match https://v2ex.com/recent*
// @match https://v2ex.com/go/*
// @match https://v2ex.com/member/*
// @match https://v2ex.com/changes*
// @match https://*.v2ex.com/
// @match https://*.v2ex.com/?tab=*
// @match https://*.v2ex.com/t/*
// @match https://*.v2ex.com/recent*
// @match https://*.v2ex.com/go/*
// @match https://*.v2ex.com/member/*
// @match https://*.v2ex.com/changes*
// @require http://code.jquery.com/jquery-3.7.1.min.js
// @require https://cdn.jsdelivr.net/npm/vue@3.4.14/dist/vue.global.prod.js
// @grant GM_addStyle
// @grant GM_notification
// @grant GM_openInTab
// @grant GM_registerMenuCommand
// ==/UserScript==
(function (vue) {
'use strict';
var PageType = /* @__PURE__ */ ((PageType2) => {
PageType2["Home"] = "Home";
PageType2["Node"] = "Node";
PageType2["Post"] = "Post";
PageType2["Member"] = "Member";
PageType2["Changes"] = "Changes";
return PageType2;
})(PageType || {});
var CommentDisplayType = /* @__PURE__ */ ((CommentDisplayType2) => {
CommentDisplayType2[CommentDisplayType2["FloorInFloor"] = 0] = "FloorInFloor";
CommentDisplayType2[CommentDisplayType2["FloorInFloorNoCallUser"] = 4] = "FloorInFloorNoCallUser";
CommentDisplayType2[CommentDisplayType2["FloorInFloorNested"] = 5] = "FloorInFloorNested";
CommentDisplayType2[CommentDisplayType2["Like"] = 1] = "Like";
CommentDisplayType2[CommentDisplayType2["V2exOrigin"] = 2] = "V2exOrigin";
CommentDisplayType2[CommentDisplayType2["OnlyOp"] = 3] = "OnlyOp";
CommentDisplayType2[CommentDisplayType2["New"] = 6] = "New";
return CommentDisplayType2;
})(CommentDisplayType || {});
const MAX_REPLY_LIMIT = 400;
const _sfc_main$r = {
name: "Tooltip",
props: {
title: {
type: String,
default() {
return "";
}
},
disabled: {
type: Boolean,
default() {
return false;
}
}
},
data() {
return {
show: false
};
},
methods: {
showPop(e2) {
if (this.disabled)
return;
if (!this.title)
return;
e2.stopPropagation();
let rect = e2.target.getBoundingClientRect();
this.show = true;
vue.nextTick(() => {
var _a, _b;
let tip = (_b = (_a = this.$refs) == null ? void 0 : _a.tip) == null ? void 0 : _b.getBoundingClientRect();
if (!tip)
return;
if (rect.top < 50) {
this.$refs.tip.style.top = rect.top + rect.height + 10 + "px";
} else {
this.$refs.tip.style.top = rect.top - tip.height - 10 + "px";
}
let tipWidth = tip.width;
let rectWidth = rect.width;
this.$refs.tip.style.left = rect.left - (tipWidth - rectWidth) / 2 + "px";
});
}
},
render() {
let Vnode = this.$slots.default()[0];
return vue.createVNode(vue.Fragment, null, [this.show && this.title && vue.createVNode(vue.Teleport, {
"to": "body"
}, {
default: () => [vue.createVNode(vue.Transition, {
"name": "fade"
}, {
default: () => [vue.createVNode("div", {
"ref": "tip",
"className": "tip"
}, [this.title])]
})]
}), vue.createVNode(Vnode, {
"onClick": () => this.show = false,
"onmouseenter": (e2) => this.showPop(e2),
"onmouseleave": () => this.show = false
}, null)]);
}
};
const _export_sfc = (sfc, props) => {
const target = sfc.__vccOpts || sfc;
for (const [key, val] of props) {
target[key] = val;
}
return target;
};
const Tooltip = /* @__PURE__ */ _export_sfc(_sfc_main$r, [["__scopeId", "data-v-ee672411"]]);
const _sfc_main$q = /* @__PURE__ */ vue.defineComponent({
__name: "BaseSwitch",
props: {
modelValue: { type: Boolean }
},
emits: ["update:modelValue"],
setup(__props, { emit: __emit }) {
return (_ctx, _cache) => {
return vue.openBlock(), vue.createElementBlock("div", {
class: vue.normalizeClass(["switch", { active: _ctx.modelValue }]),
onClick: _cache[0] || (_cache[0] = ($event) => _ctx.$emit("update:modelValue", !_ctx.modelValue))
}, null, 2);
};
}
});
const BaseSwitch = /* @__PURE__ */ _export_sfc(_sfc_main$q, [["__scopeId", "data-v-e7c0fbef"]]);
const matchIconName = /^[a-z0-9]+(-[a-z0-9]+)*$/;
const stringToIcon = (value, validate, allowSimpleName, provider = "") => {
const colonSeparated = value.split(":");
if (value.slice(0, 1) === "@") {
if (colonSeparated.length < 2 || colonSeparated.length > 3) {
return null;
}
provider = colonSeparated.shift().slice(1);
}
if (colonSeparated.length > 3 || !colonSeparated.length) {
return null;
}
if (colonSeparated.length > 1) {
const name2 = colonSeparated.pop();
const prefix = colonSeparated.pop();
const result = {
// Allow provider without '@': "provider:prefix:name"
provider: colonSeparated.length > 0 ? colonSeparated[0] : provider,
prefix,
name: name2
};
return validate && !validateIconName(result) ? null : result;
}
const name = colonSeparated[0];
const dashSeparated = name.split("-");
if (dashSeparated.length > 1) {
const result = {
provider,
prefix: dashSeparated.shift(),
name: dashSeparated.join("-")
};
return validate && !validateIconName(result) ? null : result;
}
if (allowSimpleName && provider === "") {
const result = {
provider,
prefix: "",
name
};
return validate && !validateIconName(result, allowSimpleName) ? null : result;
}
return null;
};
const validateIconName = (icon, allowSimpleName) => {
if (!icon) {
return false;
}
return !!((icon.provider === "" || icon.provider.match(matchIconName)) && (allowSimpleName && icon.prefix === "" || icon.prefix.match(matchIconName)) && icon.name.match(matchIconName));
};
const defaultIconDimensions = Object.freeze(
{
left: 0,
top: 0,
width: 16,
height: 16
}
);
const defaultIconTransformations = Object.freeze({
rotate: 0,
vFlip: false,
hFlip: false
});
const defaultIconProps = Object.freeze({
...defaultIconDimensions,
...defaultIconTransformations
});
const defaultExtendedIconProps = Object.freeze({
...defaultIconProps,
body: "",
hidden: false
});
function mergeIconTransformations(obj1, obj2) {
const result = {};
if (!obj1.hFlip !== !obj2.hFlip) {
result.hFlip = true;
}
if (!obj1.vFlip !== !obj2.vFlip) {
result.vFlip = true;
}
const rotate = ((obj1.rotate || 0) + (obj2.rotate || 0)) % 4;
if (rotate) {
result.rotate = rotate;
}
return result;
}
function mergeIconData(parent, child) {
const result = mergeIconTransformations(parent, child);
for (const key in defaultExtendedIconProps) {
if (key in defaultIconTransformations) {
if (key in parent && !(key in result)) {
result[key] = defaultIconTransformations[key];
}
} else if (key in child) {
result[key] = child[key];
} else if (key in parent) {
result[key] = parent[key];
}
}
return result;
}
function getIconsTree(data, names) {
const icons = data.icons;
const aliases = data.aliases || /* @__PURE__ */ Object.create(null);
const resolved = /* @__PURE__ */ Object.create(null);
function resolve(name) {
if (icons[name]) {
return resolved[name] = [];
}
if (!(name in resolved)) {
resolved[name] = null;
const parent = aliases[name] && aliases[name].parent;
const value = parent && resolve(parent);
if (value) {
resolved[name] = [parent].concat(value);
}
}
return resolved[name];
}
(names || Object.keys(icons).concat(Object.keys(aliases))).forEach(resolve);
return resolved;
}
function internalGetIconData(data, name, tree) {
const icons = data.icons;
const aliases = data.aliases || /* @__PURE__ */ Object.create(null);
let currentProps = {};
function parse(name2) {
currentProps = mergeIconData(
icons[name2] || aliases[name2],
currentProps
);
}
parse(name);
tree.forEach(parse);
return mergeIconData(data, currentProps);
}
function parseIconSet(data, callback) {
const names = [];
if (typeof data !== "object" || typeof data.icons !== "object") {
return names;
}
if (data.not_found instanceof Array) {
data.not_found.forEach((name) => {
callback(name, null);
names.push(name);
});
}
const tree = getIconsTree(data);
for (const name in tree) {
const item = tree[name];
if (item) {
callback(name, internalGetIconData(data, name, item));
names.push(name);
}
}
return names;
}
const optionalPropertyDefaults = {
provider: "",
aliases: {},
not_found: {},
...defaultIconDimensions
};
function checkOptionalProps(item, defaults) {
for (const prop in defaults) {
if (prop in item && typeof item[prop] !== typeof defaults[prop]) {
return false;
}
}
return true;
}
function quicklyValidateIconSet(obj) {
if (typeof obj !== "object" || obj === null) {
return null;
}
const data = obj;
if (typeof data.prefix !== "string" || !obj.icons || typeof obj.icons !== "object") {
return null;
}
if (!checkOptionalProps(obj, optionalPropertyDefaults)) {
return null;
}
const icons = data.icons;
for (const name in icons) {
const icon = icons[name];
if (!name.match(matchIconName) || typeof icon.body !== "string" || !checkOptionalProps(
icon,
defaultExtendedIconProps
)) {
return null;
}
}
const aliases = data.aliases || /* @__PURE__ */ Object.create(null);
for (const name in aliases) {
const icon = aliases[name];
const parent = icon.parent;
if (!name.match(matchIconName) || typeof parent !== "string" || !icons[parent] && !aliases[parent] || !checkOptionalProps(
icon,
defaultExtendedIconProps
)) {
return null;
}
}
return data;
}
const dataStorage = /* @__PURE__ */ Object.create(null);
function newStorage(provider, prefix) {
return {
provider,
prefix,
icons: /* @__PURE__ */ Object.create(null),
missing: /* @__PURE__ */ new Set()
};
}
function getStorage(provider, prefix) {
const providerStorage = dataStorage[provider] || (dataStorage[provider] = /* @__PURE__ */ Object.create(null));
return providerStorage[prefix] || (providerStorage[prefix] = newStorage(provider, prefix));
}
function addIconSet(storage2, data) {
if (!quicklyValidateIconSet(data)) {
return [];
}
return parseIconSet(data, (name, icon) => {
if (icon) {
storage2.icons[name] = icon;
} else {
storage2.missing.add(name);
}
});
}
function addIconToStorage(storage2, name, icon) {
try {
if (typeof icon.body === "string") {
storage2.icons[name] = { ...icon };
return true;
}
} catch (err) {
}
return false;
}
let simpleNames = false;
function allowSimpleNames(allow) {
if (typeof allow === "boolean") {
simpleNames = allow;
}
return simpleNames;
}
function getIconData(name) {
const icon = typeof name === "string" ? stringToIcon(name, true, simpleNames) : name;
if (icon) {
const storage2 = getStorage(icon.provider, icon.prefix);
const iconName = icon.name;
return storage2.icons[iconName] || (storage2.missing.has(iconName) ? null : void 0);
}
}
function addIcon(name, data) {
const icon = stringToIcon(name, true, simpleNames);
if (!icon) {
return false;
}
const storage2 = getStorage(icon.provider, icon.prefix);
return addIconToStorage(storage2, icon.name, data);
}
function addCollection(data, provider) {
if (typeof data !== "object") {
return false;
}
if (typeof provider !== "string") {
provider = data.provider || "";
}
if (simpleNames && !provider && !data.prefix) {
let added = false;
if (quicklyValidateIconSet(data)) {
data.prefix = "";
parseIconSet(data, (name, icon) => {
if (icon && addIcon(name, icon)) {
added = true;
}
});
}
return added;
}
const prefix = data.prefix;
if (!validateIconName({
provider,
prefix,
name: "a"
})) {
return false;
}
const storage2 = getStorage(provider, prefix);
return !!addIconSet(storage2, data);
}
const defaultIconSizeCustomisations = Object.freeze({
width: null,
height: null
});
const defaultIconCustomisations = Object.freeze({
// Dimensions
...defaultIconSizeCustomisations,
// Transformations
...defaultIconTransformations
});
const unitsSplit = /(-?[0-9.]*[0-9]+[0-9.]*)/g;
const unitsTest = /^-?[0-9.]*[0-9]+[0-9.]*$/g;
function calculateSize(size, ratio, precision) {
if (ratio === 1) {
return size;
}
precision = precision || 100;
if (typeof size === "number") {
return Math.ceil(size * ratio * precision) / precision;
}
if (typeof size !== "string") {
return size;
}
const oldParts = size.split(unitsSplit);
if (oldParts === null || !oldParts.length) {
return size;
}
const newParts = [];
let code = oldParts.shift();
let isNumber = unitsTest.test(code);
while (true) {
if (isNumber) {
const num = parseFloat(code);
if (isNaN(num)) {
newParts.push(code);
} else {
newParts.push(Math.ceil(num * ratio * precision) / precision);
}
} else {
newParts.push(code);
}
code = oldParts.shift();
if (code === void 0) {
return newParts.join("");
}
isNumber = !isNumber;
}
}
const isUnsetKeyword = (value) => value === "unset" || value === "undefined" || value === "none";
function iconToSVG(icon, customisations) {
const fullIcon = {
...defaultIconProps,
...icon
};
const fullCustomisations = {
...defaultIconCustomisations,
...customisations
};
const box = {
left: fullIcon.left,
top: fullIcon.top,
width: fullIcon.width,
height: fullIcon.height
};
let body = fullIcon.body;
[fullIcon, fullCustomisations].forEach((props) => {
const transformations = [];
const hFlip = props.hFlip;
const vFlip = props.vFlip;
let rotation = props.rotate;
if (hFlip) {
if (vFlip) {
rotation += 2;
} else {
transformations.push(
"translate(" + (box.width + box.left).toString() + " " + (0 - box.top).toString() + ")"
);
transformations.push("scale(-1 1)");
box.top = box.left = 0;
}
} else if (vFlip) {
transformations.push(
"translate(" + (0 - box.left).toString() + " " + (box.height + box.top).toString() + ")"
);
transformations.push("scale(1 -1)");
box.top = box.left = 0;
}
let tempValue;
if (rotation < 0) {
rotation -= Math.floor(rotation / 4) * 4;
}
rotation = rotation % 4;
switch (rotation) {
case 1:
tempValue = box.height / 2 + box.top;
transformations.unshift(
"rotate(90 " + tempValue.toString() + " " + tempValue.toString() + ")"
);
break;
case 2:
transformations.unshift(
"rotate(180 " + (box.width / 2 + box.left).toString() + " " + (box.height / 2 + box.top).toString() + ")"
);
break;
case 3:
tempValue = box.width / 2 + box.left;
transformations.unshift(
"rotate(-90 " + tempValue.toString() + " " + tempValue.toString() + ")"
);
break;
}
if (rotation % 2 === 1) {
if (box.left !== box.top) {
tempValue = box.left;
box.left = box.top;
box.top = tempValue;
}
if (box.width !== box.height) {
tempValue = box.width;
box.width = box.height;
box.height = tempValue;
}
}
if (transformations.length) {
body = '<g transform="' + transformations.join(" ") + '">' + body + "</g>";
}
});
const customisationsWidth = fullCustomisations.width;
const customisationsHeight = fullCustomisations.height;
const boxWidth = box.width;
const boxHeight = box.height;
let width;
let height;
if (customisationsWidth === null) {
height = customisationsHeight === null ? "1em" : customisationsHeight === "auto" ? boxHeight : customisationsHeight;
width = calculateSize(height, boxWidth / boxHeight);
} else {
width = customisationsWidth === "auto" ? boxWidth : customisationsWidth;
height = customisationsHeight === null ? calculateSize(width, boxHeight / boxWidth) : customisationsHeight === "auto" ? boxHeight : customisationsHeight;
}
const attributes = {};
const setAttr = (prop, value) => {
if (!isUnsetKeyword(value)) {
attributes[prop] = value.toString();
}
};
setAttr("width", width);
setAttr("height", height);
attributes.viewBox = box.left.toString() + " " + box.top.toString() + " " + boxWidth.toString() + " " + boxHeight.toString();
return {
attributes,
body
};
}
const regex = /\sid="(\S+)"/g;
const randomPrefix = "IconifyId" + Date.now().toString(16) + (Math.random() * 16777216 | 0).toString(16);
let counter = 0;
function replaceIDs(body, prefix = randomPrefix) {
const ids = [];
let match;
while (match = regex.exec(body)) {
ids.push(match[1]);
}
if (!ids.length) {
return body;
}
const suffix = "suffix" + (Math.random() * 16777216 | Date.now()).toString(16);
ids.forEach((id) => {
const newID = typeof prefix === "function" ? prefix(id) : prefix + (counter++).toString();
const escapedID = id.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
body = body.replace(
// Allowed characters before id: [#;"]
// Allowed characters after id: [)"], .[a-z]
new RegExp('([#;"])(' + escapedID + ')([")]|\\.[a-z])', "g"),
"$1" + newID + suffix + "$3"
);
});
body = body.replace(new RegExp(suffix, "g"), "");
return body;
}
const storage = /* @__PURE__ */ Object.create(null);
function setAPIModule(provider, item) {
storage[provider] = item;
}
function getAPIModule(provider) {
return storage[provider] || storage[""];
}
function createAPIConfig(source) {
let resources;
if (typeof source.resources === "string") {
resources = [source.resources];
} else {
resources = source.resources;
if (!(resources instanceof Array) || !resources.length) {
return null;
}
}
const result = {
// API hosts
resources,
// Root path
path: source.path || "/",
// URL length limit
maxURL: source.maxURL || 500,
// Timeout before next host is used.
rotate: source.rotate || 750,
// Timeout before failing query.
timeout: source.timeout || 5e3,
// Randomise default API end point.
random: source.random === true,
// Start index
index: source.index || 0,
// Receive data after time out (used if time out kicks in first, then API module sends data anyway).
dataAfterTimeout: source.dataAfterTimeout !== false
};
return result;
}
const configStorage = /* @__PURE__ */ Object.create(null);
const fallBackAPISources = [
"https://api.simplesvg.com",
"https://api.unisvg.com"
];
const fallBackAPI = [];
while (fallBackAPISources.length > 0) {
if (fallBackAPISources.length === 1) {
fallBackAPI.push(fallBackAPISources.shift());
} else {
if (Math.random() > 0.5) {
fallBackAPI.push(fallBackAPISources.shift());
} else {
fallBackAPI.push(fallBackAPISources.pop());
}
}
}
configStorage[""] = createAPIConfig({
resources: ["https://api.iconify.design"].concat(fallBackAPI)
});
function addAPIProvider(provider, customConfig) {
const config2 = createAPIConfig(customConfig);
if (config2 === null) {
return false;
}
configStorage[provider] = config2;
return true;
}
function getAPIConfig(provider) {
return configStorage[provider];
}
const detectFetch = () => {
let callback;
try {
callback = fetch;
if (typeof callback === "function") {
return callback;
}
} catch (err) {
}
};
let fetchModule = detectFetch();
function calculateMaxLength(provider, prefix) {
const config2 = getAPIConfig(provider);
if (!config2) {
return 0;
}
let result;
if (!config2.maxURL) {
result = 0;
} else {
let maxHostLength = 0;
config2.resources.forEach((item) => {
const host = item;
maxHostLength = Math.max(maxHostLength, host.length);
});
const url = prefix + ".json?icons=";
result = config2.maxURL - maxHostLength - config2.path.length - url.length;
}
return result;
}
function shouldAbort(status) {
return status === 404;
}
const prepare = (provider, prefix, icons) => {
const results = [];
const maxLength = calculateMaxLength(provider, prefix);
const type = "icons";
let item = {
type,
provider,
prefix,
icons: []
};
let length = 0;
icons.forEach((name, index) => {
length += name.length + 1;
if (length >= maxLength && index > 0) {
results.push(item);
item = {
type,
provider,
prefix,
icons: []
};
length = name.length;
}
item.icons.push(name);
});
results.push(item);
return results;
};
function getPath(provider) {
if (typeof provider === "string") {
const config2 = getAPIConfig(provider);
if (config2) {
return config2.path;
}
}
return "/";
}
const send = (host, params, callback) => {
if (!fetchModule) {
callback("abort", 424);
return;
}
let path = getPath(params.provider);
switch (params.type) {
case "icons": {
const prefix = params.prefix;
const icons = params.icons;
const iconsList = icons.join(",");
const urlParams = new URLSearchParams({
icons: iconsList
});
path += prefix + ".json?" + urlParams.toString();
break;
}
case "custom": {
const uri = params.uri;
path += uri.slice(0, 1) === "/" ? uri.slice(1) : uri;
break;
}
default:
callback("abort", 400);
return;
}
let defaultError = 503;
fetchModule(host + path).then((response) => {
const status = response.status;
if (status !== 200) {
setTimeout(() => {
callback(shouldAbort(status) ? "abort" : "next", status);
});
return;
}
defaultError = 501;
return response.json();
}).then((data) => {
if (typeof data !== "object" || data === null) {
setTimeout(() => {
if (data === 404) {
callback("abort", data);
} else {
callback("next", defaultError);
}
});
return;
}
setTimeout(() => {
callback("success", data);
});
}).catch(() => {
callback("next", defaultError);
});
};
const fetchAPIModule = {
prepare,
send
};
function sortIcons(icons) {
const result = {
loaded: [],
missing: [],
pending: []
};
const storage2 = /* @__PURE__ */ Object.create(null);
icons.sort((a, b) => {
if (a.provider !== b.provider) {
return a.provider.localeCompare(b.provider);
}
if (a.prefix !== b.prefix) {
return a.prefix.localeCompare(b.prefix);
}
return a.name.localeCompare(b.name);
});
let lastIcon = {
provider: "",
prefix: "",
name: ""
};
icons.forEach((icon) => {
if (lastIcon.name === icon.name && lastIcon.prefix === icon.prefix && lastIcon.provider === icon.provider) {
return;
}
lastIcon = icon;
const provider = icon.provider;
const prefix = icon.prefix;
const name = icon.name;
const providerStorage = storage2[provider] || (storage2[provider] = /* @__PURE__ */ Object.create(null));
const localStorage2 = providerStorage[prefix] || (providerStorage[prefix] = getStorage(provider, prefix));
let list;
if (name in localStorage2.icons) {
list = result.loaded;
} else if (prefix === "" || localStorage2.missing.has(name)) {
list = result.missing;
} else {
list = result.pending;
}
const item = {
provider,
prefix,
name
};
list.push(item);
});
return result;
}
function removeCallback(storages, id) {
storages.forEach((storage2) => {
const items = storage2.loaderCallbacks;
if (items) {
storage2.loaderCallbacks = items.filter((row) => row.id !== id);
}
});
}
function updateCallbacks(storage2) {
if (!storage2.pendingCallbacksFlag) {
storage2.pendingCallbacksFlag = true;
setTimeout(() => {
storage2.pendingCallbacksFlag = false;
const items = storage2.loaderCallbacks ? storage2.loaderCallbacks.slice(0) : [];
if (!items.length) {
return;
}
let hasPending = false;
const provider = storage2.provider;
const prefix = storage2.prefix;
items.forEach((item) => {
const icons = item.icons;
const oldLength = icons.pending.length;
icons.pending = icons.pending.filter((icon) => {
if (icon.prefix !== prefix) {
return true;
}
const name = icon.name;
if (storage2.icons[name]) {
icons.loaded.push({
provider,
prefix,
name
});
} else if (storage2.missing.has(name)) {
icons.missing.push({
provider,
prefix,
name
});
} else {
hasPending = true;
return true;
}
return false;
});
if (icons.pending.length !== oldLength) {
if (!hasPending) {
removeCallback([storage2], item.id);
}
item.callback(
icons.loaded.slice(0),
icons.missing.slice(0),
icons.pending.slice(0),
item.abort
);
}
});
});
}
}
let idCounter = 0;
function storeCallback(callback, icons, pendingSources) {
const id = idCounter++;
const abort = removeCallback.bind(null, pendingSources, id);
if (!icons.pending.length) {
return abort;
}
const item = {
id,
icons,
callback,
abort
};
pendingSources.forEach((storage2) => {
(storage2.loaderCallbacks || (storage2.loaderCallbacks = [])).push(item);
});
return abort;
}
function listToIcons(list, validate = true, simpleNames2 = false) {
const result = [];
list.forEach((item) => {
const icon = typeof item === "string" ? stringToIcon(item, validate, simpleNames2) : item;
if (icon) {
result.push(icon);
}
});
return result;
}
var defaultConfig = {
resources: [],
index: 0,
timeout: 2e3,
rotate: 750,
random: false,
dataAfterTimeout: false
};
function sendQuery(config2, payload, query, done) {
const resourcesCount = config2.resources.length;
const startIndex = config2.random ? Math.floor(Math.random() * resourcesCount) : config2.index;
let resources;
if (config2.random) {
let list = config2.resources.slice(0);
resources = [];
while (list.length > 1) {
const nextIndex = Math.floor(Math.random() * list.length);
resources.push(list[nextIndex]);
list = list.slice(0, nextIndex).concat(list.slice(nextIndex + 1));
}
resources = resources.concat(list);
} else {
resources = config2.resources.slice(startIndex).concat(config2.resources.slice(0, startIndex));
}
const startTime = Date.now();
let status = "pending";
let queriesSent = 0;
let lastError;
let timer = null;
let queue = [];
let doneCallbacks = [];
if (typeof done === "function") {
doneCallbacks.push(done);
}
function resetTimer() {
if (timer) {
clearTimeout(timer);
timer = null;
}
}
function abort() {
if (status === "pending") {
status = "aborted";
}
resetTimer();
queue.forEach((item) => {