Skip to content

Commit ff2f728

Browse files
author
Hiram
committed
[optimize] crypto.getRandomValues judgements
1 parent 774d7d2 commit ff2f728

File tree

1 file changed

+7
-1
lines changed
  • apps/cookie-butler/static/js

1 file changed

+7
-1
lines changed

Diff for: apps/cookie-butler/static/js/core.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,16 @@ class QRCodeHandler {
3131
static generateUUID() {
3232
if (crypto && typeof crypto.randomUUID === 'function') {
3333
return crypto.randomUUID();
34-
} else {
34+
} else if (crypto && typeof crypto.getRandomValues === 'function') {
3535
return "10000000-1000-4000-8000-100000000000".replace(/[018]/g, c =>
3636
(+c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> +c / 4).toString(16)
3737
);
38+
} else {
39+
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
40+
const r = (Math.random() * 16) | 0,
41+
v = c == 'x' ? r : (r & 0x3) | 0x8;
42+
return v.toString(16);
43+
});
3844
}
3945
}
4046

0 commit comments

Comments
 (0)