From a7d6c68e3a572b0f21486f1290ef0e8a92f1d72d Mon Sep 17 00:00:00 2001 From: LobeliaComputing <31212444+ibuninngu@users.noreply.github.com> Date: Sat, 17 Feb 2024 20:48:28 +0900 Subject: [PATCH] _ --- src/js/postMessage.awaitable.module.js | 74 +++++++++++++------------- src/js/websocket.awaitable.js | 12 ++++- 2 files changed, 47 insertions(+), 39 deletions(-) diff --git a/src/js/postMessage.awaitable.module.js b/src/js/postMessage.awaitable.module.js index 1307627..5987229 100644 --- a/src/js/postMessage.awaitable.module.js +++ b/src/js/postMessage.awaitable.module.js @@ -3,48 +3,48 @@ ("use strict"); export class Connector { - constructor(tabId) { - this.tabId = tabId; - this.connector = null; - this.postPool = {}; - if (tabId) { - this.load(tabId); - } - } - - async load(tabId) { - this.tabId = tabId; - this.connector = await chrome.tabs.connect(tabId); - this.connector.onMessage.addListener((P) => { - if (Object.keys(this.postPool).includes(P.uuid)) { - this.postPool[P.uuid](P); - delete this.postPool[P.uuid]; - } - }); - this.connector.onDisconnect.addListener((P) => { - this.connector = null; - }); + constructor(tabId) { + this.tabId = tabId; + this.connector = null; + this.postPool = {}; + if (tabId) { + this.load(tabId); } + } - async post(P) { - let uuid = crypto.randomUUID(); + async load(tabId) { + this.tabId = tabId; + this.connector = await chrome.tabs.connect(tabId); + this.connector.onMessage.addListener((P) => { + if (P.uuid in this.postPool) { + this.postPool[P.uuid](P); + delete this.postPool[P.uuid]; + } + }); + this.connector.onDisconnect.addListener((P) => { + this.connector = null; + }); + } - let R = new Promise((resolve, reject) => { - this.postPool[uuid] = (_) => { - resolve(_); - }; - }); + async post(P) { + let uuid = crypto.randomUUID(); - if (!this.connector) { - await this.load(this.tabId); - } - this.connector.postMessage( - Object.assign(P, { - uuid: uuid, - }) - ); + let R = new Promise((resolve, reject) => { + this.postPool[uuid] = (_) => { + resolve(_); + }; + }); - return R; + if (!this.connector) { + await this.load(this.tabId); } + this.connector.postMessage( + Object.assign(P, { + uuid: uuid, + }) + ); + + return R; } +} diff --git a/src/js/websocket.awaitable.js b/src/js/websocket.awaitable.js index 1a38a9d..10b2fc4 100644 --- a/src/js/websocket.awaitable.js +++ b/src/js/websocket.awaitable.js @@ -2,7 +2,6 @@ // https://github.com/xoFeulB ("use strict"); - export class AwaitbleWebSocket { constructor(url) { this.socket = new WebSocket(url); @@ -39,7 +38,16 @@ export class AwaitbleWebSocket { async send(message) { let uuid = crypto.randomUUID(); this.socket.send( - JSON.stringify(Object.assign({ "message": message }, { uuid: uuid })) + JSON.stringify( + Object.assign( + { + message: message, + }, + { + uuid: uuid, + } + ) + ) ); let R = new Promise((resolve, reject) => { this.messagePool[uuid] = (_) => {