forked from utags/userscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
1320 lines (1171 loc) · 36.4 KB
/
Copy pathindex.ts
File metadata and controls
1320 lines (1171 loc) · 36.4 KB
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
import { isTopFrame } from '../../utils/is-top-frame'
import { registerValueChangeListenerTests } from './tests/value-change-listener'
/* eslint-disable @typescript-eslint/naming-convention */
declare const GM_info: any
declare const GM_setValue: any
declare const GM_getValue: any
declare const GM_deleteValue: any
declare const GM_listValues: any
declare const GM_addStyle: any
declare const GM_registerMenuCommand: any
declare const GM_unregisterMenuCommand: any
declare const GM_xmlhttpRequest: any
declare const GM_getResourceText: any
declare const GM_getResourceURL: any
declare const GM_notification: any
declare const unsafeWindow: any
declare const GM_download: any
declare const GM_openInTab: any
declare const GM_setClipboard: any
declare const GM_getTab: any
declare const GM_saveTab: any
declare const GM_getTabs: any
declare const GM_cookie: any
declare const GM_webRequest: any
declare const GM_addElement: any
declare const GM_log: any
declare const GM_setValues: any
declare const GM_getValues: any
declare const GM_deleteValues: any
declare const GM_addValueChangeListener: any
declare const GM_removeValueChangeListener: any
declare const GM: any
declare const GM_audio: any
/* eslint-enable @typescript-eslint/naming-convention */
type TestResult = {
supported: boolean
passed: number
total: number
message?: string
error?: any
}
type ApiTest = {
name: string
gmRun: () => Promise<TestResult> | TestResult
gmDotRun?: () => Promise<TestResult> | TestResult
}
/* eslint-disable unicorn/no-typeof-undefined */
const tests: ApiTest[] = []
function registerTest(
name: string,
gmRun: () => Promise<TestResult> | TestResult,
gmDotRun?: () => Promise<TestResult> | TestResult
) {
tests.push({ name, gmRun, gmDotRun })
}
// --- Utils ---
const isPromise = (value: any) =>
value &&
typeof value.then === 'function' &&
Object.prototype.toString.call(value) === '[object Promise]'
// eslint-disable-next-line @typescript-eslint/no-restricted-types
const readClipboard = async (): Promise<string | null> => {
if (typeof navigator.clipboard?.readText === 'function') {
try {
return await navigator.clipboard.readText()
} catch {
// Ignore
}
}
try {
const textarea = document.createElement('textarea')
textarea.style.position = 'fixed'
textarea.style.left = '-9999px'
document.body.append(textarea)
textarea.focus()
// eslint-disable-next-line @typescript-eslint/no-deprecated
const successful = document.execCommand('paste')
const val = textarea.value
textarea.remove()
if (successful) return val
} catch {
// Ignore
}
return null
}
let isGmSetClipboardWorking = false
let isGmDotSetClipboardWorking = false
// --- Tests ---
registerTest(
'info',
() => {
const supported = typeof GM_info !== 'undefined'
return { supported, passed: supported ? 1 : 0, total: 1 }
},
async () => {
const supported =
typeof GM !== 'undefined' && typeof GM.info !== 'undefined'
return { supported, passed: supported ? 1 : 0, total: 1 }
}
)
registerTest(
'log',
() => {
const supported = typeof GM_log === 'function'
if (supported) {
GM_log('Benchmark log test')
}
return { supported, passed: supported ? 1 : 0, total: 1 }
},
async () => {
const supported = typeof GM !== 'undefined' && typeof GM.log === 'function'
if (supported) {
GM.log('Benchmark log test')
}
return { supported, passed: supported ? 1 : 0, total: 1 }
}
)
registerTest(
'setValue / getValue',
async () => {
if (
typeof GM_setValue !== 'function' ||
typeof GM_getValue !== 'function'
) {
return { supported: false, passed: 0, total: 3 }
}
const key = 'benchmark_gm_key'
const val = 'test-' + Math.random()
GM_setValue(key, val)
// Check return type (should NOT be promise)
const retrievedRaw = GM_getValue(key)
let passed = 0
if (!isPromise(retrievedRaw)) {
passed++
}
// Check value correctness
const retrieved = await retrievedRaw
if (retrieved === val) {
passed++
}
GM_deleteValue(key)
// Check deep copy
const keyObj = 'benchmark_gm_key_obj'
const valObj = { a: 1, b: { c: 2 } }
GM_setValue(keyObj, valObj)
const v1 = GM_getValue(keyObj)
if (v1 && typeof v1 === 'object') {
v1.a = 999
v1.b.c = 888
}
const v2 = GM_getValue(keyObj)
if (v2.a === 1 && v2.b.c === 2) {
passed++
} else {
console.warn('GM_getValue should return a deep copy', v2)
}
GM_deleteValue(keyObj)
return { supported: true, passed, total: 3 }
},
async () => {
if (
typeof GM === 'undefined' ||
typeof GM.setValue !== 'function' ||
typeof GM.getValue !== 'function'
) {
return { supported: false, passed: 0, total: 3 }
}
const key = 'benchmark_gm4_key'
const val = 'gm4-' + Math.random()
await GM.setValue(key, val)
// Check return type (should be promise)
const retrievedRaw = GM.getValue(key)
let passed = 0
if (isPromise(retrievedRaw)) {
passed++
} else {
console.warn('getValue should return a promise')
}
// Check value correctness
const retrieved = await retrievedRaw
if (retrieved === val) {
passed++
} else {
console.warn('getValue should return the correct value')
}
await GM.deleteValue(key)
// Check deep copy
const keyObj = 'benchmark_gm4_key_obj'
const valObj = { a: 1, b: { c: 2 } }
await GM.setValue(keyObj, valObj)
const v1 = await GM.getValue(keyObj)
if (v1 && typeof v1 === 'object') {
v1.a = 999
v1.b.c = 888
}
const v2 = await GM.getValue(keyObj)
if (v2.a === 1 && v2.b.c === 2) {
passed++
} else {
console.warn('GM.getValue should return a deep copy', v2)
}
await GM.deleteValue(keyObj)
return { supported: true, passed, total: 3 }
}
)
registerTest(
'deleteValue',
async () => {
if (typeof GM_deleteValue !== 'function')
return { supported: false, passed: 0, total: 1 }
const key = 'benchmark_del_key'
await GM_setValue(key, '1')
await GM_deleteValue(key)
const val = await GM_getValue(key)
return {
supported: true,
passed: val === undefined ? 1 : 0,
total: 1,
}
},
async () => {
if (typeof GM === 'undefined' || typeof GM.deleteValue !== 'function')
return { supported: false, passed: 0, total: 1 }
const key = 'benchmark_gm4_del_key'
await GM.setValue(key, '1')
await GM.deleteValue(key)
const val = await GM.getValue(key)
return {
supported: true,
passed: val === undefined ? 1 : 0,
total: 1,
}
}
)
registerTest(
'listValues',
async () => {
if (typeof GM_listValues !== 'function')
return { supported: false, passed: 0, total: 1 }
const key = 'benchmark_list_key'
await GM_setValue(key, '1')
const list = await GM_listValues()
await GM_deleteValue(key)
return { supported: true, passed: list.includes(key) ? 1 : 0, total: 1 }
},
async () => {
if (typeof GM === 'undefined' || typeof GM.listValues !== 'function')
return { supported: false, passed: 0, total: 1 }
const key = 'benchmark_gm4_list_key'
await GM.setValue(key, '1')
const list = await GM.listValues()
await GM.deleteValue(key)
return { supported: true, passed: list.includes(key) ? 1 : 0, total: 1 }
}
)
registerTest(
'setValues / getValues / deleteValues',
async () => {
if (
typeof GM_setValues !== 'function' ||
typeof GM_getValues !== 'function' ||
typeof GM_deleteValues !== 'function'
) {
return { supported: false, passed: 0, total: 1 }
}
const data = {
benchmark_set_values_key1: 'val1-' + Math.random(),
benchmark_set_values_key2: 12_345,
}
await GM_setValues(data)
const retrieved = await GM_getValues(Object.keys(data))
await GM_deleteValues(Object.keys(data))
const passed =
retrieved.benchmark_set_values_key1 === data.benchmark_set_values_key1 &&
retrieved.benchmark_set_values_key2 === data.benchmark_set_values_key2
return { supported: true, passed: passed ? 1 : 0, total: 1 }
},
async () => {
if (
typeof GM === 'undefined' ||
typeof GM.setValues !== 'function' ||
typeof GM.getValues !== 'function' ||
typeof GM.deleteValues !== 'function'
) {
return { supported: false, passed: 0, total: 1 }
}
const data = {
benchmark_set_values_gm4_key1: 'val1-' + Math.random(),
benchmark_set_values_gm4_key2: 12_345,
}
await GM.setValues(data)
const retrieved = await GM.getValues(Object.keys(data))
await GM.deleteValues(Object.keys(data))
const passed =
retrieved.benchmark_set_values_gm4_key1 ===
data.benchmark_set_values_gm4_key1 &&
retrieved.benchmark_set_values_gm4_key2 ===
data.benchmark_set_values_gm4_key2
return { supported: true, passed: passed ? 1 : 0, total: 1 }
}
)
registerValueChangeListenerTests(registerTest)
registerTest(
'addStyle',
() => {
if (typeof GM_addStyle !== 'function')
return { supported: false, passed: 0, total: 1 }
try {
const el = GM_addStyle('.gm-test-style { display: none; }')
const passed = el instanceof HTMLStyleElement ? 1 : 0
if (!passed) {
console.warn('addStyle should return a style element', el, typeof el)
}
return { supported: true, passed, total: 1 }
} catch {
return { supported: true, passed: 0, total: 1 }
}
},
async () => {
if (typeof GM === 'undefined' || typeof GM.addStyle !== 'function')
return { supported: false, passed: 0, total: 1 }
try {
const el = await GM.addStyle('.gm4-test-style { display: none; }')
const passed = el instanceof HTMLStyleElement ? 1 : 0
if (!passed) {
console.warn('addStyle should return a style element', el, typeof el)
}
return { supported: true, passed, total: 1 }
} catch {
return { supported: true, passed: 0, total: 1 }
}
}
)
registerTest(
'addElement',
() => {
if (typeof GM_addElement !== 'function')
return { supported: false, passed: 0, total: 6 }
let passed = 0
const total = 6
const id = 'gm-add-element-test'
// 1. GM_addElement(tag_name, attributes)
try {
const el = GM_addElement('div', { id: `${id}-default` })
if (el && el.tagName === 'DIV' && el.parentNode) {
passed++
el.remove()
}
} catch {}
// 2. GM_addElement(document.body, tag_name, attributes)
try {
const el = GM_addElement(document.body, 'div', { id: `${id}-body` })
if (el && el.tagName === 'DIV' && el.parentNode === document.body) {
passed++
el.remove()
}
} catch {}
// 3. GM_addElement(document.head, tag_name, attributes)
try {
const el = GM_addElement(document.head, 'div', { id: `${id}-head` })
if (el && el.tagName === 'DIV' && el.parentNode === document.head) {
passed++
el.remove()
}
} catch {}
// 4. GM_addElement(document.documentElement, tag_name, attributes)
try {
const el = GM_addElement(document.documentElement, 'div', {
id: `${id}-doc`,
})
if (
el &&
el.tagName === 'DIV' &&
el.parentNode === document.documentElement
) {
passed++
el.remove()
}
} catch {}
// 5. GM_addElement(null, tag_name, attributes)
try {
const el = GM_addElement(null, 'div', { id: `${id}-null` })
if (el && el.tagName === 'DIV' && el.parentNode) {
passed++
el.remove()
} else {
console.warn(
'addElement should return a div element when parent is null',
el,
typeof el
)
}
} catch {}
// 6. GM_addElement(undefined, tag_name, attributes)
try {
const el = GM_addElement(undefined, 'div', { id: `${id}-undefined` })
if (el && el.tagName === 'DIV' && el.parentNode) {
passed++
el.remove()
} else {
console.warn(
'addElement should return a div element when parent is undefined',
el,
typeof el
)
}
} catch {}
return { supported: true, passed, total }
},
async () => {
if (typeof GM === 'undefined' || typeof GM.addElement !== 'function')
return { supported: false, passed: 0, total: 6 }
let passed = 0
const total = 6
const id = 'gm4-add-element-test'
// 1. GM.addElement(tag_name, attributes)
try {
const el = await GM.addElement('div', { id: `${id}-default` })
if (el && el.tagName === 'DIV' && el.parentNode) {
passed++
el.remove()
}
} catch {}
// 2. GM.addElement(document.body, tag_name, attributes)
try {
const el = await GM.addElement(document.body, 'div', { id: `${id}-body` })
if (el && el.tagName === 'DIV' && el.parentNode === document.body) {
passed++
el.remove()
}
} catch {}
// 3. GM.addElement(document.head, tag_name, attributes)
try {
const el = await GM.addElement(document.head, 'div', { id: `${id}-head` })
if (el && el.tagName === 'DIV' && el.parentNode === document.head) {
passed++
el.remove()
}
} catch {}
// 4. GM.addElement(document.documentElement, tag_name, attributes)
try {
const el = await GM.addElement(document.documentElement, 'div', {
id: `${id}-doc`,
})
if (
el &&
el.tagName === 'DIV' &&
el.parentNode === document.documentElement
) {
passed++
el.remove()
}
} catch {}
// 5. GM.addElement(null, tag_name, attributes)
try {
const el = await GM.addElement(null, 'div', { id: `${id}-null` })
if (el && el.tagName === 'DIV' && el.parentNode) {
passed++
el.remove()
} else {
console.warn(
'addElement should return a div element when parent is null',
el,
typeof el
)
}
} catch {}
// 6. GM.addElement(undefined, tag_name, attributes)
try {
const el = await GM.addElement(undefined, 'div', {
id: `${id}-undefined`,
})
if (el && el.tagName === 'DIV' && el.parentNode) {
passed++
el.remove()
} else {
console.warn(
'addElement should return a div element when parent is undefined',
el,
typeof el
)
}
} catch {}
return { supported: true, passed, total }
}
)
registerTest(
'registerMenuCommand',
() => {
if (typeof GM_registerMenuCommand !== 'function')
return { supported: false, passed: 0, total: 1 }
return { supported: true, passed: 1, total: 1 }
},
async () => {
if (
typeof GM === 'undefined' ||
typeof GM.registerMenuCommand !== 'function'
)
return { supported: false, passed: 0, total: 1 }
return { supported: true, passed: 1, total: 1 }
}
)
registerTest(
'unregisterMenuCommand',
() => {
if (typeof GM_unregisterMenuCommand !== 'function')
return { supported: false, passed: 0, total: 1 }
return { supported: true, passed: 1, total: 1 }
},
async () => {
if (
typeof GM === 'undefined' ||
typeof GM.unregisterMenuCommand !== 'function'
)
return { supported: false, passed: 0, total: 1 }
return { supported: true, passed: 1, total: 1 }
}
)
registerTest(
'xmlHttpRequest',
() => {
if (typeof GM_xmlhttpRequest !== 'function')
return { supported: false, passed: 0, total: 1 }
// Basic existence check is enough for compatibility chart usually,
// but let's try a dummy call if we can, or just assume passed if function exists.
return { supported: true, passed: 1, total: 1 }
},
async () => {
const supported =
typeof GM !== 'undefined' && typeof GM.xmlHttpRequest === 'function'
return { supported, passed: supported ? 1 : 0, total: 1 }
}
)
registerTest(
'download',
() => {
if (typeof GM_download !== 'function')
return { supported: false, passed: 0, total: 1 }
return { supported: true, passed: 1, total: 1 }
},
async () => {
if (typeof GM === 'undefined' || typeof GM.download !== 'function')
return { supported: false, passed: 0, total: 1 }
return { supported: true, passed: 1, total: 1 }
}
)
registerTest(
'openInTab',
() => {
if (typeof GM_openInTab !== 'function')
return { supported: false, passed: 0, total: 1 }
return { supported: true, passed: 1, total: 1 }
},
async () => {
if (typeof GM === 'undefined' || typeof GM.openInTab !== 'function')
return { supported: false, passed: 0, total: 1 }
return { supported: true, passed: 1, total: 1 }
}
)
registerTest(
'setClipboard',
async () => {
if (typeof GM_setClipboard !== 'function')
return { supported: false, passed: 0, total: 1 }
const secret = 'gm_' + Math.random().toString(36).slice(2)
try {
GM_setClipboard(secret)
// Wait for clipboard to update
await new Promise((resolve) => {
setTimeout(resolve, 100)
})
const text = await readClipboard()
console.log('GM_setClipboard: text', text)
if (text === null) {
// Can't verify whether write worked if read failed
isGmSetClipboardWorking = false
return {
supported: true,
passed: 1,
total: 1,
message: 'Write OK, Read blocked',
}
}
const passed = text === secret ? 1 : 0
if (passed) isGmSetClipboardWorking = true
return { supported: true, passed, total: 1 }
} catch (error) {
return { supported: true, passed: 0, total: 1, error }
}
},
async () => {
if (typeof GM === 'undefined' || typeof GM.setClipboard !== 'function')
return { supported: false, passed: 0, total: 1 }
const secret = 'gm.' + Math.random().toString(36).slice(2)
try {
await GM.setClipboard(secret)
// Wait for clipboard to update
await new Promise((resolve) => {
setTimeout(resolve, 100)
})
const text = await readClipboard()
console.log('GM.setClipboard: text', text)
if (text === null) {
// Can't verify whether write worked if read failed
isGmDotSetClipboardWorking = false
return {
supported: true,
passed: 1,
total: 1,
message: 'Write OK, Read blocked',
}
}
const passed = text === secret ? 1 : 0
if (passed) isGmDotSetClipboardWorking = true
return { supported: true, passed, total: 1 }
} catch (error) {
return { supported: true, passed: 0, total: 1, error }
}
}
)
registerTest(
'notification',
() => {
if (typeof GM_notification !== 'function')
return { supported: false, passed: 0, total: 1 }
return { supported: true, passed: 1, total: 1 }
},
async () => {
if (typeof GM === 'undefined' || typeof GM.notification !== 'function')
return { supported: false, passed: 0, total: 1 }
return { supported: true, passed: 1, total: 1 }
}
)
registerTest(
'getResourceText',
() => {
if (typeof GM_getResourceText !== 'function')
return { supported: false, passed: 0, total: 1 }
return { supported: true, passed: 1, total: 1 }
},
async () => {
if (typeof GM === 'undefined' || typeof GM.getResourceText !== 'function')
return { supported: false, passed: 0, total: 1 }
return { supported: true, passed: 1, total: 1 }
}
)
registerTest(
'getResourceURL',
() => {
if (typeof GM_getResourceURL !== 'function')
return { supported: false, passed: 0, total: 1 }
return { supported: true, passed: 1, total: 1 }
},
async () => {
if (typeof GM === 'undefined' || typeof GM.getResourceUrl !== 'function')
// Note: GM.getResourceUrl (lowercase 'rl' in some specs, check this)
// Usually GM.getResourceUrl
return { supported: false, passed: 0, total: 1 }
return { supported: true, passed: 1, total: 1 }
}
)
registerTest(
'getTab / saveTab / getTabs',
() => {
const s1 = typeof GM_getTab === 'function'
const s2 = typeof GM_saveTab === 'function'
const s3 = typeof GM_getTabs === 'function'
const supported = s1 && s2 && s3
return { supported, passed: supported ? 1 : 0, total: 1 }
},
async () => {
// GM.getTab etc are not standard in GM.4 usually, but let's check
if (
typeof GM === 'undefined' ||
typeof GM.getTab !== 'function' ||
typeof GM.saveTab !== 'function' ||
typeof GM.getTabs !== 'function'
)
return { supported: false, passed: 0, total: 1 }
return { supported: true, passed: 1, total: 1 }
}
)
registerTest(
'cookie',
() => {
const supported =
typeof GM_cookie !== 'undefined' &&
(typeof GM_cookie.list === 'function' || typeof GM_cookie === 'function')
return { supported, passed: supported ? 1 : 0, total: 1 }
},
async () => {
if (typeof GM === 'undefined' || typeof GM.cookie === 'undefined')
return { supported: false, passed: 0, total: 1 }
return { supported: true, passed: 1, total: 1 }
}
)
registerTest(
'audio',
() => {
// Note: This API is not standard in all managers, mainly checking existence.
// Since it requires user interaction or audio context, we just check if defined.
const supported = typeof GM_audio !== 'undefined'
return { supported, passed: supported ? 1 : 0, total: 1 }
},
async () => {
// GM.audio is not standard, but we check anyway.
const supported =
typeof GM !== 'undefined' && typeof GM.audio !== 'undefined'
return { supported, passed: supported ? 1 : 0, total: 1 }
}
)
registerTest(
'webRequest (Deprecated)',
() => {
const supported = typeof GM_webRequest === 'function'
return { supported, passed: supported ? 1 : 0, total: 1 }
},
async () => {
if (typeof GM === 'undefined' || typeof GM.webRequest !== 'function')
return { supported: false, passed: 0, total: 1 }
return { supported: true, passed: 1, total: 1 }
}
)
registerTest('unsafeWindow', () => {
const supported = typeof unsafeWindow !== 'undefined'
return { supported, passed: supported ? 1 : 0, total: 1 }
})
registerTest('window.onurlchange', () => {
// Check if the event handler is supported on window
const supported =
// eslint-disable-next-line unicorn/prefer-global-this
'onurlchange' in window && window.onurlchange === null
return { supported, passed: supported ? 1 : 0, total: 1 }
})
registerTest('window.close', () => {
// Cannot easily test if it works without closing the tab, just check existence
const supported = typeof window.close === 'function'
return { supported, passed: supported ? 1 : 0, total: 1 }
})
registerTest('window.focus', () => {
// Cannot easily test if it works without window interaction, just check existence
const supported = typeof window.focus === 'function'
return { supported, passed: supported ? 1 : 0, total: 1 }
})
// --- UI ---
async function render() {
const hostId = 'data-benchmark-host'
// Remove existing if any (for hot reload)
const existing = document.querySelector(
`[${hostId}="userscript-compatibility"]`
)
if (existing) existing.remove()
const host = document.createElement('div')
host.setAttribute(hostId, 'userscript-compatibility')
const shadow = host.attachShadow({ mode: 'open' })
const style = document.createElement('style')
style.textContent = `
:host {
position: fixed;
top: 20px;
right: 20px;
z-index: 2147483647;
background: #fff;
color: #333;
padding: 16px;
border-radius: 8px;
box-shadow: 0 4px 12px rgba(0,0,0,0.2);
font-family: sans-serif;
max-height: 90vh;
overflow-y: auto;
width: 600px;
font-size: 13px;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 10px;
}
th,
td {
border: 1px solid #eee;
padding: 6px 8px;
text-align: left;
word-break: break-word;
}
th { background: #f9f9f9; font-weight: 600; }
.pass { color: #2ecc71; font-weight: bold; }
.fail { color: #e74c3c; font-weight: bold; }
.na { color: #f59e0b; font-weight: bold; }
.header h3 { margin: 0 0 8px 0; font-size: 16px; }
.close {
position: absolute;
top: 10px;
right: 10px;
cursor: pointer;
font-size: 16px;
color: #999;
}
.close:hover { color: #333; }
.copy-btn {
position: absolute;
top: 10px;
right: 40px;
cursor: pointer;
font-size: 13px;
color: #007aff;
border: 1px solid #007aff;
padding: 2px 8px;
border-radius: 4px;
background: transparent;
}
.copy-btn:hover { background: #007aff; color: #fff; }
.copy-btn:active { transform: translateY(1px); }
.log-area {
margin-top: 16px;
padding: 10px;
background: #f5f5f5;
border: 1px solid #ddd;
border-radius: 4px;
font-family: monospace;
font-size: 11px;
max-height: 150px;
overflow-y: auto;
white-space: pre-wrap;
}
.log-entry { margin-bottom: 4px; border-bottom: 1px solid #eee; padding-bottom: 4px; }
.log-entry:last-child { border-bottom: none; margin-bottom: 0; }
.log-entry.error { color: #e74c3c; }
.log-entry.warning { color: #f59e0b; }
@media (max-width: 640px) {
:host {
top: 10px;
right: 10px;
left: 10px;
width: auto;
max-width: none;
max-height: calc(100vh - 20px);
padding: 12px;
font-size: 12px;
}
.header h3 { font-size: 14px; }
table { font-size: 12px; }
.copy-btn {
top: 8px;
right: 36px;
padding: 2px 6px;
}
.close {
top: 8px;
right: 8px;
}
}
`
shadow.append(style)
const wrapper = document.createElement('div')
// Header info
let handler = 'Unknown'
let version = 'Unknown'
if (typeof GM !== 'undefined' && typeof GM.info !== 'undefined') {
handler = GM.info.scriptHandler || handler
version = GM.info.version || version
console.log('GM.info', GM.info)
} else if (typeof GM_info !== 'undefined') {
handler = GM_info.scriptHandler || handler
version = GM_info.version || version
console.log('GM_info', GM_info)
}
// Browser info
const ua = navigator.userAgent
let browser = 'Unknown'
if (ua.includes('Chrome')) browser = 'Chrome'
else if (ua.includes('Firefox')) browser = 'Firefox'
else if (ua.includes('Safari')) browser = 'Safari'
const isMobile = /mobile|android|ip(hone|od|ad)/i.test(ua)
const platform = isMobile ? 'Mobile' : 'Desktop'
const browserInfo = `${browser} ${/(Chrome|Firefox|Safari)\/([\d.]+)/.exec(ua)?.[2] || ''} (${platform})`
wrapper.innerHTML = `
<div class="close" title="Close">×</div>
<button class="copy-btn" title="Copy as Markdown">Copy</button>
<div class="header">
<h3>Userscript API Benchmark</h3>
<div><strong>Manager:</strong> ${handler} (${version})</div>
<div><strong>Browser:</strong> ${browserInfo}</div>
</div>
<table>
<thead>
<tr>
<th rowspan="2">API</th>
<th colspan="2">GM.* (Promise)</th>
<th colspan="2">GM_* (Callback/Sync)</th>
</tr>
<tr>
<th>Support</th>
<th>Pass Rate</th>
<th>Support</th>
<th>Pass Rate</th>
</tr>
</thead>
<tbody id="benchmark-results-body"></tbody>
</table>
<div class="log-area" id="benchmark-log"></div>
`