Move generateId to core.js (#748)

This commit is contained in:
toasted-nutbread 2020-08-22 15:49:24 -04:00 committed by GitHub
parent 9fa0f2a56a
commit f0c974d319
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 19 additions and 18 deletions

View File

@ -107,6 +107,7 @@
"escapeRegExp": "readonly", "escapeRegExp": "readonly",
"deferPromise": "readonly", "deferPromise": "readonly",
"clone": "readonly", "clone": "readonly",
"generateId": "readonly",
"EventDispatcher": "readonly", "EventDispatcher": "readonly",
"EventListenerCollection": "readonly" "EventListenerCollection": "readonly"
} }

View File

@ -739,7 +739,7 @@ class Backend {
if (typeof tabId !== 'number') { throw new Error('Sender has invalid tab ID'); } if (typeof tabId !== 'number') { throw new Error('Sender has invalid tab ID'); }
const frameId = sender.frameId; const frameId = sender.frameId;
const id = yomichan.generateId(16); const id = generateId(16);
const details = { const details = {
name: 'action-port', name: 'action-port',
id id

View File

@ -25,7 +25,7 @@ class SettingsController extends EventDispatcher {
constructor(profileIndex=0) { constructor(profileIndex=0) {
super(); super();
this._profileIndex = profileIndex; this._profileIndex = profileIndex;
this._source = yomichan.generateId(16); this._source = generateId(16);
this._pageExitPreventions = new Set(); this._pageExitPreventions = new Set();
this._pageExitPreventionEventListeners = new EventListenerCollection(); this._pageExitPreventionEventListeners = new EventListenerCollection();
} }

View File

@ -42,7 +42,7 @@ class FrameOffsetForwarder {
return [0, 0]; return [0, 0];
} }
const uniqueId = yomichan.generateId(16); const uniqueId = generateId(16);
const frameOffsetPromise = yomichan.getTemporaryListenerResult( const frameOffsetPromise = yomichan.getTemporaryListenerResult(
chrome.runtime.onMessage, chrome.runtime.onMessage,

View File

@ -33,7 +33,7 @@ class Frontend {
isSearchPage, isSearchPage,
allowRootFramePopupProxy allowRootFramePopupProxy
}) { }) {
this._id = yomichan.generateId(16); this._id = generateId(16);
this._popup = null; this._popup = null;
this._disabledOverride = false; this._disabledOverride = false;
this._options = null; this._options = null;

View File

@ -86,7 +86,7 @@ class PopupFactory {
if (frameId === this._frameId) { if (frameId === this._frameId) {
// New unique id // New unique id
if (id === null) { if (id === null) {
id = yomichan.generateId(16); id = generateId(16);
} }
const popup = new Popup(id, depth, frameId, ownerFrameId); const popup = new Popup(id, depth, frameId, ownerFrameId);
if (parent !== null) { if (parent !== null) {

View File

@ -197,6 +197,16 @@ if (typeof window === 'object' && window !== null) {
window.clone = clone; window.clone = clone;
} }
function generateId(length) {
const array = new Uint8Array(length);
crypto.getRandomValues(array);
let id = '';
for (const value of array) {
id += value.toString(16).padStart(2, '0');
}
return id;
}
/* /*
* Async utilities * Async utilities

View File

@ -166,7 +166,7 @@ class DisplayHistory extends EventDispatcher {
} }
_generateId() { _generateId() {
return yomichan.generateId(16); return generateId(16);
} }
_clear() { _clear() {

View File

@ -90,7 +90,7 @@ class FrameClient {
case 'frameEndpointReady': case 'frameEndpointReady':
{ {
const {secret} = params; const {secret} = params;
const token = yomichan.generateId(16); const token = generateId(16);
tokenMap.set(secret, token); tokenMap.set(secret, token);
postMessage('frameEndpointConnect', {secret, token, hostFrameId}); postMessage('frameEndpointConnect', {secret, token, hostFrameId});
} }

View File

@ -21,7 +21,7 @@
class FrameEndpoint { class FrameEndpoint {
constructor() { constructor() {
this._secret = yomichan.generateId(16); this._secret = generateId(16);
this._token = null; this._token = null;
this._eventListeners = new EventListenerCollection(); this._eventListeners = new EventListenerCollection();
this._eventListenersSetup = false; this._eventListenersSetup = false;

View File

@ -84,16 +84,6 @@ const yomichan = (() => {
this.sendMessage({action: 'yomichanReady'}); this.sendMessage({action: 'yomichanReady'});
} }
generateId(length) {
const array = new Uint8Array(length);
crypto.getRandomValues(array);
let id = '';
for (const value of array) {
id += value.toString(16).padStart(2, '0');
}
return id;
}
isExtensionUrl(url) { isExtensionUrl(url) {
try { try {
return url.startsWith(chrome.runtime.getURL('/')); return url.startsWith(chrome.runtime.getURL('/'));