From 4e87a060f566efcf9d4009ffad783486153186c3 Mon Sep 17 00:00:00 2001 From: tophf Date: Wed, 16 Jun 2021 20:04:45 +0300 Subject: [PATCH] use crypto.randomUUID if present --- background/style-manager.js | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/background/style-manager.js b/background/style-manager.js index d8867387..be99dccf 100644 --- a/background/style-manager.js +++ b/background/style-manager.js @@ -43,6 +43,13 @@ const styleMan = (() => { const compileRe = createCompiler(text => `^(${text})$`); const compileSloppyRe = createCompiler(text => `^${text}$`); const compileExclusion = createCompiler(buildExclusion); + const uuidv4 = crypto.randomUUID ? crypto.randomUUID.bind(crypto) : (() => { + const seeds = crypto.getRandomValues(new Uint16Array(8)); + // 00001111-2222-M333-N444-555566667777 + seeds[3] = seeds[3] & 0x0FFF | 0x4000; // UUID version 4, M = 4 + seeds[4] = seeds[4] & 0x3FFF | 0x8000; // UUID variant 1, N = 8..0xB + return Array.from(seeds, hex4dashed).join(''); + }); const MISSING_PROPS = { name: style => `ID: ${style.id}`, _id: () => uuidv4(), @@ -634,14 +641,6 @@ const styleMan = (() => { } } - function uuidv4() { - const seeds = crypto.getRandomValues(new Uint16Array(8)); - // 00001111-2222-M333-N444-555566667777 - seeds[3] = seeds[3] & 0x0FFF | 0x4000; // UUID version 4, M = 4 - seeds[4] = seeds[4] & 0x3FFF | 0x8000; // UUID variant 1, N = 8..0xB - return Array.from(seeds, hex4dashed).join(''); - } - /** uuidv4 helper: converts to a 4-digit hex string and adds "-" at required positions */ function hex4dashed(num, i) { return (num + 0x10000).toString(16).slice(-4) + (i >= 1 && i <= 4 ? '-' : '');