-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathAdvancedFlagging.user.js
3874 lines (3830 loc) · 142 KB
/
AdvancedFlagging.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 Advanced Flagging
// @namespace https://github.com/SOBotics
// @version 2.1.3
// @author Robert Rudman
// @contributor double-beep
// @match *://*.stackexchange.com/*
// @match *://*.stackoverflow.com/*
// @match *://*.superuser.com/*
// @match *://*.serverfault.com/*
// @match *://*.askubuntu.com/*
// @match *://*.stackapps.com/*
// @match *://*.mathoverflow.net/*
// @exclude *://chat.stackexchange.com/*
// @exclude *://chat.meta.stackexchange.com/*
// @exclude *://chat.stackoverflow.com/*
// @exclude *://area51.stackexchange.com/*
// @exclude *://data.stackexchange.com/*
// @exclude *://stackoverflow.com/c/*
// @exclude *://winterbash*.stackexchange.com/*
// @exclude *://api.stackexchange.com/*
// @resource iconCheckmark https://cdn.sstatic.net/Img/stacks-icons/Checkmark.svg
// @resource iconClear https://cdn.sstatic.net/Img/stacks-icons/Clear.svg
// @resource iconEyeOff https://cdn.sstatic.net/Img/stacks-icons/EyeOff.svg
// @resource iconFlag https://cdn.sstatic.net/Img/stacks-icons/Flag.svg
// @resource iconPencil https://cdn.sstatic.net/Img/stacks-icons/Pencil.svg
// @resource iconTrash https://cdn.sstatic.net/Img/stacks-icons/Trash.svg
// @resource iconPlus https://cdn.sstatic.net/Img/stacks-icons/Plus.svg
// @grant GM_xmlhttpRequest
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_deleteValue
// @grant GM_addStyle
// @grant GM_getResourceText
// @downloadURL https://github.com/SOBotics/AdvancedFlagging/raw/master/dist/AdvancedFlagging.user.js
// @updateURL https://github.com/SOBotics/AdvancedFlagging/raw/master/dist/AdvancedFlagging.user.js
// ==/UserScript==
/* globals StackExchange, Stacks, $ */
"use strict";
(() => {
var __defProp = Object.defineProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
// src/UserscriptTools/Store.ts
var Cached = {
Configuration: {
key: "Configuration",
openOnHover: "openOnHover",
defaultNoFlag: "defaultNoFlag",
defaultNoComment: "defaultNoComment",
defaultNoDownvote: "defaultNoDownvote",
defaultNoDelete: "defaultNoDelete",
watchFlags: "watchFlags",
watchQueues: "watchQueues",
linkDisabled: "linkDisabled",
addAuthorName: "addAuthorName",
debug: "debug"
},
Fkey: "fkey",
Metasmoke: {
userKey: "MetaSmoke.userKey",
disabled: "MetaSmoke.disabled"
},
FlagTypes: "FlagTypes",
FlagCategories: "FlagCategories"
};
var Store = class _Store {
// cache-related helpers/values
// Some information from cache is stored on the variables as objects to make editing easier and simpler
// Each time something is changed in the variables, update* must also be called to save the changes to the cache
static config = _Store.get(Cached.Configuration.key) ?? {};
static categories = _Store.get(Cached.FlagCategories) ?? [];
static flagTypes = _Store.get(Cached.FlagTypes) ?? [];
static updateConfiguration = () => _Store.set(Cached.Configuration.key, this.config);
static updateFlagTypes = () => _Store.set(Cached.FlagTypes, this.flagTypes);
static dryRun = this.config[Cached.Configuration.debug];
// export const updateCategories = (): void => GreaseMonkeyCache.storeInCache(FlagCategoriesKey, cachedCategories);
static async getAndCache(cacheKey, getterPromise, expiresAt) {
const cachedItem = _Store.get(cacheKey);
if (cachedItem) return cachedItem;
const result = await getterPromise();
_Store.set(cacheKey, result, expiresAt);
return result;
}
// There are two kinds of objects that are stored in the cache:
// - those that expire (only fkey currently)
// - those that are not
//
// The type of those that are expirable is ExpiryingCacheItem.
// The others are strings or objects.
// To make TS happy and avoid runtime errors, we need to take into account both cases.
static get(cacheKey) {
const cachedItem = GM_getValue(cacheKey);
if (!cachedItem) return null;
const isItemExpired = typeof cachedItem === "object" && "Data" in cachedItem && new Date(cachedItem.Expires) < /* @__PURE__ */ new Date();
if (isItemExpired) return null;
return typeof cachedItem === "object" && "Data" in cachedItem ? cachedItem.Data : cachedItem;
}
static set(cacheKey, item, expiresAt) {
const jsonObject = expiresAt ? { Expires: expiresAt.getTime(), Data: item } : item;
GM_setValue(cacheKey, jsonObject);
}
static unset(cacheKey) {
GM_deleteValue(cacheKey);
}
};
// node_modules/@userscripters/stacks-helpers/dist/checkbox.js
var checkbox_exports = {};
__export(checkbox_exports, {
makeStacksCheckboxes: () => makeStacksCheckboxes
});
var makeStacksCheckboxes = (checkboxes, options) => {
return input_exports.makeStacksRadiosOrCheckboxes(checkboxes, "checkbox", options);
};
// node_modules/@userscripters/stacks-helpers/dist/input.js
var input_exports = {};
__export(input_exports, {
makeStacksInput: () => makeStacksInput,
makeStacksRadiosOrCheckboxes: () => makeStacksRadiosOrCheckboxes
});
var makeStacksInput = (id, inputOptions = {}, labelOptions) => {
var _a;
const { value = "", classes = [], placeholder = "", title, isSearch } = inputOptions;
const inputParent = document.createElement("div");
inputParent.classList.add("d-flex", "ps-relative");
const input = document.createElement("input");
input.classList.add("s-input", ...classes);
input.type = "text";
input.id = input.name = id;
input.placeholder = placeholder;
input.value = value;
if (title)
input.title = title;
if (isSearch) {
input.classList.add("s-input__search");
const [searchIcon] = icons_exports.makeStacksIcon("iconSearch", "m18 16.5-5.14-5.18h-.35a7 7 0 10-1.19 1.19v.35L16.5 18l1.5-1.5zM12 7A5 5 0 112 7a5 5 0 0110 0z", {
classes: ["s-input-icon", "s-input-icon__search"],
width: 18
});
inputParent.append(searchIcon);
}
inputParent.prepend(input);
if (labelOptions) {
(_a = labelOptions.parentClasses || (labelOptions.parentClasses = [])) === null || _a === void 0 ? void 0 : _a.push("flex--item");
const label = label_exports.makeStacksLabel(id, labelOptions);
const container = document.createElement("div");
container.classList.add("d-flex", "gy4", "fd-column");
container.append(label, inputParent);
return container;
}
return inputParent;
};
var makeStacksRadiosOrCheckboxes = (inputs, type, options, withoutFieldset) => {
const fieldset = document.createElement("fieldset");
fieldset.classList.add("s-check-group");
if (options) {
const { legendText = "", legendDescription = "", horizontal, classes = [] } = options;
if (horizontal) {
fieldset.classList.add("s-check-group__horizontal");
}
fieldset.classList.add(...classes);
const legend = document.createElement("legend");
legend.classList.add("flex--item", "s-label");
legend.innerText = legendText;
if (legendDescription) {
const span = document.createElement("span");
span.classList.add("ml4", "fw-normal", "fc-light");
span.innerText = legendDescription;
legend.append(" ", span);
}
fieldset.append(legend);
}
const items = inputs.map((inputType) => makeFormContainer(inputType, type));
if (withoutFieldset) {
return items;
} else {
fieldset.append(...items);
return [fieldset, ...items];
}
};
var makeFormContainer = (radioCheckbox, type) => {
const { id, labelConfig, selected = false, disabled = false, name } = radioCheckbox;
const container = document.createElement("div");
container.classList.add("s-check-control");
const input = document.createElement("input");
input.classList.add(`s-${type}`);
input.type = type;
input.id = id;
input.checked = selected;
input.disabled = disabled;
if (name) {
input.name = name;
}
const label = label_exports.makeStacksLabel(id, labelConfig);
container.append(input, label);
return container;
};
// node_modules/@userscripters/stacks-helpers/dist/label.js
var label_exports = {};
__export(label_exports, {
makeStacksLabel: () => makeStacksLabel
});
var makeStacksLabel = (forId, labelOptions) => {
const { classes = [], parentClasses = [], text, description, statusText, statusType } = labelOptions;
const labelParent = document.createElement("div");
labelParent.classList.add(...parentClasses);
const label = document.createElement("label");
label.classList.add("s-label", ...classes);
label.htmlFor = forId;
label.innerHTML = text;
if (statusText && statusType) {
const status = document.createElement("span");
status.innerHTML = statusText;
status.classList.add("s-label--status");
if (statusType !== "optional") {
status.classList.add(`s-label--status__${statusType}`);
}
label.append(" ", status);
}
if (description) {
const p = document.createElement("p");
p.classList.add("s-description", "mt2");
p.innerHTML = description;
label.classList.add("d-block");
label.append(p);
labelParent.append(label);
return labelParent;
} else {
label.classList.add("flex--item");
return label;
}
};
// node_modules/@userscripters/stacks-helpers/dist/links.js
var links_exports = {};
__export(links_exports, {
makeLink: () => makeLink
});
var makeLink = (options = {}) => {
const { href = "", isButton = false, type = "", blockLink = null, text, click, classes = [] } = options;
const anchor = document.createElement(isButton ? "button" : "a");
anchor.classList.add("s-link", ...classes);
anchor.textContent = text;
if (type) {
anchor.classList.add(`s-link__${type}`);
}
if (blockLink) {
anchor.classList.add("s-block-link");
anchor.classList.remove("s-link");
if (blockLink.border) {
anchor.classList.add(`s-block-link__${blockLink.border}`);
}
if (blockLink.selected) {
anchor.classList.add("is-selected");
}
if (blockLink.danger) {
anchor.classList.add("s-block-link__danger");
}
}
if (href && anchor instanceof HTMLAnchorElement) {
anchor.href = href;
}
if (click) {
const { handler, options: options2 } = click;
anchor.addEventListener("click", handler, options2);
}
return anchor;
};
// node_modules/@userscripters/stacks-helpers/dist/menus.js
var menus_exports = {};
__export(menus_exports, {
makeMenu: () => makeMenu
});
var makeMenu = (options = {}) => {
const { itemsType = "a", childrenClasses = [], navItems, classes = [] } = options;
const menu = document.createElement("ul");
menu.classList.add("s-menu", ...classes);
menu.setAttribute("role", "menu");
navItems.forEach((navItem) => {
var _a;
const li = document.createElement("li");
if ("popover" in navItem && navItem.popover) {
const { position = "auto", html } = navItem.popover;
Stacks.setTooltipHtml(li, html, {
placement: position
});
}
if ("separatorType" in navItem) {
const { separatorType, separatorText } = navItem;
li.setAttribute("role", "separator");
li.classList.add(`s-menu--${separatorType}`);
if (separatorText)
li.textContent = separatorText;
menu.append(li);
return;
} else if ("checkbox" in navItem) {
const { checkbox, checkboxOptions } = navItem;
const [, input] = checkbox_exports.makeStacksCheckboxes([checkbox], checkboxOptions);
li.append(input);
menu.append(li);
return;
}
(_a = navItem.classes) === null || _a === void 0 ? void 0 : _a.push(...childrenClasses);
li.setAttribute("role", "menuitem");
const item = links_exports.makeLink(Object.assign({
isButton: itemsType === "button" || navItem.isButton,
blockLink: {}
}, navItem));
li.append(item);
menu.append(li);
});
return menu;
};
// node_modules/@userscripters/stacks-helpers/dist/notices.js
var notices_exports = {};
__export(notices_exports, {
makeStacksNotice: () => makeStacksNotice
});
var makeStacksNotice = (options) => {
const { type, important = false, icon, text, classes = [] } = options;
const notice = document.createElement("aside");
notice.classList.add("s-notice", ...classes);
notice.setAttribute("role", important ? "alert" : "status");
if (type) {
notice.classList.add(`s-notice__${type}`);
}
if (important) {
notice.classList.add("s-notice__important");
}
if (icon) {
notice.classList.add("d-flex");
const iconContainer = document.createElement("div");
iconContainer.classList.add("flex--item", "mr8");
const [name, path] = icon;
const [svgIcon] = icons_exports.makeStacksIcon(name, path, { width: 18 });
iconContainer.append(svgIcon);
const textContainer = document.createElement("div");
textContainer.classList.add("flex--item", "lh-lg");
textContainer.append(text);
notice.append(iconContainer, textContainer);
} else {
const p = document.createElement("p");
p.classList.add("m0");
p.append(text);
notice.append(p);
}
return notice;
};
// node_modules/@userscripters/stacks-helpers/dist/radio.js
var radio_exports = {};
__export(radio_exports, {
makeStacksRadios: () => makeStacksRadios
});
var makeStacksRadios = (radios, groupName, options) => {
radios.forEach((radio) => {
radio.name = groupName;
});
return input_exports.makeStacksRadiosOrCheckboxes(radios, "radio", options);
};
// node_modules/@userscripters/stacks-helpers/dist/select.js
var select_exports = {};
__export(select_exports, {
makeStacksSelect: () => makeStacksSelect,
toggleValidation: () => toggleValidation
});
var makeStacksSelect = (id, items, options = {}, labelOptions) => {
const { disabled = false, size, validation, classes = [] } = options;
const container = document.createElement("div");
container.classList.add("d-flex", "gy4", "fd-column");
if (labelOptions) {
(labelOptions.parentClasses || (labelOptions.parentClasses = [])).push("flex--item");
const label = label_exports.makeStacksLabel(id, labelOptions);
container.append(label);
}
const selectContainer = document.createElement("div");
selectContainer.classList.add("flex--item", "s-select");
if (size) {
selectContainer.classList.add(`s-select__${size}`);
}
const select = document.createElement("select");
select.id = id;
select.classList.add(...classes);
if (disabled) {
container.classList.add("is-disabled");
select.disabled = true;
}
items.forEach((item) => {
const { value, text, selected = false } = item;
const option = document.createElement("option");
option.value = value;
option.text = text;
option.selected = selected;
select.append(option);
});
selectContainer.append(select);
container.append(selectContainer);
if (validation) {
toggleValidation(container, validation);
}
return container;
};
var toggleValidation = (container, state) => {
var _a, _b;
container.classList.remove("has-success", "has-warning", "has-error");
(_a = container.querySelector(".s-input-icon")) === null || _a === void 0 ? void 0 : _a.remove();
if (!state)
return;
container.classList.add(`has-${state}`);
const [name, path] = icons_exports.validationIcons[state];
const [icon] = icons_exports.makeStacksIcon(name, path, {
classes: ["s-input-icon"],
width: 18
});
(_b = container.querySelector(".s-select")) === null || _b === void 0 ? void 0 : _b.append(icon);
};
// node_modules/@userscripters/stacks-helpers/dist/spinner.js
var spinner_exports = {};
__export(spinner_exports, {
makeSpinner: () => makeSpinner
});
var makeSpinner = (options = {}) => {
const { size = "", hiddenText = "", classes = [] } = options;
const spinner = document.createElement("div");
spinner.classList.add("s-spinner", ...classes);
if (size) {
spinner.classList.add(`s-spinner__${size}`);
}
if (hiddenText) {
const hiddenElement = document.createElement("div");
hiddenElement.classList.add("v-visible-sr");
hiddenElement.innerText = hiddenText;
spinner.append(hiddenElement);
}
return spinner;
};
// node_modules/@userscripters/stacks-helpers/dist/textarea.js
var textarea_exports = {};
__export(textarea_exports, {
makeStacksTextarea: () => makeStacksTextarea,
toggleValidation: () => toggleValidation2
});
var makeStacksTextarea = (id, textareaOptions = {}, labelOptions) => {
const { value = "", classes = [], placeholder = "", title = "", size, validation } = textareaOptions;
const textareaParent = document.createElement("div");
textareaParent.classList.add("d-flex", "fd-column", "gy4", ...classes);
if (labelOptions) {
const label = label_exports.makeStacksLabel(id, labelOptions);
textareaParent.append(label);
}
const textarea = document.createElement("textarea");
textarea.classList.add("flex--item", "s-textarea");
textarea.id = id;
textarea.placeholder = placeholder;
textarea.value = value;
textarea.title = title;
if (size) {
textarea.classList.add(`s-textarea__${size}`);
}
textareaParent.append(textarea);
if (validation) {
toggleValidation2(textareaParent, validation);
}
return textareaParent;
};
var toggleValidation2 = (textareaParent, validation) => {
var _a, _b;
textareaParent.classList.remove("has-success", "has-warning", "has-error");
const oldTextarea = textareaParent.querySelector(".s-textarea");
if (!validation) {
(_a = textareaParent.querySelector(".s-input-icon")) === null || _a === void 0 ? void 0 : _a.remove();
(_b = textareaParent.querySelector(".s-input-message")) === null || _b === void 0 ? void 0 : _b.remove();
const validationContainer = oldTextarea.parentElement;
validationContainer === null || validationContainer === void 0 ? void 0 : validationContainer.replaceWith(oldTextarea);
return;
}
const { state, description } = validation;
textareaParent.classList.add(`has-${state}`);
const [iconName, iconPath] = icons_exports.validationIcons[state];
const [icon] = icons_exports.makeStacksIcon(iconName, iconPath, {
classes: ["s-input-icon"],
width: 18
});
if (oldTextarea.nextElementSibling) {
oldTextarea.nextElementSibling.replaceWith(icon);
const inputMessage = textareaParent.querySelector(".s-input-message");
if (description) {
if (inputMessage) {
inputMessage.innerHTML = description;
} else {
createAndAppendDescription(description, textareaParent);
}
} else if (!description && inputMessage) {
inputMessage.remove();
}
} else {
const validationContainer = document.createElement("div");
validationContainer.classList.add("d-flex", "ps-relative");
validationContainer.append(oldTextarea, icon);
textareaParent.append(validationContainer);
if (description) {
createAndAppendDescription(description, textareaParent);
}
}
};
var createAndAppendDescription = (description, appendTo) => {
const message = document.createElement("p");
message.classList.add("flex--item", "s-input-message");
message.innerHTML = description;
appendTo.append(message);
};
// node_modules/@userscripters/stacks-helpers/dist/toggle.js
var toggle_exports = {};
__export(toggle_exports, {
makeStacksToggle: () => makeStacksToggle
});
var makeStacksToggle = (id, labelOptions, on = false, ...classes) => {
const container = document.createElement("div");
container.classList.add("d-flex", "g8", "ai-center", ...classes);
const label = label_exports.makeStacksLabel(id, labelOptions);
const toggle = document.createElement("input");
toggle.id = id;
toggle.classList.add("s-toggle-switch");
toggle.type = "checkbox";
toggle.checked = on;
container.append(label, toggle);
return container;
};
// node_modules/@userscripters/stacks-helpers/dist/buttons/index.js
var buttons_exports = {};
__export(buttons_exports, {
makeStacksButton: () => makeStacksButton
});
var makeStacksButton = (id, text, options = {}) => {
const { title, type = [], primary = false, loading = false, selected = false, disabled = false, badge, size, iconConfig, click, classes = [] } = options;
const btn = document.createElement("button");
if (id !== "") {
btn.id = id;
}
btn.classList.add("s-btn", ...type.map((name) => `s-btn__${name}`), ...classes);
btn.append(text);
btn.type = "button";
btn.setAttribute("role", "button");
const ariaLabel = title || (text instanceof HTMLElement ? text.textContent || "" : text);
btn.setAttribute("aria-label", ariaLabel);
if (primary) {
btn.classList.add("s-btn__filled");
}
if (loading) {
btn.classList.add("is-loading");
}
if (title) {
btn.title = title;
}
if (selected) {
btn.classList.add("is-selected");
}
if (disabled) {
btn.disabled = true;
}
if (badge) {
const badgeEl = document.createElement("span");
badgeEl.classList.add("s-btn--badge");
const badgeNumber = document.createElement("span");
badgeNumber.classList.add("s-btn--number");
badgeNumber.textContent = badge.toString();
badgeEl.append(badgeNumber);
btn.append(" ", badgeEl);
}
if (size) {
btn.classList.add(`s-btn__${size}`);
}
if (iconConfig) {
btn.classList.add("s-btn__icon");
const { name, path, width, height } = iconConfig;
const [icon] = icons_exports.makeStacksIcon(name, path, { width, height });
btn.prepend(icon, " ");
}
if (click) {
const { handler, options: options2 } = click;
btn.addEventListener("click", handler, options2);
}
return btn;
};
// node_modules/@userscripters/stacks-helpers/dist/icons/index.js
var icons_exports = {};
__export(icons_exports, {
makeStacksIcon: () => makeStacksIcon,
validationIcons: () => validationIcons
});
var validationIcons = {
warning: [
"iconAlert",
"M7.95 2.71c.58-.94 1.52-.94 2.1 0l7.69 12.58c.58.94.15 1.71-.96 1.71H1.22C.1 17-.32 16.23.26 15.29L7.95 2.71ZM8 6v5h2V6H8Zm0 7v2h2v-2H8Z"
],
error: [
"iconAlertCircle",
"M9 17c-4.36 0-8-3.64-8-8 0-4.36 3.64-8 8-8 4.36 0 8 3.64 8 8 0 4.36-3.64 8-8 8ZM8 4v6h2V4H8Zm0 8v2h2v-2H8Z"
],
success: [
"iconCheckmark",
"M16 4.41 14.59 3 6 11.59 2.41 8 1 9.41l5 5 10-10Z"
]
};
var makeStacksIcon = (name, pathConfig, { classes = [], width = 14, height = width } = {}) => {
const ns = "http://www.w3.org/2000/svg";
const svg = document.createElementNS(ns, "svg");
svg.classList.add("svg-icon", name, ...classes);
svg.setAttribute("width", width.toString());
svg.setAttribute("height", height.toString());
svg.setAttribute("viewBox", `0 0 ${width} ${height}`);
svg.setAttribute("aria-hidden", "true");
const path = document.createElementNS(ns, "path");
path.setAttribute("d", pathConfig);
svg.append(path);
return [svg, path];
};
// node_modules/@userscripters/stacks-helpers/dist/modals/index.js
var modals_exports = {};
__export(modals_exports, {
makeStacksModal: () => makeStacksModal
});
var makeStacksModal = (id, options) => {
const { classes = [], danger = false, fullscreen = false, celebratory = false, title: { text, id: titleId, classes: titleClasses = [] }, body: { bodyHtml, id: bodyId, classes: bodyClasses = [] }, footer: { buttons, classes: footerClasses = [] } } = options;
const modal = document.createElement("aside");
modal.id = id;
modal.classList.add("s-modal", ...classes);
modal.setAttribute("role", "dialog");
modal.setAttribute("data-controller", "s-modal");
modal.setAttribute("data-s-modal-target", "modal");
if (danger) {
modal.classList.add("s-modal__danger");
}
if (celebratory) {
modal.classList.add("s-modal__celebration");
}
const dialog = document.createElement("div");
dialog.classList.add("s-modal--dialog");
dialog.setAttribute("role", "document");
if (fullscreen) {
dialog.classList.add("s-modal__full");
}
const header = document.createElement("h1");
header.classList.add("s-modal--header", ...titleClasses);
header.append(text);
if (titleId) {
header.id = titleId;
modal.setAttribute("aria-labelledby", titleId);
}
const body = document.createElement("p");
body.classList.add("s-modal--body", ...bodyClasses);
body.append(bodyHtml);
if (bodyId) {
body.id = bodyId;
modal.setAttribute("aria-describedby", bodyId);
}
const footer = document.createElement("div");
footer.classList.add("d-flex", "gx8", "s-modal--footer", ...footerClasses);
buttons.forEach((button) => {
const { element, hideOnClick } = button;
element.classList.add("flex--item");
if (hideOnClick) {
element.setAttribute("data-action", "s-modal#hide");
}
footer.append(element);
});
const [iconClear] = icons_exports.makeStacksIcon("iconClear", "M15 4.41 13.59 3 9 7.59 4.41 3 3 4.41 7.59 9 3 13.59 4.41 15 9 10.41 13.59 15 15 13.59 10.41 9 15 4.41Z", { width: 18 });
const close = document.createElement("button");
close.classList.add("s-modal--close", "s-btn", "s-btn__muted");
close.setAttribute("type", "button");
close.setAttribute("aria-label", "Close");
close.setAttribute("data-action", "s-modal#hide");
close.append(iconClear);
dialog.append(header, body, footer, close);
modal.append(dialog);
return modal;
};
// src/UserscriptTools/Progress.ts
var Progress = class {
constructor(controller) {
this.controller = controller;
this.element = this.getPopover();
}
element;
attach() {
if (!this.controller) return;
Stacks.attachPopover(this.controller, this.element, {
autoShow: true,
placement: "bottom-start",
toggleOnClick: true
});
this.element.style.display = "none";
}
updateLocation() {
const controller = document.querySelector(
'.s-spinner[aria-controls="advanced-flagging-progress-popover"]'
);
if (!controller) return;
Stacks.hidePopover(controller);
Stacks.showPopover(controller);
}
delete() {
if (this.controller) {
Stacks.detachPopover(this.controller);
}
this.element.remove();
}
addItem(text) {
this.element.style.display = "";
const flexItem = this.createItem(text);
const wrapper = flexItem.firstElementChild;
this.element.lastElementChild?.append(flexItem);
return {
completed: () => this.completed(wrapper),
failed: (reason) => this.failed(wrapper, reason),
addSubItem: (text2) => this.addSubItem(flexItem, text2)
};
}
createItem(text) {
const flexItem = document.createElement("div");
flexItem.classList.add("flex--item");
const wrapper = document.createElement("div");
wrapper.classList.add("d-flex", "g8", "fd-row");
const action = document.createElement("div");
action.classList.add("flex--item");
action.textContent = text;
const spinner = spinner_exports.makeSpinner({
size: "sm",
classes: ["flex--item"]
});
wrapper.append(spinner, action);
flexItem.append(wrapper);
return flexItem;
}
completed(wrapper) {
const done = document.createElement("div");
done.classList.add("flex--item", "fc-green-500", "fw-bold");
done.textContent = "done!";
const tick = Post.getActionIcons()[0];
tick.style.display = "block";
wrapper.querySelector(".s-spinner")?.remove();
wrapper.prepend(tick);
wrapper.append(done);
}
failed(wrapper, reason) {
const failed = document.createElement("div");
failed.classList.add("flex--item", "fc-red-500", "fw-bold");
failed.textContent = `failed${reason ? `: ${reason}` : "!"}`;
const cross = Post.getActionIcons()[1];
cross.style.display = "block";
wrapper.querySelector(".s-spinner")?.remove();
wrapper.prepend(cross);
wrapper.append(failed);
}
addSubItem(div, text) {
const parent = this.createItem(text);
parent.classList.add("ml24", "mt4");
parent.classList.remove("flex--item");
div.append(parent);
const wrapper = parent.firstElementChild;
return {
completed: () => this.completed(wrapper),
failed: (reason) => this.failed(wrapper, reason),
addSubItem: (text2) => this.addSubItem(parent, text2)
};
}
getPopover() {
const popover = document.createElement("div");
popover.classList.add("s-popover", "wmn4");
popover.id = "advanced-flagging-progress-popover";
const arrow = document.createElement("div");
arrow.classList.add("s-popover--arrow");
const wrapper = document.createElement("div");
wrapper.classList.add("d-flex", "g8", "fd-column");
popover.append(arrow, wrapper);
return popover;
}
};
// src/shared.ts
var possibleFeedbacks = {
Smokey: ["tpu-", "tp-", "fp-", "naa-", ""],
Natty: ["tp", "fp", "ne", ""],
Guttenberg: ["tp", "fp", ""],
"Generic Bot": ["track", ""]
};
var username = document.querySelector(
'a[href^="/users/"] div[title]'
)?.title ?? "";
var popupDelay = 4 * 1e3;
var getIconPath = (name) => {
const element = GM_getResourceText(name);
const parsed = new DOMParser().parseFromString(element, "text/html");
const path = parsed.body.querySelector("path");
return path.getAttribute("d") ?? "";
};
var getSvg = (name) => {
const element = GM_getResourceText(name);
const parsed = new DOMParser().parseFromString(element, "text/html");
return parsed.body.firstElementChild;
};
function displayStacksToast(message, type, dismissable) {
StackExchange.helpers.showToast(message, {
type,
transientTimeout: popupDelay,
// disallow dismissing the popup if inside modal
dismissable
// so that dismissing the toast won't close the modal
// $parent: addParent ? $(parent) : $()
});
}
function attachPopover(element, text, position = "bottom-start") {
Stacks.setTooltipText(
element,
text,
{ placement: position }
);
}
function getFormDataFromObject(object) {
return Object.keys(object).reduce((formData, key) => {
formData.append(key, object[key]);
return formData;
}, new FormData());
}
async function delay(milliseconds) {
return new Promise((resolve) => setTimeout(resolve, milliseconds));
}
var callbacks = [];
var postIds = [];
function addXHRListener(callback, postId) {
if (postId && postIds.includes(postId)) return;
else if (postId) postIds.push(postId);
callbacks.push(callback);
}
function interceptXhr() {
const open = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function() {
this.addEventListener("load", () => {
callbacks.forEach((cb) => setTimeout(() => cb(this)));
}, false);
open.apply(this, arguments);
};
}
function getFullFlag(flagType, target, postId) {
const placeholderTarget = /\$TARGET\$/g;
const placeholderCopypastorLink = /\$COPYPASTOR\$/g;
const content = flagType.flagText;
if (!content) return null;
const copypastorLink = `https://copypastor.sobotics.org/posts/${postId}`;
return content.replace(placeholderTarget, `https:${target}`).replace(placeholderCopypastorLink, copypastorLink);
}
function getFlagTypeFromFlagId(flagId) {
return Store.flagTypes.find(({ id }) => id === flagId) ?? null;
}
function getHumanFromDisplayName(displayName) {
const flags = {
["PostSpam" /* Spam */]: "as spam",
["PostOffensive" /* Rude */]: "as R/A",
["AnswerNotAnAnswer" /* NAA */]: "as NAA",
["PostLowQuality" /* VLQ */]: "as VLQ",
["NoFlag" /* NoFlag */]: "",
["PlagiarizedContent" /* Plagiarism */]: "for plagiarism",
["PostOther" /* ModFlag */]: "for moderator attention"
};
return flags[displayName] || "";
}
function toggleLoading(button) {
button.classList.toggle("is-loading");
button.ariaDisabled = button.ariaDisabled === "true" ? "false" : "true";
button.disabled = !button.disabled;
}
async function addProgress(event, flagType, post = new Page(true).posts[0]) {
const input = document.querySelector("#advanced-flagging-flag-post");
if (!post.filterReporters(flagType.feedbacks).length && !input?.checked) return;
event.preventDefault();
event.stopPropagation();
const target = event.target;
toggleLoading(target);
post.progress = new Progress(target);
post.progress.attach();
if (input?.checked && !StackExchange.options.user.isModerator) {
const flagProgress = post.progress.addItem("Flagging as NAA...");
try {
await post.flag("AnswerNotAnAnswer" /* NAA */, null);
flagProgress.completed();
} catch (error) {
console.error(error);
flagProgress.failed(
error instanceof Error ? error.message : "see console for more details"
);
}
}
try {
await post.sendFeedbacks(flagType);
} finally {
await delay(1e3);
toggleLoading(target);
target.click();
}
}
function appendLabelAndBoxes(element, post) {
const label = label_exports.makeStacksLabel(
"noid",
{
text: "Send feedback to:",
classes: ["mt2", "fw-normal"]
}
);
const boxes = Object.entries(post.getFeedbackBoxes(true)).map(([, box]) => box);
const [, ...checkboxes] = checkbox_exports.makeStacksCheckboxes(
boxes,
{ horizontal: true }
);
checkboxes.forEach((box) => box.classList.add("flex--item"));
element.parentElement?.append(label, ...checkboxes);
}
// src/UserscriptTools/ChatApi.ts
var ChatApi = class _ChatApi {
constructor(chatUrl = "https://chat.stackoverflow.com", roomId = 111347) {
this.chatUrl = chatUrl;
this.roomId = roomId;
}
nattyId = 6817005;
getChatUserId() {
return StackExchange.options.user.userId;
}
async sendMessage(message) {
let numTries = 0;
const makeRequest = async () => {
return await this.sendRequestToChat(message);
};
const onFailure = async () => {
numTries++;
if (numTries < 3) {
Store.unset(Cached.Fkey);
if (!await makeRequest()) {
return onFailure();
}
} else {
throw new Error("Failed to send message to chat");
}
return true;
};
if (!await makeRequest()) {
return onFailure();
}
return true;
}
async getFinalUrl() {
const url = await this.getWsUrl();
const l = await this.getLParam();
return `${url}?l=${l}`;
}
reportReceived(event) {
const data = JSON.parse(event.data);
return data[`r${this.roomId}`].e?.filter(({ event_type, user_id }) => {
return event_type === 1 && user_id === this.nattyId;
}).map((item) => {
const { content } = item;
if (Store.dryRun) {
console.log("New message posted by Natty on room", this.roomId, item);
}
const matchRegex = /stackoverflow\.com\/a\/(\d+)/;
const id = matchRegex.exec(content)?.[1];
return Number(id);
}) ?? [];
}
static getExpiryDate() {
const expiryDate = /* @__PURE__ */ new Date();
expiryDate.setDate(expiryDate.getDate() + 1);
return expiryDate;
}
async sendRequestToChat(message) {
const url = `${this.chatUrl}/chats/${this.roomId}/messages/new`;
if (Store.dryRun) {
console.log("Send", message, `to ${this.roomId} via`, url);
return Promise.resolve(true);
}
const fkey = await this.getChannelFKey();
return new Promise((resolve) => {
GM_xmlhttpRequest({
method: "POST",