-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuserscript-api-benchmark.user.js
More file actions
879 lines (879 loc) · 30.9 KB
/
Copy pathuserscript-api-benchmark.user.js
File metadata and controls
879 lines (879 loc) · 30.9 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
// ==UserScript==
// @name Userscript API Benchmark
// @name:zh-CN 用户脚本 API 基准测试
// @namespace https://github.com/utags/userscripts
// @version 0.1.1
// @description Comprehensive benchmark tool for UserScript Manager APIs (GM.* and GM_*)
// @description:zh-CN 用户脚本管理器 API (GM.* 和 GM_*) 的综合基准测试工具,用于检查兼容性与准确性
// @author Pipecraft
// @noframes
// @match *://*/*
// @grant unsafeWindow
// @grant window.close
// @grant window.focus
// @grant window.onurlchange
// @grant GM_info
// @grant GM.info
// @grant GM_log
// @grant GM.log
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_deleteValue
// @grant GM.setValue
// @grant GM.getValue
// @grant GM.deleteValue
// @grant GM_listValues
// @grant GM.listValues
// @grant GM_setValues
// @grant GM_getValues
// @grant GM_deleteValues
// @grant GM.setValues
// @grant GM.getValues
// @grant GM.deleteValues
// @grant GM_addValueChangeListener
// @grant GM_removeValueChangeListener
// @grant GM.addValueChangeListener
// @grant GM.removeValueChangeListener
// @grant GM_addStyle
// @grant GM.addStyle
// @grant GM_addElement
// @grant GM.addElement
// @grant GM_registerMenuCommand
// @grant GM.registerMenuCommand
// @grant GM_unregisterMenuCommand
// @grant GM.unregisterMenuCommand
// @grant GM_xmlhttpRequest
// @grant GM.xmlHttpRequest
// @grant GM_download
// @grant GM.download
// @grant GM_openInTab
// @grant GM.openInTab
// @grant GM_setClipboard
// @grant GM.setClipboard
// @grant GM_notification
// @grant GM.notification
// @grant GM_getResourceText
// @grant GM.getResourceText
// @grant GM_getResourceURL
// @grant GM.getResourceUrl
// @grant GM_getTab
// @grant GM_saveTab
// @grant GM_getTabs
// @grant GM.getTab
// @grant GM.saveTab
// @grant GM.getTabs
// @grant GM_cookie
// @grant GM.cookie
// @grant GM_audio
// @grant GM.audio
// @grant GM_webRequest
// @grant GM.webRequest
// ==/UserScript==
//
;(() => {
'use strict'
var tests = []
function registerTest(name, gmRun, gmDotRun) {
tests.push({ name, gmRun, gmDotRun })
}
var isPromise = (value) =>
value &&
typeof value.then === 'function' &&
Object.prototype.toString.call(value) === '[object Promise]'
var readClipboard = async () => {
var _a
if (
typeof ((_a = navigator.clipboard) == null ? void 0 : _a.readText) ===
'function'
) {
try {
return await navigator.clipboard.readText()
} catch (e) {}
}
try {
const textarea = document.createElement('textarea')
textarea.style.position = 'fixed'
textarea.style.left = '-9999px'
document.body.append(textarea)
textarea.focus()
const successful = document.execCommand('paste')
const val = textarea.value
textarea.remove()
if (successful) return val
} catch (e) {}
return null
}
var isGmSetClipboardWorking = false
var isGmDotSetClipboardWorking = false
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: 2 }
}
const key = 'benchmark_gm_key'
const val = 'test-' + Math.random()
GM_setValue(key, val)
const retrievedRaw = GM_getValue(key)
let passed = 0
if (!isPromise(retrievedRaw)) {
passed++
}
const retrieved = await retrievedRaw
if (retrieved === val) {
passed++
}
GM_deleteValue(key)
return { supported: true, passed, total: 2 }
},
async () => {
if (
typeof GM === 'undefined' ||
typeof GM.setValue !== 'function' ||
typeof GM.getValue !== 'function'
) {
return { supported: false, passed: 0, total: 2 }
}
const key = 'benchmark_gm4_key'
const val = 'gm4-' + Math.random()
await GM.setValue(key, val)
const retrievedRaw = GM.getValue(key)
let passed = 0
if (isPromise(retrievedRaw)) {
passed++
}
const retrieved = await retrievedRaw
if (retrieved === val) {
passed++
}
await GM.deleteValue(key)
return { supported: true, passed, total: 2 }
}
)
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 === void 0 ? 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 === void 0 ? 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_gm_key1: 'val1-' + Math.random(),
benchmark_gm_key2: 12345,
}
await GM_setValues(data)
const retrieved = await GM_getValues(Object.keys(data))
await GM_deleteValues(Object.keys(data))
const passed =
retrieved.benchmark_gm_key1 === data.benchmark_gm_key1 &&
retrieved.benchmark_gm_key2 === data.benchmark_gm_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_gm_key1: 'val1-' + Math.random(),
benchmark_gm_key2: 12345,
}
await GM.setValues(data)
const retrieved = await GM.getValues(Object.keys(data))
await GM.deleteValues(Object.keys(data))
const passed =
retrieved.benchmark_gm_key1 === data.benchmark_gm_key1 &&
retrieved.benchmark_gm_key2 === data.benchmark_gm_key2
return { supported: true, passed: passed ? 1 : 0, total: 1 }
}
)
registerTest(
'addValueChangeListener / removeValueChangeListener',
async () => {
if (
typeof GM_addValueChangeListener !== 'function' ||
typeof GM_removeValueChangeListener !== 'function' ||
typeof GM_setValue !== 'function'
) {
return { supported: false, passed: 0, total: 1 }
}
const key = 'benchmark_listener_key'
let triggered = false
const id = GM_addValueChangeListener(
key,
(name, oldVal, newVal, remote) => {
if (name === key && newVal === 'changed') {
triggered = true
}
}
)
await GM_setValue(key, 'changed')
await new Promise((resolve) => {
setTimeout(resolve, 200)
})
GM_removeValueChangeListener(id)
await GM_deleteValue(key)
return { supported: true, passed: triggered ? 1 : 0, total: 1 }
},
async () => {
if (
typeof GM === 'undefined' ||
typeof GM.addValueChangeListener !== 'function' ||
typeof GM.removeValueChangeListener !== 'function' ||
typeof GM.setValue !== 'function'
) {
return { supported: false, passed: 0, total: 1 }
}
const key = 'benchmark_listener_key'
let triggered = false
const id = await GM.addValueChangeListener(
key,
(name, oldVal, newVal, remote) => {
if (name === key && newVal === 'changed') {
triggered = true
}
}
)
await GM.setValue(key, 'changed')
await new Promise((resolve) => {
setTimeout(resolve, 200)
})
await GM.removeValueChangeListener(id)
await GM.deleteValue(key)
return { supported: true, passed: triggered ? 1 : 0, total: 1 }
}
)
registerTest(
'addStyle',
() => {
if (typeof GM_addStyle !== 'function')
return { supported: false, passed: 0, total: 1 }
try {
const el = GM_addStyle('.gm-test-style { display: none; }')
return { supported: true, passed: 1, total: 1 }
} catch (e) {
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; }')
return { supported: true, passed: 1, total: 1 }
} catch (e) {
return { supported: true, passed: 0, total: 1 }
}
}
)
registerTest(
'addElement',
() => {
if (typeof GM_addElement !== 'function')
return { supported: false, passed: 0, total: 1 }
try {
const el = GM_addElement('div', { id: 'gm-add-element-test' })
const passed = el && el.tagName === 'DIV' ? 1 : 0
return { supported: true, passed, total: 1 }
} catch (e) {
return { supported: true, passed: 0, total: 1 }
}
},
async () => {
if (typeof GM === 'undefined' || typeof GM.addElement !== 'function')
return { supported: false, passed: 0, total: 1 }
try {
const el = await GM.addElement('div', { id: 'gm4-add-element-test' })
const passed = el && el.tagName === 'DIV' ? 1 : 0
return { supported: true, passed, total: 1 }
} catch (e) {
return { supported: true, passed: 0, total: 1 }
}
}
)
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 }
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)
await new Promise((resolve) => {
setTimeout(resolve, 100)
})
const text = await readClipboard()
console.log('GM_setClipboard: text', text)
if (text === null) {
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)
await new Promise((resolve) => {
setTimeout(resolve, 100)
})
const text = await readClipboard()
console.log('GM.setClipboard: text', text)
if (text === null) {
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')
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 () => {
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',
() => {
const supported = typeof GM_audio !== 'undefined'
return { supported, passed: supported ? 1 : 0, total: 1 }
},
async () => {
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 globalThis.unsafeWindow !== 'undefined'
return { supported, passed: supported ? 1 : 0, total: 1 }
})
registerTest('window.onurlchange', () => {
const supported =
'onurlchange' in globalThis && globalThis.onurlchange === null
return { supported, passed: supported ? 1 : 0, total: 1 }
})
registerTest('window.close', () => {
const supported = typeof window.close === 'function'
return { supported, passed: supported ? 1 : 0, total: 1 }
})
registerTest('window.focus', () => {
const supported = typeof window.focus === 'function'
return { supported, passed: supported ? 1 : 0, total: 1 }
})
async function render() {
var _a
const hostId = 'data-benchmark-host'
const existing = document.querySelector(
'['.concat(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 =
'\n :host {\n position: fixed; top: 20px; right: 20px; z-index: 2147483647;\n background: #fff; color: #333; padding: 16px; border-radius: 8px;\n box-shadow: 0 4px 12px rgba(0,0,0,0.2); font-family: sans-serif;\n max-height: 90vh; overflow-y: auto; width: 600px;\n font-size: 13px;\n }\n table { width: 100%; border-collapse: collapse; margin-top: 10px; }\n th, td { border: 1px solid #eee; padding: 6px 8px; text-align: left; }\n th { background: #f9f9f9; font-weight: 600; }\n .pass { color: #2ecc71; font-weight: bold; }\n .fail { color: #e74c3c; font-weight: bold; }\n .na { color: #f59e0b; font-weight: bold; }\n .header h3 { margin: 0 0 8px 0; font-size: 16px; }\n .close { position: absolute; top: 10px; right: 10px; cursor: pointer; font-size: 16px; color: #999; }\n .close:hover { color: #333; }\n .copy-btn {\n position: absolute; top: 10px; right: 40px;\n cursor: pointer; font-size: 13px; color: #007aff; border: 1px solid #007aff;\n padding: 2px 8px; border-radius: 4px; background: transparent;\n }\n .copy-btn:hover { background: #007aff; color: #fff; }\n .copy-btn:active { transform: translateY(1px); }\n .log-area {\n margin-top: 16px;\n padding: 10px;\n background: #f5f5f5;\n border: 1px solid #ddd;\n border-radius: 4px;\n font-family: monospace;\n font-size: 11px;\n max-height: 150px;\n overflow-y: auto;\n white-space: pre-wrap;\n }\n .log-entry { margin-bottom: 4px; border-bottom: 1px solid #eee; padding-bottom: 4px; }\n .log-entry:last-child { border-bottom: none; margin-bottom: 0; }\n .log-entry.error { color: #e74c3c; }\n '
shadow.append(style)
const wrapper = document.createElement('div')
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)
}
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 browserInfo = ''
.concat(browser, ' ')
.concat(
((_a = /(Chrome|Firefox|Safari)\/([\d.]+)/.exec(ua)) == null
? void 0
: _a[2]) || ''
)
wrapper.innerHTML =
'\n <div class="close" title="Close">\xD7</div>\n <button class="copy-btn" title="Copy as Markdown">Copy</button>\n <div class="header">\n <h3>Userscript API Benchmark</h3>\n <div><strong>Manager:</strong> '
.concat(handler, ' (')
.concat(version, ')</div>\n <div><strong>Browser:</strong> ')
.concat(
browserInfo,
'</div>\n </div>\n <table>\n <thead>\n <tr>\n <th rowspan="2">API</th>\n <th colspan="2">GM.* (Promise)</th>\n <th colspan="2">GM_* (Callback/Sync)</th>\n </tr>\n <tr>\n <th>Support</th>\n <th>Pass Rate</th>\n <th>Support</th>\n <th>Pass Rate</th>\n </tr>\n </thead>\n <tbody id="benchmark-results-body"></tbody>\n </table>\n <div class="log-area" id="benchmark-log"></div>\n '
)
shadow.append(wrapper)
document.documentElement.append(host)
wrapper.querySelector('.close').addEventListener('click', () => {
host.remove()
})
const logArea = wrapper.querySelector('#benchmark-log')
const appendLog = (msg, type = 'info') => {
const entry = document.createElement('div')
entry.className = 'log-entry '.concat(type)
entry.textContent = '['
.concat(/* @__PURE__ */ new Date().toLocaleTimeString(), '] ')
.concat(msg)
logArea.append(entry)
logArea.scrollTop = logArea.scrollHeight
}
const resultsData = []
const copyBtn = wrapper.querySelector('.copy-btn')
copyBtn.addEventListener('click', async () => {
const lines = [
'# Userscript API Benchmark Results',
'',
'- **Manager**: '.concat(handler, ' (').concat(version, ')'),
'- **Browser**: '.concat(browserInfo),
'- **Date**: '.concat(
/* @__PURE__ */ new Date().toISOString().split('T')[0]
),
'',
'| API | GM.* (Support) | GM.* (Pass) | GM_* (Support) | GM_* (Pass) |',
'| :--- | :---: | :---: | :---: | :---: |',
]
for (const { name, gmRes, gmDotRes, isWindowApi } of resultsData) {
const formatCell = (res) => {
if (res.error) return 'Error'
if (res.message === 'N/A') return '-'
if (res.supported) {
return res.passed === res.total ? '\u2705' : '\u26A0\uFE0F'
}
return '\u274C'
}
const formatRate = (res) => {
if (res.error) return '-'
if (res.message === 'N/A') return '-'
return ''.concat(res.passed, '/').concat(res.total)
}
let cell1
let cell2
let cell3
let cell4
if (isWindowApi) {
cell1 = formatCell(gmRes)
cell2 = formatRate(gmRes)
cell3 = formatCell(gmDotRes)
cell4 = formatRate(gmDotRes)
} else {
cell1 = formatCell(gmDotRes)
cell2 = formatRate(gmDotRes)
cell3 = formatCell(gmRes)
cell4 = formatRate(gmRes)
}
lines.push(
'| '
.concat(name, ' | ')
.concat(cell1, ' | ')
.concat(cell2, ' | ')
.concat(cell3, ' | ')
.concat(cell4, ' |')
)
}
const markdown = lines.join('\n')
if (isGmSetClipboardWorking && typeof GM_setClipboard === 'function') {
GM_setClipboard(markdown, 'text')
} else if (
isGmDotSetClipboardWorking &&
typeof GM !== 'undefined' &&
typeof GM.setClipboard === 'function'
) {
void GM.setClipboard(markdown, 'text')
} else {
try {
await navigator.clipboard.writeText(markdown)
} catch (e) {
const textarea = document.createElement('textarea')
textarea.value = markdown
textarea.style.position = 'fixed'
textarea.style.bottom = '0'
textarea.style.left = '0'
textarea.style.width = '100%'
textarea.style.height = '150px'
textarea.style.zIndex = '2147483647'
if (host.shadowRoot) {
host.shadowRoot.append(textarea)
} else {
document.body.append(textarea)
}
textarea.focus()
textarea.select()
if (host.shadowRoot) {
const wrapper2 = host.shadowRoot.querySelector('div')
if (wrapper2) wrapper2.scrollTop = wrapper2.scrollHeight
}
appendLog(
'Clipboard write failed. Please copy manually from the textarea below.',
'error'
)
}
}
const originalText = copyBtn.textContent
copyBtn.textContent = 'Copied!'
setTimeout(() => {
copyBtn.textContent = originalText
}, 2e3)
})
const tbody = wrapper.querySelector('#benchmark-results-body')
tbody.innerHTML = ''
const totalPassed = 0
const totalTests = 0
for (const t of tests) {
const tr = document.createElement('tr')
tr.innerHTML = '<td>'.concat(
t.name,
'</td><td>...</td><td>...</td><td>...</td><td>...</td>'
)
tbody.append(tr)
let gmRes = { supported: false, passed: 0, total: 0 }
let gmDotRes = { supported: false, passed: 0, total: 0 }
try {
gmRes = await t.gmRun()
} catch (error) {
gmRes = { supported: false, passed: 0, total: 0, error }
}
if (t.gmDotRun) {
try {
gmDotRes = await t.gmDotRun()
} catch (error) {
gmDotRes = { supported: false, passed: 0, total: 0, error }
}
} else {
gmDotRes = { supported: false, passed: 0, total: 1, message: 'N/A' }
}
if (gmRes.error) {
const msg = ''.concat(t.name, ' (GM_): ').concat(String(gmRes.error))
appendLog(msg, 'error')
console.error(msg, gmRes.error)
}
if (gmDotRes.error) {
const msg = ''.concat(t.name, ' (GM.): ').concat(String(gmDotRes.error))
appendLog(msg, 'error')
console.error(msg, gmDotRes.error)
}
const isWindowApi = [
'unsafeWindow',
'window.onurlchange',
'window.close',
'window.focus',
].includes(t.name)
resultsData.push({ name: t.name, gmRes, gmDotRes, isWindowApi })
const renderCell = (res) => {
if (res.error) {
return '<td class="fail">Error</td><td class="fail" title="'.concat(
String(res.error),
'">-</td>'
)
}
if (res.message === 'N/A') {
return '<td class="na">-</td><td class="na">-</td>'
}
const passClass = res.supported ? 'pass' : 'fail'
const passRateClass = res.passed === res.total ? 'pass' : 'fail'
return '\n <td class="'
.concat(passClass, '" title="')
.concat(res.message || '', '">')
.concat(res.supported ? 'Yes' : 'No', '</td>\n <td class="')
.concat(passRateClass, '">')
.concat(res.passed, '/')
.concat(res.total, '</td>\n ')
}
let rowContent = '<td>'.concat(t.name, '</td>')
if (isWindowApi) {
rowContent += renderCell(gmRes) + renderCell(gmDotRes)
} else {
rowContent += renderCell(gmDotRes) + renderCell(gmRes)
}
tr.innerHTML = rowContent
}
}
function start() {
void render()
}
function main() {
try {
const de = document.documentElement
if (de && de.dataset && de.dataset.uab === '1') return
if (de && de.dataset) de.dataset.uab = '1'
} catch (e) {}
if (
typeof GM !== 'undefined' &&
typeof GM.registerMenuCommand === 'function'
) {
GM.registerMenuCommand('Run Benchmark', start)
} else if (typeof GM_registerMenuCommand === 'function') {
GM_registerMenuCommand('Run Benchmark', start)
} else {
start()
}
}
main()
})()