From a279e55519e9c8c861ea69ed1816e328a180b11e Mon Sep 17 00:00:00 2001 From: Twilight <46562212+twlite@users.noreply.github.com> Date: Mon, 28 Oct 2024 16:24:09 +0545 Subject: [PATCH] fix: use native uuid generator when possible --- chatgpt.js | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/chatgpt.js b/chatgpt.js index faabd835f..90a7ed48c 100644 --- a/chatgpt.js +++ b/chatgpt.js @@ -1872,14 +1872,19 @@ const chatgpt = { // eslint-disable-line no-redeclare unminify() { chatgpt.code.unminify(); }, uuidv4() { - let d = new Date().getTime(); // get current timestamp in ms (to ensure UUID uniqueness) - const uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => { - const r = ( // generate random nibble - ( d + (window.crypto.getRandomValues(new Uint32Array(1))[0] / (Math.pow(2, 32) - 1))*16)%16 | 0 ); - d = Math.floor(d/16); // correspond each UUID digit to unique 4-bit chunks of timestamp - return ( c == 'x' ? r : (r&0x3|0x8) ).toString(16); // generate random hexadecimal digit - }); - return uuid; + try { + // use native secure uuid generator when available + return crypto.randomUUID(); + } catch(_e) { + let d = new Date().getTime(); // get current timestamp in ms (to ensure UUID uniqueness) + const uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => { + const r = ( // generate random nibble + ( d + (window.crypto.getRandomValues(new Uint32Array(1))[0] / (Math.pow(2, 32) - 1))*16)%16 | 0 ); + d = Math.floor(d/16); // correspond each UUID digit to unique 4-bit chunks of timestamp + return ( c == 'x' ? r : (r&0x3|0x8) ).toString(16); // generate random hexadecimal digit + }); + return uuid; + } }, writeCode() { chatgpt.code.write(); }