57 lines
1.7 KiB
TypeScript
57 lines
1.7 KiB
TypeScript
export const randomUUIDPolyfillScript = `
|
|
(function () {
|
|
var root = typeof globalThis !== "undefined" ? globalThis : window;
|
|
var cryptoObject = root.crypto || root.msCrypto;
|
|
if (cryptoObject && typeof cryptoObject.randomUUID === "function") return;
|
|
if (!cryptoObject) {
|
|
cryptoObject = {};
|
|
try {
|
|
Object.defineProperty(root, "crypto", {
|
|
configurable: true,
|
|
value: cryptoObject
|
|
});
|
|
} catch (error) {
|
|
root.crypto = cryptoObject;
|
|
}
|
|
}
|
|
|
|
var getRandomValues = typeof cryptoObject.getRandomValues === "function"
|
|
? cryptoObject.getRandomValues.bind(cryptoObject)
|
|
: null;
|
|
|
|
function byteToHex(byte) {
|
|
return (byte + 256).toString(16).slice(1);
|
|
}
|
|
|
|
function createRandomUUID() {
|
|
var bytes = new Uint8Array(16);
|
|
if (getRandomValues) {
|
|
getRandomValues(bytes);
|
|
} else {
|
|
for (var index = 0; index < bytes.length; index += 1) {
|
|
bytes[index] = Math.floor(Math.random() * 256);
|
|
}
|
|
}
|
|
bytes[6] = (bytes[6] & 15) | 64;
|
|
bytes[8] = (bytes[8] & 63) | 128;
|
|
return (
|
|
byteToHex(bytes[0]) + byteToHex(bytes[1]) + byteToHex(bytes[2]) + byteToHex(bytes[3]) + "-" +
|
|
byteToHex(bytes[4]) + byteToHex(bytes[5]) + "-" +
|
|
byteToHex(bytes[6]) + byteToHex(bytes[7]) + "-" +
|
|
byteToHex(bytes[8]) + byteToHex(bytes[9]) + "-" +
|
|
byteToHex(bytes[10]) + byteToHex(bytes[11]) + byteToHex(bytes[12]) +
|
|
byteToHex(bytes[13]) + byteToHex(bytes[14]) + byteToHex(bytes[15])
|
|
);
|
|
}
|
|
|
|
try {
|
|
Object.defineProperty(cryptoObject, "randomUUID", {
|
|
configurable: true,
|
|
value: createRandomUUID
|
|
});
|
|
} catch (error) {
|
|
cryptoObject.randomUUID = createRandomUUID;
|
|
}
|
|
})();
|
|
`;
|