Provide a uuid function fallback which doesn't fail in the Tor Browser.

This commit is contained in:
hackademix 2018-07-03 19:35:33 +02:00
parent 5c71c94a13
commit 8ae9513f75
1 changed files with 12 additions and 3 deletions

View File

@ -1,6 +1,15 @@
'use strict'; 'use strict';
function uuid() { function uuid() {
try {
return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g,
c => (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4) c => (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4)
.toString(16)); .toString(16));
} catch (e) {
// fallback, as the Tor Browser seems to fail above
uuid = () => 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
let r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
} }