From 42f1c2463c8051d9cbbcacd43f06922c2f11ec71 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sun, 16 Feb 2020 11:59:17 -0500 Subject: Move generateId function --- ext/fg/js/frontend-api-sender.js | 10 +--------- ext/mixed/js/core.js | 10 ++++++++++ 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/ext/fg/js/frontend-api-sender.js b/ext/fg/js/frontend-api-sender.js index 93c2e593..8dc6aaf3 100644 --- a/ext/fg/js/frontend-api-sender.js +++ b/ext/fg/js/frontend-api-sender.js @@ -19,7 +19,7 @@ class FrontendApiSender { constructor() { - this.senderId = FrontendApiSender.generateId(16); + this.senderId = yomichan.generateId(16); this.ackTimeout = 3000; // 3 seconds this.responseTimeout = 10000; // 10 seconds this.callbacks = new Map(); @@ -123,12 +123,4 @@ class FrontendApiSender { info.timer = null; info.reject(new Error(reason)); } - - static generateId(length) { - let id = ''; - for (let i = 0; i < length; ++i) { - id += Math.floor(Math.random() * 256).toString(16).padStart(2, '0'); - } - return id; - } } diff --git a/ext/mixed/js/core.js b/ext/mixed/js/core.js index 2dd9cf40..b6ecb48b 100644 --- a/ext/mixed/js/core.js +++ b/ext/mixed/js/core.js @@ -257,6 +257,16 @@ const yomichan = (() => { // Public + generateId(length) { + const array = new Uint8Array(length); + window.crypto.getRandomValues(array); + let id = ''; + for (const value of array) { + id += value.toString(16).padStart(2, '0'); + } + return id; + } + triggerOrphaned(error) { this.trigger('orphaned', {error}); } -- cgit v1.2.3